|
| 1 | +--- |
| 2 | +title: Using Socket Mode |
| 3 | +lang: en |
| 4 | +slug: socket-mode |
| 5 | +order: 16 |
| 6 | +--- |
| 7 | + |
| 8 | +<div class="section-content"> |
| 9 | +With the introduction of [Socket Mode](https://api.slack.com/socket-mode), Bolt for Python introduced support in version `1.2.0`. With Socket Mode, instead of creating a server with endpoints that Slack sends payloads too, the app will instead connect to Slack via a WebSocket connection and receive data from Slack over the socket connection. Make sure to enable Socket Mode in your app configuration settings. |
| 10 | + |
| 11 | +To use the Socket Mode, add `SLACK_APP_TOKEN` as an environment variable. You can get your App Token in your app configuration settings under the **Basic Information** section. |
| 12 | +</div> |
| 13 | + |
| 14 | +```python |
| 15 | +import os |
| 16 | +from slack_bolt import App |
| 17 | +from slack_bolt.adapter.socket_mode import SocketModeHandler |
| 18 | + |
| 19 | +# Install the Slack app and get xoxb- token in advance |
| 20 | +app = App(token=os.environ["SLACK_BOT_TOKEN"]) |
| 21 | + |
| 22 | +if __name__ == "__main__": |
| 23 | + # export SLACK_APP_TOKEN=xapp-*** |
| 24 | + # export SLACK_BOT_TOKEN=xoxb-*** |
| 25 | + SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"]).start() |
| 26 | +``` |
| 27 | + |
| 28 | +While we recommend using [the built-in Socket Mode adapter](https://github.com/slackapi/bolt-python/tree/main/slack_bolt/adapter/socket_mode/builtin), there are a few other 3rd party library based implementations. Here is the list of available adapters. |
| 29 | + |
| 30 | +|PyPI Project|Bolt Adapter| |
| 31 | +|-|-| |
| 32 | +|[slack_sdk](https://pypi.org/project/slack-sdk/)|[slack_bolt.adapter.socket_mode](https://github.com/slackapi/bolt-python/tree/main/slack_bolt/adapter/socket_mode/builtin)| |
| 33 | +|[websocket_client](https://pypi.org/project/websocket_client/)|[slack_bolt.adapter.socket_mode.websocket_client](https://github.com/slackapi/bolt-python/tree/main/slack_bolt/adapter/socket_mode/websocket_client)| |
| 34 | +|[aiohttp](https://pypi.org/project/aiohttp/) (asyncio-based)|[slack_bolt.adapter.socket_mode.aiohttp](https://github.com/slackapi/bolt-python/tree/main/slack_bolt/adapter/socket_mode/aiohttp)| |
| 35 | +|[websockets](https://pypi.org/project/websockets/) (asyncio-based)|[slack_bolt.adapter.socket_mode.websockets](https://github.com/slackapi/bolt-python/tree/main/slack_bolt/adapter/socket_mode/websockets)| |
| 36 | + |
| 37 | +To use the asyncio-based adapters such as aiohttp, your app needs to be compatible with asyncio's async/await programming model. `AsyncSocketModeHandler` is available for running `AsyncApp` and its async middleware and listeners. |
| 38 | + |
| 39 | +```python |
| 40 | +from slack_bolt.app.async_app import AsyncApp |
| 41 | +# The default is the aiohttp based implementation |
| 42 | +from slack_bolt.adapter.socket_mode.async_handler import AsyncSocketModeHandler |
| 43 | + |
| 44 | +app = AsyncApp(token=os.environ["SLACK_BOT_TOKEN"]) |
| 45 | + |
| 46 | +async def main(): |
| 47 | + handler = AsyncSocketModeHandler(app, os.environ["SLACK_APP_TOKEN"]) |
| 48 | + await handler.start_async() |
| 49 | + |
| 50 | +if __name__ == "__main__": |
| 51 | + import asyncio |
| 52 | + asyncio.run(main()) |
| 53 | +``` |
| 54 | + |
| 55 | +To learn how to use `AsyncApp`, checkout the [Using Async](https://slack.dev/bolt-python/concepts#async) document and relevant [examples](https://github.com/slackapi/bolt-python/tree/main/examples). |
0 commit comments