Slack Bolt for Python
Slack Bolt for Python is a framework that makes it easy to build Slack apps using the Slack Platform. It simplifies common patterns like handling events, processing interactive components, and sending messages. The library is actively maintained with frequent releases, typically monthly, and is currently at version 1.28.0.
Warnings
- breaking Python 3.6 support has been officially dropped. Applications running on Python 3.6 will need to upgrade to Python 3.7 or newer.
- gotcha Deployment strategies differ significantly for local development versus production. `App.start()` or `SocketModeHandler` is for local Socket Mode development. For production, you must use an HTTP adapter (e.g., `FlaskResponder`, `AwsLambdaPythonReceiver`) and expose an endpoint for Slack to send events.
- gotcha All Slack apps require careful configuration within api.slack.com/apps, including setting correct OAuth Scopes, enabling Event Subscriptions, and verifying the Slack Signing Secret. Many errors (e.g., 401 Unauthorized, events not being received) stem from misconfigurations or missing environment variables.
- gotcha New AI-enabled features like `say_stream` and `set_status` (for streaming messages and showing loading states) introduced in v1.26.0 and v1.28.0 require specific Bolt versions and may depend on the latest Slack platform features. If these methods are not available or behave unexpectedly, check your Bolt version and Slack app setup.
Install
-
pip install slack-bolt
Imports
- App
from slack_bolt import App
- AsyncApp
from slack_bolt.async_app import AsyncApp
- SocketModeHandler
from slack_bolt.adapter.socket_mode import SocketModeHandler
- FlaskResponder
from slack_bolt.adapter.flask import FlaskResponder
Quickstart
import os
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
# Initialize your app with your bot token and signing secret
# Ensure SLACK_BOT_TOKEN, SLACK_SIGNING_SECRET, and SLACK_APP_TOKEN are set in your environment
app = App(
token=os.environ.get("SLACK_BOT_TOKEN", ""),
signing_secret=os.environ.get("SLACK_SIGNING_SECRET", "")
)
# Listen for messages containing "hello"
@app.message("hello")
def message_hello(message, say):
# say() sends a message to the same channel where the event was triggered
say(f"Hey there <@{message['user']}>!")
# Start your app
# For local development, Socket Mode is often used.
# For production, you'd typically use an HTTP adapter (e.g., Flask, AWS Lambda).
if __name__ == "__main__":
# SLACK_APP_TOKEN is required for Socket Mode
SocketModeHandler(app, os.environ.get("SLACK_APP_TOKEN", "")).start()