7777 AsyncRequestVerification ,
7878 AsyncIgnoringSelfEvents ,
7979 AsyncUrlVerification ,
80+ AsyncAttachingFunctionToken ,
8081)
8182from slack_bolt .middleware .async_custom_middleware import (
8283 AsyncMiddleware ,
@@ -122,6 +123,7 @@ def __init__(
122123 ignoring_self_events_enabled : bool = True ,
123124 ssl_check_enabled : bool = True ,
124125 url_verification_enabled : bool = True ,
126+ attaching_function_token_enabled : bool = True ,
125127 # for the OAuth flow
126128 oauth_settings : Optional [AsyncOAuthSettings ] = None ,
127129 oauth_flow : Optional [AsyncOAuthFlow ] = None ,
@@ -184,6 +186,8 @@ async def message_hello(message, say): # async function
184186 that verify the endpoint for Events API in HTTP Mode requests.
185187 ssl_check_enabled: bool = False if you would like to disable the built-in middleware (Default: True).
186188 `AsyncSslCheck` is a built-in middleware that handles ssl_check requests from Slack.
189+ attaching_function_token_enabled: False if you would like to disable the built-in middleware (Default: True).
190+ `AsyncAttachingFunctionToken` is a built-in middleware that handles tokens with function requests from Slack.
187191 oauth_settings: The settings related to Slack app installation flow (OAuth flow)
188192 oauth_flow: Instantiated `slack_bolt.oauth.AsyncOAuthFlow`. This is always prioritized over oauth_settings.
189193 verification_token: Deprecated verification mechanism. This can used only for ssl_check requests.
@@ -354,6 +358,7 @@ async def message_hello(message, say): # async function
354358 ignoring_self_events_enabled = ignoring_self_events_enabled ,
355359 ssl_check_enabled = ssl_check_enabled ,
356360 url_verification_enabled = url_verification_enabled ,
361+ attaching_function_token_enabled = attaching_function_token_enabled ,
357362 )
358363
359364 self ._server : Optional [AsyncSlackAppServer ] = None
@@ -364,6 +369,7 @@ def _init_async_middleware_list(
364369 ignoring_self_events_enabled : bool = True ,
365370 ssl_check_enabled : bool = True ,
366371 url_verification_enabled : bool = True ,
372+ attaching_function_token_enabled : bool = True ,
367373 ):
368374 if self ._init_middleware_list_done :
369375 return
@@ -403,6 +409,8 @@ def _init_async_middleware_list(
403409 self ._async_middleware_list .append (AsyncIgnoringSelfEvents (base_logger = self ._base_logger ))
404410 if url_verification_enabled is True :
405411 self ._async_middleware_list .append (AsyncUrlVerification (base_logger = self ._base_logger ))
412+ if attaching_function_token_enabled is True :
413+ self ._async_middleware_list .append (AsyncAttachingFunctionToken ())
406414 self ._init_middleware_list_done = True
407415
408416 # -------------------------
@@ -861,6 +869,52 @@ def __call__(*args, **kwargs):
861869
862870 return __call__
863871
872+ def function (
873+ self ,
874+ callback_id : Union [str , Pattern ],
875+ matchers : Optional [Sequence [Callable [..., Awaitable [bool ]]]] = None ,
876+ middleware : Optional [Sequence [Union [Callable , AsyncMiddleware ]]] = None ,
877+ ) -> Callable [..., Optional [Callable [..., Awaitable [BoltResponse ]]]]:
878+ """Registers a new Function listener.
879+ This method can be used as either a decorator or a method.
880+ # Use this method as a decorator
881+ @app.function("reverse")
882+ async def reverse_string(event, client: AsyncWebClient, complete: AsyncComplete):
883+ try:
884+ string_to_reverse = event["inputs"]["stringToReverse"]
885+ await client.functions_completeSuccess(
886+ function_execution_id=context.function_execution_id,
887+ outputs={"reverseString": string_to_reverse[::-1]},
888+ )
889+ except Exception as e:
890+ await client.functions_completeError(
891+ function_execution_id=context.function_execution_id,
892+ error=f"Cannot reverse string (error: {e})",
893+ )
894+ raise e
895+ # Pass a function to this method
896+ app.function("reverse")(reverse_string)
897+ To learn available arguments for middleware/listeners, see `slack_bolt.kwargs_injection.async_args`'s API document.
898+ Args:
899+ callback_id: The callback id to identify the function
900+ matchers: A list of listener matcher functions.
901+ Only when all the matchers return True, the listener function can be invoked.
902+ middleware: A list of lister middleware functions.
903+ Only when all the middleware call `next()` method, the listener function can be invoked.
904+ """
905+
906+ matchers = list (matchers ) if matchers else []
907+ middleware = list (middleware ) if middleware else []
908+
909+ def __call__ (* args , ** kwargs ):
910+ functions = self ._to_listener_functions (kwargs ) if kwargs else list (args )
911+ primary_matcher = builtin_matchers .function_executed (
912+ callback_id = callback_id , base_logger = self ._base_logger , asyncio = True
913+ )
914+ return self ._register_listener (functions , primary_matcher , matchers , middleware , True )
915+
916+ return __call__
917+
864918 # -------------------------
865919 # slash commands
866920
0 commit comments