-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathlazy_async_modals_app.py
164 lines (138 loc) · 4.53 KB
/
lazy_async_modals_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import asyncio
import logging
from slack_bolt.async_app import AsyncApp
logging.basicConfig(level=logging.DEBUG)
app = AsyncApp()
@app.middleware # or app.use(log_request)
async def log_request(logger, body, next):
logger.debug(body)
return await next()
async def ack_command(body, ack, logger):
logger.info(body)
await ack("Thanks!")
async def post_button_message(respond):
await respond(
blocks=[
{
"type": "section",
"block_id": "b",
"text": {
"type": "mrkdwn",
"text": "You can add a button alongside text in your message. ",
},
"accessory": {
"type": "button",
"action_id": "a",
"text": {"type": "plain_text", "text": "Button"},
"value": "click_me_123",
},
}
]
)
async def open_modal(body, client, logger):
res = await client.views_open(
trigger_id=body["trigger_id"],
view={
"type": "modal",
"callback_id": "view-id",
"title": {
"type": "plain_text",
"text": "My App",
},
"submit": {
"type": "plain_text",
"text": "Submit",
},
"close": {
"type": "plain_text",
"text": "Cancel",
},
"blocks": [
{
"type": "input",
"element": {"type": "plain_text_input"},
"label": {
"type": "plain_text",
"text": "Label",
},
},
{
"type": "input",
"block_id": "es_b",
"element": {
"type": "external_select",
"action_id": "es_a",
"placeholder": {"type": "plain_text", "text": "Select an item"},
"min_query_length": 0,
},
"label": {"type": "plain_text", "text": "Search"},
},
{
"type": "input",
"block_id": "mes_b",
"element": {
"type": "multi_external_select",
"action_id": "mes_a",
"placeholder": {"type": "plain_text", "text": "Select an item"},
"min_query_length": 0,
},
"label": {"type": "plain_text", "text": "Search (multi)"},
},
],
},
)
logger.info(res)
app.command("/hello-bolt-python")(
ack=ack_command,
lazy=[post_button_message, open_modal],
)
@app.options("es_a")
async def show_options(ack):
await ack({"options": [{"text": {"type": "plain_text", "text": "Maru"}, "value": "maru"}]})
@app.options("mes_a")
async def show_multi_options(ack):
await ack(
{
"option_groups": [
{
"label": {"type": "plain_text", "text": "Group 1"},
"options": [
{
"text": {"type": "plain_text", "text": "Option 1"},
"value": "1-1",
},
{
"text": {"type": "plain_text", "text": "Option 2"},
"value": "1-2",
},
],
},
{
"label": {"type": "plain_text", "text": "Group 2"},
"options": [
{
"text": {"type": "plain_text", "text": "Option 1"},
"value": "2-1",
},
],
},
]
}
)
@app.view("view-id")
async def handle_view_submission(ack, body, logger):
await ack()
logger.info(body["view"]["state"]["values"])
async def ack_button_click(ack, respond):
await ack()
await respond("Loading ...")
async def respond_5_seconds_later(respond):
await asyncio.sleep(5)
await respond("Completed!")
app.action("a")(ack=ack_button_click, lazy=[respond_5_seconds_later])
if __name__ == "__main__":
app.start(3000)
# pip install slack_bolt
# export SLACK_SIGNING_SECRET=***
# export SLACK_BOT_TOKEN=xoxb-***
# python modals_app.py