Skip to content

Commit d777e39

Browse files
committed
Update README
1 parent 5747525 commit d777e39

File tree

3 files changed

+62
-12
lines changed

3 files changed

+62
-12
lines changed

README.md

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,6 @@ from slack_bolt import App
3535
# export SLACK_BOT_TOKEN=xoxb-***
3636
app = App()
3737

38-
# Middleware
39-
@app.middleware # or app.use(log_request)
40-
def log_request(logger, body, next):
41-
logger.info(body)
42-
return next()
43-
4438
# Events API: https://api.slack.com/events-api
4539
@app.event("app_mention")
4640
def event_test(say):
@@ -84,10 +78,10 @@ def open_modal(ack, client, logger, body):
8478
logger.debug(api_response)
8579

8680
@app.view("view-id")
87-
def view_submission(ack, body, logger):
81+
def view_submission(ack, view, logger):
8882
ack()
8983
# Prints {'b': {'a': {'type': 'plain_text_input', 'value': 'Your Input'}}}
90-
logger.info(body["view"]["state"]["values"])
84+
logger.info(view["state"]["values"])
9185

9286
if __name__ == "__main__":
9387
app.start(3000) # POST http://localhost:3000/slack/events
@@ -104,6 +98,53 @@ python app.py
10498
ngrok http 3000
10599
```
106100

101+
## AsyncApp Setup
102+
103+
If you prefer building Slack apps using [asyncio](https://docs.python.org/3/library/asyncio.html), you can go with `AsyncApp` instead. You can use async/await style for everything in the app. To use `AsyncApp`, [AIOHTTP](https://docs.aiohttp.org/en/stable/) library is required for asynchronous Slack Web API calls and the default web server.
104+
105+
```bash
106+
python -m venv env
107+
source env/bin/activate
108+
pip install slack_bolt aiohttp
109+
```
110+
111+
Import `slack_bolt.async_app.AsyncApp` instead of `slack_bolt.App`. All middleware/listeners must be async functions. Inside the functions, all utility methods such as `ack`, `say`, and `respond` requires `await` keyword.
112+
113+
```python
114+
from slack_bolt.async_app import AsyncApp
115+
116+
app = AsyncApp()
117+
118+
@app.event("app_mention")
119+
async def event_test(body, say, logger):
120+
logger.info(body)
121+
await say("What's up?")
122+
123+
@app.command("/hello-bolt-python")
124+
async def command(ack, body, respond):
125+
await ack()
126+
await respond(f"Hi <@{body['user_id']}>!")
127+
128+
if __name__ == "__main__":
129+
app.start(3000)
130+
```
131+
132+
Starting the app is exactly the same with the way using `slack_bolt.App`.
133+
134+
```bash
135+
export SLACK_SIGNING_SECRET=***
136+
export SLACK_BOT_TOKEN=xoxb-***
137+
python app.py
138+
139+
# in another terminal
140+
ngrok http 3000
141+
```
142+
143+
If you want to use another async Web framework (e.g., Sanic, FastAPI, Starlette), take a look at the built-in adapters and their samples.
144+
145+
* [The Bolt app samples](https://github.com/slackapi/bolt-python/tree/main/samples)
146+
* [The built-in adapters](https://github.com/slackapi/bolt-python/tree/main/slack_bolt/adapter)
147+
107148
# Feedback
108149

109150
We are keen to hear your feedback. Please feel free to [submit an issue](https://github.com/slackapi/bolt-python/issues)!

samples/readme_app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ def open_modal(ack, client, logger, body):
5050

5151

5252
@app.view("view-id")
53-
def view_submission(ack, body, logger):
53+
def view_submission(ack, view, logger):
5454
ack()
5555
# Prints {'b': {'a': {'type': 'plain_text_input', 'value': 'Your Input'}}}
56-
logger.info(body["view"]["state"]["values"])
56+
logger.info(view["state"]["values"])
5757

5858

5959
if __name__ == "__main__":

samples/readme_async_app.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010
app = AsyncApp()
1111

1212

13+
from slack_bolt.async_app import AsyncApp
14+
15+
app = AsyncApp()
16+
17+
@app.command("/hello-bolt-python")
18+
async def command(ack, body, respond):
19+
await ack()
20+
await respond(f"Hi <@{body['user_id']}>!")
21+
1322
# Middleware
1423
@app.middleware # or app.use(log_request)
1524
async def log_request(logger, body, next):
@@ -51,10 +60,10 @@ async def open_modal(ack, client, logger, body):
5160

5261

5362
@app.view("view-id")
54-
async def view_submission(ack, body, logger):
63+
async def view_submission(ack, view, logger):
5564
await ack()
5665
# Prints {'b': {'a': {'type': 'plain_text_input', 'value': 'Your Input'}}}
57-
logger.info(body["view"]["state"]["values"])
66+
logger.info(view["state"]["values"])
5867

5968

6069
if __name__ == "__main__":

0 commit comments

Comments
 (0)