docs/english/tutorial/ai-chatbot/ai-chatbot.md
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.
With Bolty, users can:
/ask-bolty slash command to ask Bolty questions, andIntrigued? First, grab your tools by following the three steps below.
import QuickstartGuide from '@site/src/components/QuickstartGuide';
<QuickstartGuide steps={[ { number: 1, title: 'Install the Slack CLI', description: 'Download the command-line tool for developing Slack apps.', commands: { macos: 'curl -fsSL https://downloads.slack-edge.com/slack-cli/install.sh | bash', windows: 'irm https://downloads.slack-edge.com/slack-cli/install-windows.ps1 | iex' } }, { number: 2, title: 'Connect to your workspace', description: 'Log in and authenticate with your Slack workspace.', commands: { macos: 'slack login', windows: 'slack login' } }, { number: 3, title: 'Create a project', description: 'Set up a new Bolt project from a starter template.', commands: { macos: 'slack create ai-chatbot --template slack-samples/bolt-python-ai-chatbot', windows: 'slack create ai-chatbot --template slack-samples/bolt-python-ai-chatbot' } } ]} buttonText="View sample app" buttonLink="https://github.com/slack-samples/bolt-python-ai-chatbot" buttonIcon={true} />
You will also need the following:
Before you'll be able to successfully run the app, you'll need to first obtain and set some environment variables.
Models from different AI providers are available if the corresponding environment variable is added as shown in the sections below.
<Tabs groupId="ai-providers"> <TabItem value="anthropic" label="Anthropic">To interact with Anthropic models, navigate to your Anthropic account dashboard to create an API key, then export the key as follows:
export ANTHROPIC_API_KEY=<your-api-key>
To use Google Cloud Vertex AI, follow this quick start to create a project for sending requests to the Gemini API, then gather Application Default Credentials with the strategy to match your development environment.
Once your project and credentials are configured, export environment variables to select from Gemini models:
export VERTEX_AI_PROJECT_ID=<your-project-id>
export VERTEX_AI_LOCATION=<location-to-deploy-model>
The project location can be located under the Region on the Vertex AI dashboard, as well as more details about available Gemini models.
</TabItem> <TabItem value="OpenAI" label="OpenAI">Unlock the OpenAI models from your OpenAI account dashboard by clicking create a new secret key, then export the key like so:
export OPENAI_API_KEY=<your-api-key>
Start your Python virtual environment:
<Tabs groupId="os"> <TabItem value="macos" label="macOS">python3 -m venv .venv
source .venv/bin/activate
py -m venv .venv
.venv\Scripts\activate
Install the required dependencies:
pip install -r requirements.txt
Run your app locally:
slack run
If your app is indeed up and running, you'll see a message that says "⚡️ Bolt app is running!"
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.
Within your development workspace, open Workflow Builder by clicking on your workspace name and then Tools > Workflow Builder. Select New Workflow > Build Workflow.
Click Untitled Workflow at the top to rename your workflow. For this tutorial, we'll call the workflow Welcome to the channel. Enter a description, such as Summarizes channels for new members, and click Save.
Select Choose an event under Start the workflow..., and then choose When a person joins a channel. Select the channel name from the drop-down menu and click Save.
Under Then, do these things, click Add steps and complete the following:
{}The channel that the user joined. Would you like a summary of the recent conversation? Note that the {}The channel that the user joined is a variable; you can insert it by selecting {}Insert a variable at the bottom of the message text box.We'll add two more steps under the Then, do these things section.
First, scroll to the bottom of the list of steps and choose Custom, then choose Bolty and Bolty Custom Function. In the Channel drop-down menu, select Channel that the user joined. Click Save.
For the final step, complete the following:
{}Summary under the Bolty Custom Function section in the list that appears. Click Save.When finished, click Finish Up, then click Publish to make the workflow available in your workspace.
In order for Bolty to provide summaries of recent conversation in a channel, Bolty must be a member of that channel.
To test this, leave the channel you just invited Bolty to and rejoin it. This will kick off your workflow and you'll receive a direct message from Welcome to the channel. Click the Yes, give me a summary button, and Bolty will summarize the recent conversations in the channel you joined.
The central part of this functionality is shown in the following code snippet. Note the use of the user_context object, a Slack type that represents the user who is interacting with our workflow, as well as the history of the channel that will be summarized, which includes the ten most recent messages.
from ai.providers import get_provider_response
from logging import Logger
from slack_bolt import Complete, Fail, Ack
from slack_sdk import WebClient
from ..listener_utils.listener_constants import SUMMARIZE_CHANNEL_WORKFLOW
from ..listener_utils.parse_conversation import parse_conversation
"""
Handles the event to summarize a Slack channel's conversation history.
It retrieves the conversation history, parses it, generates a summary using an AI response,
and completes the workflow with the summary or fails if an error occurs.
"""
def handle_summary_function_callback(
ack: Ack, inputs: dict, fail: Fail, logger: Logger, client: WebClient, complete: Complete
):
ack()
try:
user_context = inputs["user_context"]
channel_id = inputs["channel_id"]
history = client.conversations_history(channel=channel_id, limit=10)["messages"]
conversation = parse_conversation(history)
summary = get_provider_response(user_context["id"], SUMMARIZE_CHANNEL_WORKFLOW, conversation)
complete({"user_context": user_context, "response": summary})
except Exception as e:
logger.exception(e)
fail(e)
To ask Bolty a question, you can chat with Bolty in any channel the app is in. Use the \ask-bolty slash command to provide a prompt for Bolty to answer. Note that Bolty is currently not supported in threads.
You can also navigate to Bolty in your Apps list and select the Messages tab to chat with Bolty directly.
Congratulations! You've successfully integrated the power of AI into your workspace. Check out these links to take the next steps in your Bolt for Python journey.