The Bolt Framework for Python - a comprehensive framework for building Slack applications with decorator-based API for handling events, actions, and interactive components
—
Core application setup patterns for Slack Bolt, covering both single-workspace and multi-workspace configurations, OAuth flows, Socket Mode, and essential middleware configuration options.
Create a basic Bolt app for single-workspace development with hardcoded tokens.
class App:
def __init__(
self,
*,
logger=None,
name: str = None,
process_before_response: bool = False,
raise_error_for_unhandled_request: bool = False,
signing_secret: str = None,
token: str = None,
token_verification_enabled: bool = True,
client=None,
before_authorize=None,
authorize=None,
user_facing_authorize_error_message: str = None,
installation_store=None,
installation_store_bot_only: bool = True,
request_verification_enabled: bool = True,
ignoring_self_events_enabled: bool = True,
ignoring_self_assistant_message_events_enabled: bool = True,
ssl_check_enabled: bool = True,
url_verification_enabled: bool = True,
attaching_function_token_enabled: bool = True,
oauth_settings: OAuthSettings = None,
oauth_flow: OAuthFlow = None,
verification_token: str = None,
listener_executor=None,
assistant_thread_context_store=None
):
"""
Initialize a Bolt app instance.
Args:
token (str): Bot user OAuth access token (xoxb-...) or user token (xoxp-...)
signing_secret (str): Slack app signing secret for request verification
logger: Custom logger instance
name (str): App name for identification
process_before_response (bool): Enable FaaS mode for serverless deployment
raise_error_for_unhandled_request (bool): Whether to raise error for unmatched requests
client: Custom Slack WebClient instance
oauth_settings (OAuthSettings): OAuth configuration for multi-workspace apps
installation_store: Custom installation data store
assistant_thread_context_store: Store for AI assistant thread contexts
"""Asynchronous version of the main App class for high-performance applications.
class AsyncApp:
def __init__(self, **kwargs):
"""
Initialize an async Bolt app instance.
Args:
Same parameters as App class
"""Start the app with Bolt's built-in HTTP server for development and simple deployments.
def start(
self,
port: int = 3000,
path: str = "/slack/events",
host: str = "0.0.0.0"
):
"""
Start the built-in HTTP server.
Args:
port (int): Port number to listen on
path (str): URL path for Slack event handling
host (str): Host address to bind to
"""
async def start_async(
self,
port: int = 3000,
path: str = "/slack/events",
host: str = "0.0.0.0"
):
"""Async version of start() method."""Common environment variable patterns for configuration:
import os
from slack_bolt import App
app = App(
token=os.environ.get("SLACK_BOT_TOKEN"),
signing_secret=os.environ.get("SLACK_SIGNING_SECRET")
)Configure for serverless/FaaS deployment (AWS Lambda, Google Cloud Functions):
app = App(
token=os.environ.get("SLACK_BOT_TOKEN"),
signing_secret=os.environ.get("SLACK_SIGNING_SECRET"),
process_before_response=True # Enable FaaS mode
)Use a custom Slack WebClient with specific configuration:
from slack_sdk import WebClient
from slack_bolt import App
client = WebClient(
token=os.environ.get("SLACK_BOT_TOKEN"),
timeout=30,
retry_handlers=[RateLimitErrorRetryHandler(max_retry_count=2)]
)
app = App(
client=client,
signing_secret=os.environ.get("SLACK_SIGNING_SECRET")
)Control built-in middleware behavior:
app = App(
token=os.environ.get("SLACK_BOT_TOKEN"),
signing_secret=os.environ.get("SLACK_SIGNING_SECRET"),
# Middleware configuration
request_verification_enabled=True, # Verify request signatures
ignoring_self_events_enabled=True, # Ignore bot's own events
ssl_check_enabled=True, # Verify SSL certificates
token_verification_enabled=True # Verify token authenticity
)Configure Socket Mode for development and certain deployment scenarios:
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
app = App(
token=os.environ.get("SLACK_BOT_TOKEN"),
signing_secret=os.environ.get("SLACK_SIGNING_SECRET")
)
# Add event listeners...
if __name__ == "__main__":
handler = SocketModeHandler(
app=app,
app_token=os.environ.get("SLACK_APP_TOKEN") # xapp-... token
)
handler.start()Setup OAuth flow for multi-workspace app installation:
from slack_bolt import App
from slack_bolt.oauth import OAuthSettings
oauth_settings = OAuthSettings(
client_id=os.environ.get("SLACK_CLIENT_ID"),
client_secret=os.environ.get("SLACK_CLIENT_SECRET"),
scopes=["chat:write", "commands", "app_mentions:read"],
install_path="/slack/install",
redirect_uri_path="/slack/oauth_redirect",
success_url="https://your-domain.com/success",
failure_url="https://your-domain.com/failure"
)
app = App(
signing_secret=os.environ.get("SLACK_SIGNING_SECRET"),
oauth_settings=oauth_settings
)Implement custom authorization logic:
def custom_authorize(enterprise_id, team_id, user_id, logger):
# Custom authorization logic
# Return AuthorizeResult or None
return AuthorizeResult(
enterprise_id=enterprise_id,
team_id=team_id,
user_id=user_id,
bot_token="xoxb-...",
bot_user_id="U123456789"
)
app = App(
signing_secret=os.environ.get("SLACK_SIGNING_SECRET"),
authorize=custom_authorize
)Use custom installation store for multi-workspace apps:
from slack_bolt.oauth.oauth_settings import OAuthSettings
from slack_bolt.oauth.installation_store import FileInstallationStore
# File-based installation store
installation_store = FileInstallationStore(base_dir="./data/installations")
oauth_settings = OAuthSettings(
client_id=os.environ.get("SLACK_CLIENT_ID"),
client_secret=os.environ.get("SLACK_CLIENT_SECRET"),
scopes=["chat:write", "commands"],
)
app = App(
signing_secret=os.environ.get("SLACK_SIGNING_SECRET"),
oauth_settings=oauth_settings,
installation_store=installation_store
)Configure AI assistant features with thread context management:
from slack_bolt import App
from slack_bolt.context.assistant.thread_context_store import FileAssistantThreadContextStore
thread_context_store = FileAssistantThreadContextStore(
base_dir="./data/assistant_contexts"
)
app = App(
token=os.environ.get("SLACK_BOT_TOKEN"),
signing_secret=os.environ.get("SLACK_SIGNING_SECRET"),
assistant_thread_context_store=thread_context_store
)Setup custom logging for debugging and monitoring:
import logging
from slack_bolt import App
# Configure logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
app = App(
token=os.environ.get("SLACK_BOT_TOKEN"),
signing_secret=os.environ.get("SLACK_SIGNING_SECRET"),
logger=logger
)Simple development configuration with Socket Mode:
import os
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
app = App(
token=os.environ["SLACK_BOT_TOKEN"],
signing_secret=os.environ["SLACK_SIGNING_SECRET"]
)
@app.message("hello")
def say_hello(message, say):
say(f"Hi <@{message['user']}>!")
if __name__ == "__main__":
handler = SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"])
handler.start()Production-ready OAuth configuration with database storage:
import os
from slack_bolt import App
from slack_bolt.oauth import OAuthSettings
from your_app.stores import DatabaseInstallationStore
oauth_settings = OAuthSettings(
client_id=os.environ["SLACK_CLIENT_ID"],
client_secret=os.environ["SLACK_CLIENT_SECRET"],
scopes=["chat:write", "commands", "app_mentions:read"],
install_path="/slack/install",
redirect_uri_path="/slack/oauth_redirect"
)
app = App(
signing_secret=os.environ["SLACK_SIGNING_SECRET"],
oauth_settings=oauth_settings,
installation_store=DatabaseInstallationStore(),
process_before_response=True # For serverless deployment
)
# Add your event listeners here...Install with Tessl CLI
npx tessl i tessl/pypi-slack-bolt