|
| 1 | +"""Module for creating asyncio based apps |
| 2 | +
|
| 3 | +### Creating an async app |
| 4 | +
|
| 5 | +If you'd prefer to build your app with [asyncio](https://docs.python.org/3/library/asyncio.html), you can import the [AIOHTTP](https://docs.aiohttp.org/en/stable/) library and call the `AsyncApp` constructor. Within async apps, you can use the async/await pattern. |
| 6 | +
|
| 7 | +```bash |
| 8 | +# Python 3.6+ required |
| 9 | +python -m venv .venv |
| 10 | +source .venv/bin/activate |
| 11 | +
|
| 12 | +pip install -U pip |
| 13 | +# aiohttp is required |
| 14 | +pip install slack_bolt aiohttp |
| 15 | +``` |
| 16 | +
|
| 17 | +In async apps, all middleware/listeners must be async functions. When calling utility methods (like `ack` and `say`) within these functions, it's required to use the `await` keyword. |
| 18 | +
|
| 19 | +```python |
| 20 | +# Import the async app instead of the regular one |
| 21 | +from slack_bolt.async_app import AsyncApp |
| 22 | +
|
| 23 | +app = AsyncApp() |
| 24 | +
|
| 25 | +@app.event("app_mention") |
| 26 | +async def event_test(body, say, logger): |
| 27 | + logger.info(body) |
| 28 | + await say("What's up?") |
| 29 | +
|
| 30 | +@app.command("/hello-bolt-python") |
| 31 | +async def command(ack, body, respond): |
| 32 | + await ack() |
| 33 | + await respond(f"Hi <@{body['user_id']}>!") |
| 34 | +
|
| 35 | +if __name__ == "__main__": |
| 36 | + app.start(3000) |
| 37 | +``` |
| 38 | +
|
| 39 | +If you want to use another async Web framework (e.g., Sanic, FastAPI, Starlette), take a look at the built-in adapters and their examples. |
| 40 | +
|
| 41 | +* [The Bolt app examples](https://github.com/slackapi/bolt-python/tree/main/examples) |
| 42 | +* [The built-in adapters](https://github.com/slackapi/bolt-python/tree/main/slack_bolt/adapter) |
| 43 | +Apps can be run the same way as the synchronous example above. If you'd prefer another async Web framework (e.g., Sanic, FastAPI, Starlette), take a look at [the built-in adapters](https://github.com/slackapi/bolt-python/tree/main/slack_bolt/adapter) and their corresponding [examples](https://github.com/slackapi/bolt-python/tree/main/examples). |
| 44 | +
|
| 45 | +Refer to `slack_bolt.app.async_app` for more details. |
| 46 | +""" |
1 | 47 | from .app.async_app import AsyncApp # noqa |
2 | 48 | from .context.ack.async_ack import AsyncAck # noqa |
3 | 49 | from .context.async_context import AsyncBoltContext # noqa |
|
0 commit comments