github slackapi/bolt-python v1.2.0
version 1.2.0

latest releases: v1.19.0rc1, v1.18.1, v1.18.0.dev1...
3 years ago

New Features

Socket Mode

This version includes support for Socket Mode, which enables developers to receive interactivy payalods and events through WebSocket connections.

https://api.slack.com/socket-mode

For WebSocket connection handling, there are four implementations including major 3rd party open-source libraries.

PyPI Project Bolt Adapter
skack_sdk slack_bolt.adapter.socket_mode.SocketModeHandler
websocket_client slack_bolt.adapter.socket_mode.websocket_client.SocketModeHandler
aiohttp (asyncio-based) slack_bolt.adapter.socket_mode.aiohttp.AsyncSocketModeHandler
websockets (asyncio-based) slack_bolt.adapter.socket_mode.websockets.AsyncSocketModeHandler

Here is a minimal working example with the built-in WebSocket client. You can switch to other implementation by changing the imports and adding the extra dependencies (websocket_client, aiohttp, websockets).

import os
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler

# Install the Slack app and get xoxb- token in advance
app = App(token=os.environ["SLACK_BOT_TOKEN"])

if __name__ == "__main__":
    # export SLACK_APP_TOKEN=xapp-***
    # export SLACK_BOT_TOKEN=xoxb-***
    SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"]).start()

If you want to use asyncio for everything, you can use aiohttp or websockets (along with aiohttp for AsyncWebClient). AsyncSocketModeHandler requires all of your middleware/listeners to be compatible with the async/await programming style.

from slack_bolt.app.async_app import AsyncApp
# The default is the aiohttp based implementation
from slack_bolt.adapter.socket_mode.async_handler import AsyncSocketModeHandler

app = AsyncApp(token=os.environ["SLACK_BOT_TOKEN"])

async def main():
    handler = AsyncSocketModeHandler(app, os.environ["SLACK_APP_TOKEN"])
    await handler.start_async()

if __name__ == "__main__":
    import asyncio
    asyncio.run(main())

Changes

References

Don't miss a new bolt-python release

NewReleases is sending notifications on new releases.