66from asyncio import Future
77from typing import Optional , List , Union , Callable , Pattern , Dict , Awaitable
88
9+ from slack_sdk .oauth import OAuthStateUtils
10+ from slack_sdk .oauth .installation_store import FileInstallationStore
11+ from slack_sdk .oauth .installation_store .async_installation_store import (
12+ AsyncInstallationStore ,
13+ )
14+ from slack_sdk .oauth .state_store import FileOAuthStateStore
15+ from slack_sdk .oauth .state_store .async_state_store import AsyncOAuthStateStore
16+ from slack_sdk .web .async_client import AsyncWebClient
17+
18+ from slack_bolt .error import BoltError
919from slack_bolt .listener .async_listener import AsyncListener , AsyncCustomListener
1020from slack_bolt .listener_matcher import builtins as builtin_matchers
1121from slack_bolt .listener_matcher .async_listener_matcher import (
3242from slack_bolt .oauth .async_oauth_flow import AsyncOAuthFlow
3343from slack_bolt .request .async_request import AsyncBoltRequest
3444from slack_bolt .response import BoltResponse
35- from slack_sdk .oauth import OAuthStateUtils
36- from slack_sdk .oauth .installation_store import FileInstallationStore
37- from slack_sdk .oauth .installation_store .async_installation_store import (
38- AsyncInstallationStore ,
39- )
40- from slack_sdk .oauth .state_store import FileOAuthStateStore
41- from slack_sdk .oauth .state_store .async_state_store import AsyncOAuthStateStore
42- from slack_sdk .web .async_client import AsyncWebClient
45+ from slack_bolt .util .async_utils import create_async_web_client
4346
4447
4548class AsyncApp :
@@ -51,9 +54,9 @@ def __init__(
5154 # Set True when you run this app on a FaaS platform
5255 process_before_response : bool = False ,
5356 # Basic Information > Credentials > Signing Secret
54- signing_secret : str = os . environ . get ( "SLACK_SIGNING_SECRET" , None ) ,
57+ signing_secret : Optional [ str ] = None ,
5558 # for single-workspace apps
56- token : Optional [str ] = os . environ . get ( "SLACK_BOT_TOKEN" , None ) ,
59+ token : Optional [str ] = None ,
5760 client : Optional [AsyncWebClient ] = None ,
5861 # for multi-workspace apps
5962 installation_store : Optional [AsyncInstallationStore ] = None ,
@@ -74,6 +77,14 @@ def __init__(
7477 # No need to set (the value is used only in response to ssl_check requests)
7578 verification_token : Optional [str ] = None ,
7679 ):
80+ signing_secret = signing_secret or os .environ .get ("SLACK_SIGNING_SECRET" , None )
81+ token = token or os .environ .get ("SLACK_BOT_TOKEN" , None )
82+
83+ if signing_secret is None or signing_secret == "" :
84+ raise BoltError (
85+ "Signing secret not found, so could not initialize the Bolt app."
86+ )
87+
7788 self ._name : str = name or inspect .stack ()[1 ].filename .split (os .path .sep )[- 1 ]
7889 self ._signing_secret : str = signing_secret
7990
@@ -104,9 +115,8 @@ def __init__(
104115 "As you gave client as well, the bot token will be unused."
105116 )
106117 else :
107- self ._async_client = AsyncWebClient (
108- token = token
109- ) # NOTE: the token here can be None
118+ # NOTE: the token here can be None
119+ self ._async_client = create_async_web_client (token )
110120
111121 self ._async_installation_store : Optional [
112122 AsyncInstallationStore
@@ -154,7 +164,7 @@ def __init__(
154164 )
155165
156166 self ._async_oauth_flow = AsyncOAuthFlow (
157- client = AsyncWebClient ( token = None ),
167+ client = create_async_web_client ( ),
158168 logger = self ._framework_logger ,
159169 # required storage implementations
160170 installation_store = self ._async_installation_store ,
@@ -198,12 +208,18 @@ def _init_async_middleware_list(self):
198208 self ._async_middleware_list .append (
199209 AsyncRequestVerification (self ._signing_secret )
200210 )
201- if self ._async_oauth_flow is None and self ._token :
202- self ._async_middleware_list .append (AsyncSingleTeamAuthorization ())
211+ if self ._async_oauth_flow is None :
212+ if self ._token :
213+ self ._async_middleware_list .append (AsyncSingleTeamAuthorization ())
214+ else :
215+ raise BoltError (
216+ "AsyncOAuthFlow not found, so could not initialize the Bolt app."
217+ )
203218 else :
204219 self ._async_middleware_list .append (
205220 AsyncMultiTeamsAuthorization (self ._async_installation_store )
206221 )
222+
207223 self ._async_middleware_list .append (AsyncIgnoringSelfEvents ())
208224 self ._async_middleware_list .append (AsyncUrlVerification ())
209225 self ._init_middleware_list_done = True
@@ -557,6 +573,12 @@ def _register_listener(
557573 auto_acknowledgement : bool = False ,
558574 ) -> Callable [..., None ]:
559575
576+ if not inspect .iscoroutinefunction (func ):
577+ name = func .__name__
578+ raise BoltError (
579+ f"The listener function ({ name } ) is not a coroutine function."
580+ )
581+
560582 listener_matchers = [
561583 AsyncCustomListenerMatcher (app_name = self .name , func = f )
562584 for f in (matchers or [])
0 commit comments