77# ------------------------------------------------
88
99import logging
10+
11+ logging .basicConfig (level = logging .DEBUG )
12+
1013import os
1114
1215from slack_bolt .app .async_app import AsyncApp
13- from slack_bolt .adapter .socket_mode .aiohttp import AsyncSocketModeHandler
16+ from slack_bolt .adapter .socket_mode .async_handler import AsyncSocketModeHandler
1417
1518# Install the Slack app and get xoxb- token in advance
1619app = AsyncApp (token = os .environ ["SLACK_BOT_TOKEN" ])
@@ -27,20 +30,83 @@ async def event_test(event, say):
2730 await say (f"Hi there, <@{ event ['user' ]} >!" )
2831
2932
30- @app .shortcut ("socket-mode" )
31- async def global_shortcut (ack ):
33+ async def ack_shortcut (ack ):
34+ await ack ()
35+
36+
37+ async def open_modal (body , client ):
38+ await client .views_open (
39+ trigger_id = body ["trigger_id" ],
40+ view = {
41+ "type" : "modal" ,
42+ "callback_id" : "socket_modal_submission" ,
43+ "submit" : {"type" : "plain_text" , "text" : "Submit" ,},
44+ "close" : {"type" : "plain_text" , "text" : "Cancel" ,},
45+ "title" : {"type" : "plain_text" , "text" : "Socket Modal" ,},
46+ "blocks" : [
47+ {
48+ "type" : "input" ,
49+ "block_id" : "q1" ,
50+ "label" : {"type" : "plain_text" , "text" : "Write anything here!" ,},
51+ "element" : {"action_id" : "feedback" , "type" : "plain_text_input" ,},
52+ },
53+ {
54+ "type" : "input" ,
55+ "block_id" : "q2" ,
56+ "label" : {
57+ "type" : "plain_text" ,
58+ "text" : "Can you tell us your favorites?" ,
59+ },
60+ "element" : {
61+ "type" : "external_select" ,
62+ "action_id" : "favorite-animal" ,
63+ "min_query_length" : 0 ,
64+ "placeholder" : {
65+ "type" : "plain_text" ,
66+ "text" : "Select your favorites" ,
67+ },
68+ },
69+ },
70+ ],
71+ },
72+ )
73+
74+
75+ app .shortcut ("socket-mode" )(ack = ack_shortcut , lazy = [open_modal ])
76+
77+
78+ all_options = [
79+ {"text" : {"type" : "plain_text" , "text" : ":cat: Cat" }, "value" : "cat" ,},
80+ {"text" : {"type" : "plain_text" , "text" : ":dog: Dog" }, "value" : "dog" ,},
81+ {"text" : {"type" : "plain_text" , "text" : ":bear: Bear" }, "value" : "bear" ,},
82+ ]
83+
84+
85+ @app .options ("favorite-animal" )
86+ async def external_data_source_handler (ack , body ):
87+ keyword = body .get ("value" )
88+ if keyword is not None and len (keyword ) > 0 :
89+ options = [o for o in all_options if keyword in o ["text" ]["text" ]]
90+ await ack (options = options )
91+ else :
92+ await ack (options = all_options )
93+
94+
95+ @app .view ("socket_modal_submission" )
96+ async def submission (ack ):
3297 await ack ()
3398
3499
35100# export SLACK_APP_TOKEN=xapp-***
36101# export SLACK_BOT_TOKEN=xoxb-***
37102
103+
38104async def main ():
39105 handler = AsyncSocketModeHandler (app , os .environ ["SLACK_APP_TOKEN" ])
40106 await handler .start_async ()
41107
42108
43109if __name__ == "__main__" :
44- logging .basicConfig (level = logging .DEBUG )
45110 import asyncio
111+
46112 asyncio .run (main ())
0 commit comments