Skip to content

Commit 538c2fb

Browse files
authored
Update ack() repsond() args (slackapi#33)
* Add attachments, options, option_groups to ack() * Add attachments to respond() * Enable ack(), respond() to accept dict values * Enable ack(), respond() to accept slack_sdk.models.JsonObject in arrays
1 parent 7e75a97 commit 538c2fb

18 files changed

Lines changed: 527 additions & 74 deletions

samples/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def event_test(payload, say, logger):
2929
if __name__ == "__main__":
3030
app.start(3000)
3131

32-
# pip install -i https://test.pypi.org/simple/ slack_bolt
32+
# pip install slack_bolt
3333
# export SLACK_SIGNING_SECRET=***
3434
# export SLACK_BOT_TOKEN=xoxb-***
3535
# python app.py

samples/async_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async def command(ack):
3535
if __name__ == "__main__":
3636
app.start(3000)
3737

38-
# pip install -i https://test.pypi.org/simple/ slack_bolt
38+
# pip install slack_bolt
3939
# export SLACK_SIGNING_SECRET=***
4040
# export SLACK_BOT_TOKEN=xoxb-***
4141
# python app.py

samples/async_modals_app.py

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# ------------------------------------------------
2+
# instead of slack_bolt in requirements.txt
3+
import asyncio
4+
import sys
5+
6+
sys.path.insert(1, "..")
7+
# ------------------------------------------------
8+
9+
import logging
10+
11+
logging.basicConfig(level=logging.DEBUG)
12+
13+
from slack_bolt.async_app import AsyncApp
14+
15+
app = AsyncApp()
16+
17+
18+
@app.middleware # or app.use(log_request)
19+
async def log_request(logger, payload, next):
20+
logger.debug(payload)
21+
return await next()
22+
23+
24+
@app.command("/hello-bolt-python")
25+
async def handle_command(payload, ack, respond, client, logger):
26+
logger.info(payload)
27+
await ack("Accepted!")
28+
29+
await respond(
30+
blocks=[
31+
{
32+
"type": "section",
33+
"block_id": "b",
34+
"text": {
35+
"type": "mrkdwn",
36+
"text": "You can add a button alongside text in your message. ",
37+
},
38+
"accessory": {
39+
"type": "button",
40+
"action_id": "a",
41+
"text": {"type": "plain_text", "text": "Button"},
42+
"value": "click_me_123",
43+
},
44+
}
45+
]
46+
)
47+
48+
res = await client.views_open(
49+
trigger_id=payload["trigger_id"],
50+
view={
51+
"type": "modal",
52+
"callback_id": "view-id",
53+
"title": {"type": "plain_text", "text": "My App",},
54+
"submit": {"type": "plain_text", "text": "Submit",},
55+
"close": {"type": "plain_text", "text": "Cancel",},
56+
"blocks": [
57+
{
58+
"type": "input",
59+
"element": {"type": "plain_text_input"},
60+
"label": {"type": "plain_text", "text": "Label",},
61+
},
62+
{
63+
"type": "input",
64+
"block_id": "es_b",
65+
"element": {
66+
"type": "external_select",
67+
"action_id": "es_a",
68+
"placeholder": {"type": "plain_text", "text": "Select an item"},
69+
},
70+
"label": {"type": "plain_text", "text": "Search"},
71+
},
72+
{
73+
"type": "input",
74+
"block_id": "mes_b",
75+
"element": {
76+
"type": "multi_external_select",
77+
"action_id": "mes_a",
78+
"placeholder": {"type": "plain_text", "text": "Select an item"},
79+
},
80+
"label": {"type": "plain_text", "text": "Search (multi)"},
81+
},
82+
],
83+
},
84+
)
85+
logger.info(res)
86+
87+
88+
@app.options("es_a")
89+
async def show_options(ack):
90+
await ack(
91+
{"options": [{"text": {"type": "plain_text", "text": "Maru"}, "value": "maru"}]}
92+
)
93+
94+
95+
@app.options("mes_a")
96+
async def show_multi_options(ack):
97+
await ack(
98+
{
99+
"option_groups": [
100+
{
101+
"label": {"type": "plain_text", "text": "Group 1"},
102+
"options": [
103+
{
104+
"text": {"type": "plain_text", "text": "Option 1"},
105+
"value": "1-1",
106+
},
107+
{
108+
"text": {"type": "plain_text", "text": "Option 2"},
109+
"value": "1-2",
110+
},
111+
],
112+
},
113+
{
114+
"label": {"type": "plain_text", "text": "Group 2"},
115+
"options": [
116+
{
117+
"text": {"type": "plain_text", "text": "Option 1"},
118+
"value": "2-1",
119+
},
120+
],
121+
},
122+
]
123+
}
124+
)
125+
126+
127+
@app.view("view-id")
128+
async def view_submission(ack, payload, logger):
129+
await ack()
130+
logger.info(payload["view"]["state"]["values"])
131+
132+
133+
@app.action("a")
134+
async def button_click(ack, respond):
135+
await ack()
136+
await asyncio.sleep(5)
137+
await respond(
138+
{"response_type": "in_channel", "text": "Clicked!",}
139+
)
140+
141+
142+
if __name__ == "__main__":
143+
app.start(3000)
144+
145+
# pip install slack_bolt
146+
# export SLACK_SIGNING_SECRET=***
147+
# export SLACK_BOT_TOKEN=xoxb-***
148+
# python modals_app.py

samples/dialogs_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def dialog_cancellation(ack):
8383
if __name__ == "__main__":
8484
app.start(3000)
8585

86-
# pip install -i https://test.pypi.org/simple/ slack_bolt
86+
# pip install slack_bolt
8787
# export SLACK_SIGNING_SECRET=***
8888
# export SLACK_BOT_TOKEN=xoxb-***
8989
# python dialogs_app.py

samples/events_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def bot_message_deleted(logger):
6767
if __name__ == "__main__":
6868
app.start(3000)
6969

70-
# pip install -i https://test.pypi.org/simple/ slack_bolt
70+
# pip install slack_bolt
7171
# export SLACK_SIGNING_SECRET=***
7272
# export SLACK_BOT_TOKEN=xoxb-***
7373
# python events_app.py

samples/modals_app.py

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from slack_bolt import App
1313

14-
app = App(process_before_response=True)
14+
app = App()
1515

1616

1717
@app.middleware # or app.use(log_request)
@@ -21,10 +21,18 @@ def log_request(logger, payload, next):
2121

2222

2323
@app.command("/hello-bolt-python")
24-
# or app.command(re.compile(r"/hello-.+"))(test_command)
25-
def test_command(payload, respond, client, ack, logger):
24+
def handle_command(payload, ack, respond, client, logger):
2625
logger.info(payload)
27-
ack("Thanks!")
26+
ack(
27+
text="Accepted!",
28+
blocks=[
29+
{
30+
"type": "section",
31+
"block_id": "b",
32+
"text": {"type": "mrkdwn", "text": ":white_check_mark: Accepted!",},
33+
}
34+
],
35+
)
2836

2937
respond(
3038
blocks=[
@@ -66,6 +74,7 @@ def test_command(payload, respond, client, ack, logger):
6674
"type": "external_select",
6775
"action_id": "es_a",
6876
"placeholder": {"type": "plain_text", "text": "Select an item"},
77+
"min_query_length": 0,
6978
},
7079
"label": {"type": "plain_text", "text": "Search"},
7180
},
@@ -76,6 +85,7 @@ def test_command(payload, respond, client, ack, logger):
7685
"type": "multi_external_select",
7786
"action_id": "mes_a",
7887
"placeholder": {"type": "plain_text", "text": "Select an item"},
88+
"min_query_length": 0,
7989
},
8090
"label": {"type": "plain_text", "text": "Search (multi)"},
8191
},
@@ -126,22 +136,33 @@ def show_multi_options(ack):
126136

127137
@app.view("view-id")
128138
def view_submission(ack, payload, logger):
129-
logger.info(payload)
130-
return ack()
139+
ack()
140+
logger.info(payload["view"]["state"]["values"])
131141

132142

133143
@app.action("a")
134-
def button_click(logger, payload, ack, respond):
135-
logger.info(payload)
136-
respond("respond!")
137-
# say(text="say!")
144+
def button_click(ack, payload, respond):
138145
ack()
139146

147+
user_id = payload["user"]["id"]
148+
# in_channel / dict
149+
respond(
150+
{
151+
"response_type": "in_channel",
152+
"replace_original": False,
153+
"text": f"<@{user_id}> clicked a button! (in_channel)",
154+
}
155+
)
156+
# ephemeral / kwargs
157+
respond(
158+
replace_original=False, text=":white_check_mark: Done!",
159+
)
160+
140161

141162
if __name__ == "__main__":
142163
app.start(3000)
143164

144-
# pip install -i https://test.pypi.org/simple/ slack_bolt
165+
# pip install slack_bolt
145166
# export SLACK_SIGNING_SECRET=***
146167
# export SLACK_BOT_TOKEN=xoxb-***
147168
# python modals_app.py

0 commit comments

Comments
 (0)