Skip to content

Commit 64eedee

Browse files
Docs: AI chatbot tutorial. (#1141)
* tutorial content * link update * update name
1 parent cee9455 commit 64eedee

File tree

10 files changed

+205
-1
lines changed

10 files changed

+205
-1
lines changed
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
# AI Chatbot
2+
3+
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:
4+
5+
1. Create your app from an app manifest and clone a starter template
6+
2. Set up and run your local project
7+
3. Create a workflow using Workflow Builder to summarize messages in conversations
8+
4. Select your preferred API and model to customize Bolty's responses
9+
5. Interact with Bolty via direct message, the `/ask-bolty` slash command, or by mentioning the app in conversations
10+
11+
## Prerequisites {#prereqs}
12+
13+
Before getting started, you will need the following:
14+
15+
* 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](https://slack.com/get-started#create) to create one, or you can join the [Developer Program](https://api.slack.com/developer-program) and provision a sandbox with access to all Slack features for free.
16+
* a development environment with [Python 3.6](https://www.python.org/downloads/) or later.
17+
* an Anthropic or OpenAI account with sufficient credits, and in which you have generated a secret key.
18+
19+
**Skip to the code**
20+
If you'd rather skip the tutorial and just head straight to the code, you can use our [Bolt for Python AI Chatbot sample](https://github.com/slack-samples/bolt-python-ai-chatbot) as a template.
21+
22+
## Creating your app {#create-app}
23+
24+
1. Navigate to the [app creation page](https://api.slack.com/apps/new) and select **From a manifest**.
25+
2. Select the workspace you want to install the application in.
26+
3. Copy the contents of the [`manifest.json`](https://github.com/slack-samples/bolt-python-ai-chatbot/blob/main/manifest.json) file into the text box that says **Paste your manifest code here** (within the **JSON** tab) and click **Next**.
27+
4. Review the configuration and click **Create**.
28+
5. 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.
29+
30+
### Obtaining and storing your environment variables {#environment-variables}
31+
32+
Before you'll be able to successfully run the app, you'll need to first obtain and set some environment variables.
33+
34+
1. 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).
35+
2. Navigate to **Basic Information** and in the **App-Level Tokens** section , click **Generate Token and Scopes**. Add the [`connections:write`](https://api.slack.com/scopes/connections:write) scope, name the token, and click **Generate**. (For more details, refer to [understanding OAuth scopes for bots](https://api.slack.com/tutorials/tracks/understanding-oauth-scopes-bot)). Copy this token. You will store this in your environment as `SLACK_APP_TOKEN`.
36+
37+
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:
38+
39+
**For macOS**
40+
```bash
41+
export SLACK_BOT_TOKEN=<your-bot-token>
42+
export SLACK_APP_TOKEN=<your-app-token>
43+
export OPENAI_API_KEY=<your-api-key>
44+
export ANTHROPIC_API_KEY=<your-api-key>
45+
```
46+
47+
**For Windows**
48+
```bash
49+
set SLACK_BOT_TOKEN=<your-bot-token>
50+
set SLACK_APP_TOKEN=<your-app-token>
51+
set OPENAI_API_KEY=<your-api-key>
52+
set ANTHROPIC_API_KEY=<your-api-key>
53+
```
54+
55+
## Setting up and running your local project {#configure-project}
56+
57+
Clone the starter template onto your machine by running the following command:
58+
59+
```bash
60+
git clone https://github.com/slack-samples/bolt-python-ai-chatbot.git
61+
```
62+
63+
Change into the new project directory:
64+
65+
```bash
66+
cd bolt-python-ai-chatbot
67+
```
68+
69+
Start your Python virtual environment:
70+
71+
**For macOS**
72+
```bash
73+
python3 -m venv .venv
74+
source .venv/bin/activate
75+
```
76+
77+
**For Windows**
78+
```bash
79+
py -m venv .venv
80+
.venv\Scripts\activate
81+
```
82+
83+
Install the required dependencies:
84+
85+
```bash
86+
pip install -r requirements.txt
87+
```
88+
89+
Start your local server:
90+
91+
```bash
92+
python app.py
93+
```
94+
95+
If your app is up and running, you'll see a message that says "⚡️ Bolt app is running!"
96+
97+
## Choosing your provider {#provider}
98+
99+
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.
100+
101+
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**.
102+
103+
![Choose your AI provider](/img/ai-chatbot/6.png)
104+
105+
## Setting up your workflow {#workflow}
106+
107+
Within your development workspace, open Workflow Builder by clicking on your workspace name and then **Tools > Workflow Builder**. Select **New Workflow** > **Build Workflow**.
108+
109+
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**.
110+
111+
![Setting up a new workflow](/img/ai-chatbot/1.png)
112+
113+
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**.
114+
115+
![Start the workflow](/img/ai-chatbot/2.png)
116+
117+
Under **Then, do these things**, click **Add steps** and complete the following:
118+
119+
1. Select **Messages** > **Send a message to a person**.
120+
2. Under **Select a member**, choose **The user who joined the channel** from the drop-down menu.
121+
3. Under **Add a message**, enter a short message, such as _Hi! Welcome to `{}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.
122+
4. Select the **Add Button** button, and name the button _Yes, give me a summary_. Click **Done**.
123+
124+
![Send a message](/img/ai-chatbot/3.png)
125+
126+
We'll add two more steps under the **Then, do these things** section.
127+
128+
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**.
129+
130+
![Bolty custom function](/img/ai-chatbot/4.png)
131+
132+
For the final step, complete the following:
133+
134+
1. Choose **Messages** and then **Send a message to a person**. Under **Select a member**, choose **Person who clicked the button** from the drop-down menu.
135+
2. Under **Add a message**, click **Insert a variable** and choose **`{}Summary`** under the **Bolty Custom Function** section in the list that appears. Click **Save**.
136+
137+
![Summary](/img/ai-chatbot/5.png)
138+
139+
When finished, click **Finish Up**, then click **Publish** to make the workflow available in your workspace.
140+
141+
## Interacting with Bolty {#interact}
142+
143+
### Summarizing recent conversations {#summarize}
144+
145+
In order for Bolty to provide summaries of recent conversation in a channel, Bolty _must_ be a member of that channel.
146+
147+
1. Invite Bolty to a channel that you are able to leave and rejoin (for example, not the **#general** channel or a private channel someone else created) by mentioning the app in the channel&mdash;i.e., tagging **@Bolty** in the channel and sending your message.
148+
2. Slackbot will prompt you to either invite Bolty to the channel, or do nothing. Click **Invite Them**. Now when new users join the channel, the workflow you just created will be kicked off.
149+
150+
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.
151+
152+
![Channel summary](/img/ai-chatbot/7.png)
153+
154+
The central part of this functionality is shown in the following code snippet. Note the use of the [`user_context`](https://api.slack.com/automation/types#usercontext) 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.
155+
156+
```python
157+
from ai.providers import get_provider_response
158+
from logging import Logger
159+
from slack_bolt import Complete, Fail, Ack
160+
from slack_sdk import WebClient
161+
from ..listener_utils.listener_constants import SUMMARIZE_CHANNEL_WORKFLOW
162+
from ..listener_utils.parse_conversation import parse_conversation
163+
164+
"""
165+
Handles the event to summarize a Slack channel's conversation history.
166+
It retrieves the conversation history, parses it, generates a summary using an AI response,
167+
and completes the workflow with the summary or fails if an error occurs.
168+
"""
169+
170+
171+
def handle_summary_function_callback(
172+
ack: Ack, inputs: dict, fail: Fail, logger: Logger, client: WebClient, complete: Complete
173+
):
174+
ack()
175+
try:
176+
user_context = inputs["user_context"]
177+
channel_id = inputs["channel_id"]
178+
history = client.conversations_history(channel=channel_id, limit=10)["messages"]
179+
conversation = parse_conversation(history)
180+
181+
summary = get_provider_response(user_context["id"], SUMMARIZE_CHANNEL_WORKFLOW, conversation)
182+
183+
complete({"user_context": user_context, "response": summary})
184+
except Exception as e:
185+
logger.exception(e)
186+
fail(e)
187+
```
188+
189+
### Asking Bolty a question {#ask-app}
190+
191+
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.
192+
193+
You can also navigate to **Bolty** in your **Apps** list and select the **Messages** tab to chat with Bolty directly.
194+
195+
![Ask Bolty](/img/ai-chatbot/8.png)
196+
197+
## Next steps {#next-steps}
198+
199+
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.
200+
201+
* To learn more about Bolt for Python, refer to the [Getting started](../getting-started) documentation.
202+
* For more details about creating workflow steps using the Bolt SDK, refer to the [workflow steps for Bolt](https://api.slack.com/automation/functions/custom-bolt) guide.
203+
* To use the Bolt for Python SDK to develop on the automations platform, refer to the [Create a workflow step for Workflow Builder: Bolt for Python](https://api.slack.com/tutorials/tracks/bolt-custom-function) tutorial.

docs/sidebars.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ const sidebars = {
8181
type: 'category',
8282
label: 'Tutorials',
8383
items: [
84-
'tutorial/getting-started-http'
84+
'tutorial/getting-started-http',
85+
'tutorial/ai-chatbot'
8586
],
8687
},
8788
{ type: 'html', value: '<hr>' },

docs/static/img/ai-chatbot/1.png

55.7 KB
Loading

docs/static/img/ai-chatbot/2.png

66.8 KB
Loading

docs/static/img/ai-chatbot/3.png

175 KB
Loading

docs/static/img/ai-chatbot/4.png

58 KB
Loading

docs/static/img/ai-chatbot/5.png

129 KB
Loading

docs/static/img/ai-chatbot/6.png

46.7 KB
Loading

docs/static/img/ai-chatbot/7.png

357 KB
Loading

docs/static/img/ai-chatbot/8.png

288 KB
Loading

0 commit comments

Comments
 (0)