The Bolt Framework for Python - a comprehensive framework for building Slack applications with decorator-based API for handling events, actions, and interactive components
npx @tessl/cli install tessl/pypi-slack-bolt@1.24.0The Bolt Framework for Python is a comprehensive library for building Slack applications with modern, decorator-based APIs. It provides developers with tools for handling Slack events, actions, shortcuts, slash commands, and interactive components, supporting both synchronous and asynchronous programming models.
pip install slack-boltfrom slack_bolt import AppFor asynchronous applications:
from slack_bolt.async_app import AsyncAppImport additional utilities as needed:
from slack_bolt import BoltContext, Args
from slack_bolt.oauth.oauth_flow import OAuthFlow
from slack_bolt.oauth.oauth_settings import OAuthSettings
from slack_bolt.middleware.custom_middleware import CustomMiddlewarefrom slack_bolt import App
# Initialize app with bot token and signing secret
app = App(
token="xoxb-your-bot-token",
signing_secret="your-signing-secret"
)
# Listen for message events containing "hello"
@app.message("hello")
def say_hello(message, say):
say(f"Hello <@{message['user']}>!")
# Listen for slash commands
@app.command("/weather")
def weather_command(ack, respond, command):
ack() # Always acknowledge commands immediately
city = command['text'] or "San Francisco"
respond(f"The weather in {city} is sunny! ☀️")
# Listen for button clicks
@app.action("approve_button")
def handle_approve(ack, respond, body):
ack()
user = body["user"]["name"]
respond(f"<@{user}> approved the request!")
# Start the app
if __name__ == "__main__":
app.start(port=int(os.environ.get("PORT", 3000)))from slack_bolt import App
from slack_bolt.oauth.oauth_flow import OAuthFlow
from slack_bolt.oauth.oauth_settings import OAuthSettings
# Configure OAuth flow for multi-workspace installation
oauth_settings = OAuthSettings(
client_id="your-client-id",
client_secret="your-client-secret",
scopes=["chat:write", "commands", "app_mentions:read"],
install_path="/slack/install",
redirect_uri_path="/slack/oauth_redirect"
)
app = App(
signing_secret="your-signing-secret",
oauth_settings=oauth_settings
)
@app.event("app_mention")
def handle_mention(event, say):
say(f"Thanks for mentioning me, <@{event['user']}>!")
app.start(port=3000)Slack Bolt follows an event-driven architecture with several key components:
ack(), say(), respond(), and client for API callsThe framework supports both single-workspace apps (with hardcoded tokens) and multi-workspace apps (with OAuth installation flows), and provides both synchronous and asynchronous programming models.
Core application setup including single vs multi-workspace configuration, OAuth flows, Socket Mode, and essential middleware options. Covers both App and AsyncApp initialization patterns.
class App:
def __init__(
self,
*,
logger=None,
name: str = None,
process_before_response: bool = False,
signing_secret: str = None,
token: str = None,
client=None,
authorize=None,
installation_store=None,
oauth_settings: OAuthSettings = None,
oauth_flow: OAuthFlow = None,
# ... many more configuration options
): ...
def start(self, port: int = 3000, path: str = "/slack/events", host: str = "0.0.0.0"): ...Decorator-based event handling for all Slack event types including messages, slash commands, interactive components, shortcuts, and view submissions. Supports event filtering and custom matchers.
def message(self, keyword=None, *matchers): ...
def command(self, command, *matchers): ...
def event(self, event_type, *matchers): ...
def action(self, action_id, *matchers): ...
def shortcut(self, callback_id, *matchers): ...
def view(self, callback_id, *matchers): ...
def options(self, action_id, *matchers): ...Event Listeners & Request Handling
Rich context objects providing utilities for acknowledging requests, sending messages, making API calls, and accessing request data. Includes argument injection system for clean listener functions.
class BoltContext:
client: WebClient
ack: Ack
say: Say
respond: Respond
logger: Logger
# ... additional properties
def ack(response=None): ...
def say(text: str = None, **kwargs): ...
def respond(text: str = None, **kwargs): ...OAuth 2.0 flow implementation for multi-workspace Slack app installation and management. Includes installation stores, token management, and authorization patterns.
class OAuthFlow:
def __init__(self, *, settings: OAuthSettings, logger: Logger = None): ...
class OAuthSettings:
def __init__(
self,
*,
client_id: str,
client_secret: str,
scopes: Union[str, Sequence[str]],
redirect_uri_path: str = "/slack/oauth_redirect",
install_path: str = "/slack/install"
): ...OAuth & Multi-Workspace Installation
Request processing pipeline with built-in middleware for authentication, request verification, and logging. Supports custom middleware development and per-listener middleware.
def use(self, *middleware): ...
def middleware(self, *middleware): ...
def error(self, error_handler): ...
class CustomMiddleware:
def __init__(self, app_name: str = None): ...
def process(self, *, req: BoltRequest, resp: BoltResponse, next): ...Handling of Slack's interactive UI components including Block Kit elements, modal views, shortcuts, and form submissions. Covers view management and complex UI workflows.
def view_submission(self, callback_id, *matchers): ...
def view_closed(self, callback_id, *matchers): ...
def block_action(self, action_id, *matchers): ...
def global_shortcut(self, callback_id, *matchers): ...
def message_shortcut(self, callback_id, *matchers): ...Event Listeners & Request Handling
Adapters for integrating Bolt apps with popular Python web frameworks and deployment platforms. Supports FastAPI, Flask, Django, ASGI/WSGI, and cloud platforms.
# FastAPI example
from slack_bolt.adapter.fastapi import SlackRequestHandler
handler = SlackRequestHandler(app)
fastapi_app.post("/slack/events")(handler.handle)
# Flask example
from slack_bolt.adapter.flask import SlackRequestHandler
handler = SlackRequestHandler(app)
flask_app.route("/slack/events", methods=["POST"])(handler.handle)AI assistant middleware for building conversational AI experiences within Slack. Includes thread context management, assistant-specific utilities, and workflow integration.
from slack_bolt.middleware.assistant import Assistant
class Assistant:
def __init__(self, thread_context_store=None): ...
def set_status(text: str): ...
def set_title(title: str): ...
def set_suggested_prompts(prompts: List[str]): ...
def save_thread_context(context: dict): ...AsyncApp for high-performance asynchronous Slack applications. Includes async decorators, utilities, and integration patterns for async web frameworks.
class AsyncApp:
def __init__(self, **kwargs): ...
async def start_async(self, port: int = 3000): ...
def message(self, keyword=None, *matchers): ... # Returns async decorator
def command(self, command, *matchers): ... # Returns async decoratorclass BoltError(Exception): ...
class BoltUnhandledRequestError(BoltError):
request: BoltRequest
body: dict
current_response: BoltResponse
last_global_middleware_name: strCommon error patterns and debugging techniques are covered in the middleware and context documentation.
__version__ = "1.24.0"