A Python framework to build Slack apps in a flash with the latest platform features. Read the getting started guide and look at our code examples to learn how to build apps using Bolt. The Python module documents are available here.
# Python 3.7+ required
python -m venv .venv
source .venv/bin/activate
pip install -U pip
pip install slack_boltCreate a Bolt for Python app by calling a constructor, which is a top-level export. If you'd prefer, you can create an async app.
import logging
logging.basicConfig(level=logging.DEBUG)
from slack_bolt import App
# export SLACK_SIGNING_SECRET=***
# export SLACK_BOT_TOKEN=xoxb-***
app = App()
# Add functionality here
if __name__ == "__main__":
app.start(3000) # POST http://localhost:3000/slack/eventsexport SLACK_SIGNING_SECRET=***
export SLACK_BOT_TOKEN=xoxb-***
python app.py
# in another terminal
ngrok http 3000If you use Socket Mode for running your app, SocketModeHandler is available for it.
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"])
# Add functionality here
if __name__ == "__main__":
# Create an app-level token with connections:write scope
handler = SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"])
handler.start()Run the app this way:
export SLACK_APP_TOKEN=xapp-***
export SLACK_BOT_TOKEN=xoxb-***
python app.py
# SLACK_SIGNING_SECRET is not required
# Running ngrok is not requiredApps typically react to a collection of incoming events, which can correspond to Events API events, actions, shortcuts, slash commands or options requests. For each type of request, there's a method to build a listener function.
# Listen for an action from a Block Kit element (buttons, select menus, date pickers, etc)
app.action(action_id)(fn)
# Listen for dialog submissions
app.action({"callback_id": callbackId})(fn)
# Listen for slash commands
app.command(command_name)(fn)
# Listen for an event from the Events API
app.event(event_type)(fn)
# Listen for a custom step execution from a workflow
app.function(callback_id)(fn)
# Convenience method to listen to only `message` events using a string or re.Pattern
app.message([pattern ,])(fn)
# Listen for options requests (from select menus with an external data source)
app.options(action_id)(fn)
# Listen for a global or message shortcuts
app.shortcut(callback_id)(fn)
# Listen for view_submission modal events
app.view(callback_id)(fn)The recommended way to use these methods are decorators:
@app.event(event_type)
def handle_event(event):
passMost of the app's functionality will be inside listener functions (the fn parameters above). These functions are called with a set of arguments, each of which can be used in any order. If you'd like to access arguments off of a single object, you can use args, an slack_bolt.kwargs_injection.Args instance that contains all available arguments for that event.
If you'd prefer to build your app with asyncio, you can import the AIOHTTP library and call the AsyncApp constructor. Within async apps, you can use the async/await pattern.
# Python 3.7+ required
python -m venv .venv
source .venv/bin/activate
pip install -U pip
# aiohttp is required
pip install slack_bolt aiohttpIn async apps, all middleware/listeners must be async functions. When calling utility methods (like ack and say) within these functions, it's required to use the await keyword.
# Import the async app instead of the regular one
from slack_bolt.async_app import AsyncApp
app = AsyncApp()
@app.event("app_mention")
async def event_test(body, say, logger):
logger.info(body)
await say("What's up?")
@app.command("/hello-bolt-python")
async def command(ack, body, respond):
await ack()
await respond(f"Hi <@{body['user_id']}>!")
if __name__ == "__main__":
app.start(3000)If you want to use another async Web framework (e.g., Sanic, FastAPI, Starlette), take a look at the built-in adapters and their examples.
- The Bolt app examples
- The built-in adapters Apps can be run the same way as the syncronous example above. If you'd prefer another async Web framework (e.g., Sanic, FastAPI, Starlette), take a look at the built-in adapters and their corresponding examples.
The documentation has more information on basic and advanced concepts for Bolt for Python. Also, all the Python module documents of this library are available here.
If you otherwise get stuck, we're here to help. The following are the best ways to get assistance working through your issue:
- Issue Tracker for questions, bug reports, feature requests, and general discussion related to Bolt for Python. Try searching for an existing issue before creating a new one.
- Email our developer support team:
support@slack.com
