Bolt is a foundational framework that makes it easier to build Slack apps with the platform's latest features. This guide walks you through building your first app with Bolt for Python.
Along the way, you'll create a new Slack app, set up a simple development environment, and build an app that listens and responds to events from your Slack workspace.
The first section of this guide covers creating a basic Slack app tailored to work with Bolt. While we glance over some configuration, our more general app setup guide goes into greater detail.
To get started, you'll need to create a new Slack app:
Fill out your App Name and select the Development Workspace where you'll play around and build your app. Don't fuss too much over either field—no matter what workspace you select, you'll still be able to distribute your app to other workspaces if you choose.
Scopes give your app permission to do things (for example, post messages) in your development workspace. You can select the scopes to add to your app by navigating over to the OAuth & Permissions sidebar or selecting your app below:
You currently have no apps. Try scopes out by creating a new Slack app:
Scroll down to the Bot Token Scopes section and click Add an OAuth Scope.
For now, we'll only use one scope. Add the chat:write
scope to grant your app the permission to post messages in channels it's a member of.
Install your own app by selecting the Install App button at the top of the OAuth & Permissions page, or from the sidebar.
After clicking through one more green Install App To Workspace button, you'll be sent through the Slack OAuth UI.
After installation, you'll land back in the OAuth & Permissions page and find a Bot User OAuth Access Token.
Access tokens are imbued with power. They represent the permissions delegated to your app by the installing user. Remember to keep your access token secret and safe, to avoid violating the trust of the installing user.
At a minimum, avoid checking your access token into public version control. Access it via an environment variable. We've also got plenty more best practices for app security.
This guide uses home tabs, a feature which offers a persistent space for your app to display dynamic interfaces for users.
To enable your app's home tab, click on the App Home sidebar on the App Management page, or select your app from the dropdown below:
With the app configured and installed, it’s time to set up a new Bolt for Python project with your app's credentials.
It's recommended to use a Python virtual environment to manage your project's dependencies.
Create your virtual environment:
python3 -m venv .venv
Then activate it:
source .venv/bin/activate
Before creating and starting the app, you'll need to copy over the credentials for your app. Copy the Bot User OAuth Access Token under the OAuth & Permissions sidebar (talked about in the installation section).
You'll just be using a local project in this guide, so you'll save your bot token as an environment variable. In the command line, you'll export your token as SLACK_BOT_TOKEN
:
export SLACK_BOT_TOKEN=xoxb-your-token
In addition to the access token, you'll need a signing secret. Your app's signing secret verifies that incoming requests are coming from Slack. Navigate to the Basic Information page from your app management page. Under App Credentials, copy the value for Signing Secret.
Like before, export your signing secret. This time as SLACK_SIGNING_SECRET
:
export SLACK_SIGNING_SECRET=your-signing-secret
To develop locally we'll be using ngrok, which allows you to expose a public endpoint that Slack can use to send your app events. If you haven't already, install ngrok from their website .
If you haven't used ngrok before, read our full tutorial for a more in-depth walkthrough on using ngrok to develop locally.
After you've installed ngrok, go ahead and tell ngrok to use port 3000 (which Bolt for Python uses by default):
ngrok http 3000
With your local environment configured, let's begin coding the app.
Your can install the slack_bolt
Python package to your virtual environment using the following command:
pip install slack_bolt
After you've installed the slack_bolt
package and added your app credentials, create a new app.py
file and paste the following:
import os
# Use the package we installed
from slack_bolt import App
# Initializes your app with your bot token and signing secret
app = App(
token=os.environ.get("SLACK_BOT_TOKEN"),
signing_secret=os.environ.get("SLACK_SIGNING_SECRET")
)
# Add functionality here
# @app.event("app_home_opened") etc
# Start your app
if __name__ == "__main__":
app.start(port=int(os.environ.get("PORT", 3000)))
The above code initializes the app using the App
constructor, then starts a simple HTTP server on port 3000. The HTTP server is using a built-in development adapter, which is responsible for handling and parsing incoming events from Slack. You can run your app now, but it won't do much.
python3 app.py
examples
folder
Your app can listen to all sorts of events happening around your workspace — messages being posted, reactjis being emoted, users joining the team, and more. To listen for events, your app uses the Events API.
Let's subscribe to the app_home_opened
event. On your app configuration page, select the Event Subscriptions sidebar. You'll be presented with an input box to enter a Request URL, which is where Slack sends the events your app is subscribed to. For local development, we'll use your ngrok URL from above.
For example:
https://1234abcde.ngrok.io
By default Bolt for Python listens for all incoming requests at the /slack/events
route, so for the Request URL you can enter your ngrok URL appended with /slack/events
.
For example:
https://1234abcde.ngrok.io/slack/events
After you've saved your Request URL, click on Subscribe to bot events, then Add Bot User Event and search for app_home_opened
. Then Save Changes using the the green button on the bottom right, and your app will start receiving app_home_opened
events as users navigate to your app from the Slack sidebar or search bar.
To respond to events with Bolt for Python, you can write a listener. Listeners have access to the event body, the entire request payload, and an additional context
object that holds helpful information like the bot token you used to instantiate your app.
Let's set up a basic listener using the app_home_opened
event that publishes a view to your Home tab, which is a persistent space where your app can display a dynamic interface for users.
Paste this listener code, which is using a Python decorator, into your existing Bolt app:
@app.event("app_home_opened")
def update_home_tab(client, event, logger):
try:
# views.publish is the method that your app uses to push a view to the Home tab
client.views_publish(
# the user that opened your app's app home
user_id=event["user"],
# the view object that appears in the app home
view={
"type": "home",
"callback_id": "home_view",
# body of the view
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Welcome to your _App's Home_* :tada:"
}
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "This button won't do much for now but you can set up a listener for it using the `actions()` method and passing its unique `action_id`. See an example in the `examples` folder within your Bolt app."
}
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "Click me!"
}
}
]
}
]
}
)
except Exception as e:
logger.error(f"Error publishing home tab: {e}")
Now, save your app.py
file and restart your Bolt app:
python3 app.py
Now try out your app by navigating to its app home by clicking on your app from the sidebar. When you open the Home, you should see your view appear. Congrats on building your first Bolt for Python app 🎆🙌
With the basics under your belt, you can start exploring the rest of Bolt and the entire Slack platform. Here's a few paths you may consider wandering:
Check out the examples
folder within the Bolt for Python repository. There are a collection of different examples for different adapters and platform features.
Improve your app's design with the Block Kit Builder. This is a prototyping tool that allows you to design Slack apps quickly, then paste the code into your Bolt app (we also have public Figma files if you'd rather use them to design your app).
Read the Bolt for Python documentation to learn about advanced functionality and find example code snippets that show you what else is possible.
Explore other surfaces where your app can exist, like messages and pop-up modals.
And just a reminder: when you're ready to take your app from development to deployed, you'll want to host it on another platform.