AI Chatbot
In this tutorial, you'll learn how to bring the power of AI into your Slack workspace using a chatbot called Bolty that uses Anthropic or OpenAI. Here's what we'll do with this sample app:
- Create your app from an app manifest and clone a starter template
- Set up and run your local project
- Create a workflow using Workflow Builder to summarize messages in conversations
- Select your preferred API and model to customize Bolty's responses
- Interact with Bolty via direct message, the
/ask-bolty
slash command, or by mentioning the app in conversations
Prerequisites
Before getting started, you will need the following:
- a development workspace where you have permissions to install apps. If you don’t have a workspace, go ahead and set that up now—you can go here to create one, or you can join the Developer Program and provision a sandbox with access to all Slack features for free.
- a development environment with Python 3.6 or later.
- an Anthropic or OpenAI account with sufficient credits, and in which you have generated a secret key.
Skip to the code If you'd rather skip the tutorial and just head straight to the code, you can use our Bolt for Python AI Chatbot sample as a template.
Creating your app
- Navigate to the app creation page and select From a manifest.
- Select the workspace you want to install the application in.
- Copy the contents of the
manifest.json
file into the text box that says Paste your manifest code here (within the JSON tab) and click Next. - Review the configuration and click Create.
- You're now in your app configuration's Basic Information page. Navigate to the Install App link in the left nav and click *Install to Workspace, then Allow on the screen that follows.
Obtaining and storing your environment variables
Before you'll be able to successfully run the app, you'll need to first obtain and set some environment variables.
- On the Install App page, copy your Bot User OAuth Token. You will store this in your environment as
SLACK_BOT_TOKEN
(we'll get to that next). - Navigate to Basic Information and in the App-Level Tokens section , click Generate Token and Scopes. Add the
connections:write
scope, name the token, and click Generate. (For more details, refer to understanding OAuth scopes for bots). Copy this token. You will store this in your environment asSLACK_APP_TOKEN
.
To store your tokens and environment variables, run the following commands in the terminal. Replace the placeholder values with your bot and app tokens collected above, as well as the key or keys for the AI provider or providers you want to use:
For macOS
export SLACK_BOT_TOKEN=<your-bot-token>
export SLACK_APP_TOKEN=<your-app-token>
export OPENAI_API_KEY=<your-api-key>
export ANTHROPIC_API_KEY=<your-api-key>
For Windows
set SLACK_BOT_TOKEN=<your-bot-token>
set SLACK_APP_TOKEN=<your-app-token>
set OPENAI_API_KEY=<your-api-key>
set ANTHROPIC_API_KEY=<your-api-key>
Setting up and running your local project
Clone the starter template onto your machine by running the following command:
git clone https://github.com/slack-samples/bolt-python-ai-chatbot.git
Change into the new project directory:
cd bolt-python-ai-chatbot
Start your Python virtual environment:
For macOS
python3 -m venv .venv
source .venv/bin/activate
For Windows
py -m venv .venv
.venv\Scripts\activate
Install the required dependencies:
pip install -r requirements.txt
Start your local server:
python app.py
If your app is up and running, you'll see a message that says "⚡️ Bolt app is running!"
Choosing your provider
Navigate to the Bolty App Home and select a provider from the drop-down menu. The options listed will be dependent on which secret keys you added when setting your environment variables.
If you don't see Bolty listed under Apps in your workspace right away, never fear! You can mention @Bolty in a public channel to add the app, then navigate to your App Home.