Custom Steps for Workflow Builder (new app)
If you don't have a paid workspace for development, you can join the Developer Program and provision a sandbox with access to all Slack features for free.
Adding a workflow step to your app and implementing a corresponding function listener is how you define a custom Workflow Builder step. In this tutorial, you'll use Bolt for Python to add your workflow step, then wire it up in Workflow Builder.
When finished, you'll be ready to build scalable and innovative workflow steps for anyone using Workflow Builder in your workspace.
What are we building?
In this tutorial, you'll be wiring up a sample app with a sample step and corresponding function listener to be used as a workflow step in Workflow Builder. Here's how it works:
- When someone starts the workflow, Slack will notify your app that your custom step was invoked as part of a workflow.
- Your app will send a message to the requestor, along with a button to complete the step.
- When the user clicks or taps the button, Slack will let your app know, and your app will respond by changing the message.
If you'd rather skip the tutorial and just head straight to the code, create a new app and use our Bolt Python custom step sample as a template. The sample custom step provided in the template will be a good place to start exploring!
Prerequisites
Before we begin, let's make sure you're set up for success. Ensure you have a development workspace where you have permission to install apps. We recommend setting up your own space used for exploration and testing in a developer sandbox.
Cloning the sample project
For this tutorial, We'll use boltstep
as the app name. For your app, be sure to use a unique name that will be easy for you to find: then, use that name wherever you see boltstep
in this tutorial. The app will be named "Bolt Custom Step", as that is defined in the manifest.json
file of the sample app code.
Let's start by opening a terminal and cloning the starter template repository:
git clone https://github.com/slack-samples/bolt-python-custom-step-template boltstep
Once the terminal is finished cloning the template, change directories into your newly prepared app project:
cd boltstep
If you're using VSCode (highly recommended), you can enter code .
from your project's directory and VSCode will open your new project.
You can also open a terminal window from inside VSCode like this: Ctrl
+ ~
Once in VSCode, open the terminal. Let's install our package dependencies: run the following command(s) in the terminal inside VSCode:
npm install
We now have a Bolt app ready for development! Open the manifest.json
file and copy its contents; you'll need this in the next step.
Creating your app from a manifest
Open a browser and navigate to your apps page. This is where we will create a new app with our previously copied manifest details. Click the Create New App button, then select From an app manifest when prompted to choose how you'd like to configure your app's settings.
Next, select a workspace where you have permissions to install apps, and click Next. Select the JSON tab and clear the existing contents. Paste the contents of the manifest.json
file you previously copied.
Click Next again. You will be shown a brief overview of the features your app includes. You'll see we are creating an app with a chat:write
bot scope, an App Home and Bot User, as well as Socket Mode, Interactivity, an Event Subscription, and Org Deploy. We'll get into these details later. Click Create.
App settings
All of your app's settings can be configured within these screens. By creating an app from an existing manifest, you will notice many settings have already been configured. Navigate to Org Level Apps and notice that we've already opted in. This is a requirement for adding workflow steps to an app.
Navigate to Event Subscriptions and expand Subscribe to bot events to see that we have subscribed to the function_executed
event. This is also a requirement for adding workflow steps to our app, as it lets our app know when a step has been triggered, allowing our app to respond to it.
Another configuration setting to note is Socket Mode. We have turned this on for our local development, but socket mode is not intended for use in a production environment. When you are satisfied with your app and ready to deploy it to a production environment, you should switch to using public HTTP request URLs. Read more about getting started with HTTP in Bolt for Python here.
Clicking on Workflow Steps in the left nav will show you that one workflow step has been added! This reflects the function
defined in our manifest: functions are workflow steps. We will get to this step's implementation later.
Tokens
In order to connect our app here with the logic of our sample code set up locally, we need to obtain two tokens, a bot token and an app token.
- Bot tokens are associated with bot users, and are only granted once in a workspace where someone installs the app. The bot token your app uses will be the same no matter which user performed the installation. Bot tokens are the token type that most apps use.
- App-level tokens represent your app across organizations, including installations by all individual users on all workspaces in a given organization and are commonly used for creating websocket connections to your app.
To generate an app token, navigate to Basic Information and scroll down to App-Level Token.
Click Generate Token and Scopes, then Add Scope and choose connections:write
. Choose a name for your token and click Generate. Copy that value, save it somewhere accessible, and click Done to close out of the modal.
Next up is the bot token. We can only get this token by installing the app into the workspace. Navigate to Install App and click the button to install, choosing Allow at the next screen.
You will then have a bot token. Again, copy that value and save it somewhere accessible.
💡 Treat your tokens like passwords and keep them safe. Your app uses them to post and retrieve information from Slack workspaces. Minimally, do NOT commit them to version control.
Starting your local development server
While building your app, you can see your changes appear in your workspace in real-time with npm start
. Soon we'll start our local development server and see what our sample code is all about! But first, we need to store those tokens we gathered as environment variables.
Navigate back to VSCode. Rename the .env.sample
file to .env
. Open this file and update SLACK_APP_TOKEN
and SLACK_BOT_TOKEN
with the values you previously saved. It will look like this, with your actual token values where you see <your_app_token>
and <your_bot_token>
:
SLACK_APP_TOKEN=<your_app_token>
SLACK_BOT_TOKEN=<your_bot_token>
Now save the file and try starting your app:
npm start
You'll know the local development server is up and running successfully when it emits a bunch of [DEBUG]
statements to your terminal, the last one containing connected:ready
.
With your development server running, continue to the next step.
If you need to stop running the local development server, press <CTRL>
+ c
to end the process.