You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/english/concepts/ai-apps.md
+6-277Lines changed: 6 additions & 277 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,9 +7,13 @@ If you're unfamiliar with using these feature within Slack, you may want to read
7
7
8
8
## Listening for events
9
9
10
-
11
10
Agents can be invoked throughout Slack, such as @mentions in channels.
12
11
12
+
:::tip[Using the Assistant side panel]
13
+
14
+
The Assistant side panel requires additional setup. See the [Assistant class guide](/concepts/assistant-class).
15
+
:::
16
+
13
17
```python
14
18
import re
15
19
from logging import Logger
@@ -59,283 +63,10 @@ def handle_app_mentioned(
59
63
...
60
64
```
61
65
62
-
### Via the Assistant class (side panel)
63
-
64
-
:::info[Some features within this guide require a paid plan]
65
-
If you don't have a paid workspace for development, you can join the [Developer Program](https://api.slack.com/developer-program) and provision a sandbox with access to all Slack features for free.
66
-
:::
67
-
68
-
The [`Assistant`](/tools/bolt-js/reference#the-assistantconfig-configuration-object) class handles incoming events from the Slack Agents & AI Apps feature. A typical flow:
69
-
70
-
1.[The user starts a thread](#handling-new-thread). The `Assistant` class handles the incoming [`assistant_thread_started`](/reference/events/assistant_thread_started) event.
71
-
2.[The thread context may change](#handling-thread-context-changes). The `Assistant` class handles [`assistant_thread_context_changed`](/reference/events/assistant_thread_context_changed) events and automatically manages context.
72
-
3.[The user responds](#handling-user-response). The `Assistant` class handles the incoming [`message.im`](/reference/events/message.im) event.
73
-
74
-
```python
75
-
assistant = Assistant()
76
-
77
-
# This listener is invoked when a human user opened an assistant thread
78
-
@assistant.thread_started
79
-
defstart_assistant_thread(
80
-
say: Say,
81
-
get_thread_context: GetThreadContext,
82
-
set_suggested_prompts: SetSuggestedPrompts,
83
-
logger: logging.Logger,
84
-
):
85
-
try:
86
-
...
87
-
88
-
# This listener is invoked when the human user sends a reply in the assistant thread
89
-
@assistant.user_message
90
-
defrespond_in_assistant_thread(
91
-
client: WebClient,
92
-
context: BoltContext,
93
-
get_thread_context: GetThreadContext,
94
-
logger: logging.Logger,
95
-
payload: dict,
96
-
say: Say,
97
-
set_status: SetStatus,
98
-
):
99
-
try:
100
-
...
101
-
102
-
# Enable this assistant middleware in your Bolt app
103
-
app.use(assistant)
104
-
```
105
-
106
-
:::info[Consider the following]
107
-
You _could_ go it alone and [listen](/tools/bolt-python/concepts/event-listening) for the `assistant_thread_started`, `assistant_thread_context_changed`, and `message.im` events in order to implement the AI features in your app. That being said, using the `Assistant` class will streamline the process. And we already wrote this nice guide for you!
108
-
:::
109
-
110
-
While the `assistant_thread_started` and `assistant_thread_context_changed` events do provide Slack-client thread context information, the `message.im` event does not. Any subsequent user message events won't contain thread context data. For that reason, Bolt not only provides a way to store thread context — the `threadContextStore` property — but it also provides a `DefaultThreadContextStore` instance that is utilized by default. This implementation relies on storing and retrieving [message metadata](/messaging/message-metadata/) as the user interacts with the app.
111
-
112
-
If you do provide your own `threadContextStore` property, it must feature `get` and `save` methods.
113
-
114
-
:::tip[Refer to the [reference docs](https://docs.slack.dev/tools/bolt-python/reference/kwargs_injection/args.html) to learn the available listener arguments.]
115
-
:::
116
-
117
-
#### Configuring your app to support the `Assistant` class {#configuring-assistant-class}
118
-
119
-
1. Within [App Settings](https://api.slack.com/apps), enable the **Agents & AI Apps** feature.
120
-
121
-
2. Within the App Settings **OAuth & Permissions** page, add the following scopes:
When the user opens a new thread with your AI-enabled app, the [`assistant_thread_started`](/reference/events/assistant_thread_started) event will be sent to your app.
134
-
135
-
:::tip[When a user opens an app thread while in a channel, the channel info is stored as the thread's `AssistantThreadContext` data.]
136
-
137
-
You can grab that info by using the `get_thread_context` utility, as subsequent user message event payloads won't include the channel info.
138
-
:::
139
-
140
-
```python
141
-
assistant = Assistant()
142
-
143
-
@assistant.thread_started
144
-
defstart_assistant_thread(
145
-
say: Say,
146
-
get_thread_context: GetThreadContext,
147
-
set_suggested_prompts: SetSuggestedPrompts,
148
-
logger: logging.Logger,
149
-
):
150
-
try:
151
-
say("How can I help you?")
152
-
153
-
prompts: List[Dict[str, str]] = [
154
-
{
155
-
"title": "Suggest names for my Slack app",
156
-
"message": "Can you suggest a few names for my Slack app? The app helps my teammates better organize information and plan priorities and action items.",
157
-
},
158
-
]
159
-
160
-
thread_context = get_thread_context()
161
-
if thread_context isnotNoneand thread_context.channel_id isnotNone:
162
-
summarize_channel = {
163
-
"title": "Summarize the referred channel",
164
-
"message": "Can you generate a brief summary of the referred channel?",
165
-
}
166
-
prompts.append(summarize_channel)
167
-
168
-
set_suggested_prompts(prompts=prompts)
169
-
exceptExceptionas e:
170
-
logger.exception(f"Failed to handle an assistant_thread_started event: {e}", e)
171
-
say(f":warning: Something went wrong! ({e})")
172
-
```
173
-
174
-
You can send more complex messages to the user — see [Sending Block Kit alongside messages](#block-kit-interactions) for more info.
When the user switches channels, the [`assistant_thread_context_changed`](/reference/events/assistant_thread_context_changed) event will be sent to your app.
179
-
180
-
If you use the built-in `Assistant` middleware without any custom configuration, the updated context data is automatically saved as [message metadata](/messaging/message-metadata/) of the first reply from the app.
181
-
182
-
As long as you use the built-in approach, you don't need to store the context data within a datastore. The downside of this default behavior is the overhead of additional calls to the Slack API. These calls include those to `conversations.history`, which are used to look up the stored message metadata that contains the thread context (via `get_thread_context`).
183
-
184
-
To store context elsewhere, pass a custom `AssistantThreadContextStore` implementation to the `Assistant` constructor. We provide `FileAssistantThreadContextStore`, which is a reference implementation that uses the local file system. Since this reference implementation relies on local files, it's not advised for use in production. For production apps, we recommend creating a class that inherits `AssistantThreadContextStore`.
185
-
186
-
```python
187
-
from slack_bolt import FileAssistantThreadContextStore
#### Handling the user response {#handling-user-response}
192
-
193
-
When the user messages your app, the [`message.im`](/reference/events/message.im) event will be sent to your app.
194
-
195
-
Messages sent to the app do not contain a [subtype](/reference/events/message#subtypes) and must be deduced based on their shape and any provided [message metadata](/messaging/message-metadata/).
196
-
197
-
There are three utilities that are particularly useful in curating the user experience:
Within the `setStatus` utility, you can cycle through strings passed into a `loading_messages` array. See [Setting assistant status](#setting-assistant-status) for implementation examples.
For advanced use cases, Block Kit buttons may be used instead of suggested prompts, as well as the sending of messages with structured [metadata](/messaging/message-metadata/) to trigger subsequent interactions with the user.
207
-
208
-
For example, an app can display a button such as "Summarize the referring channel" in the initial reply. When the user clicks the button and submits detailed information (such as the number of messages, days to check, purpose of the summary, etc.), the app can handle that information and post a message that describes the request with structured metadata.
209
-
210
-
By default, apps can't respond to their own bot messages (Bolt prevents infinite loops by default). However, if you pass `ignoring_self_assistant_message_events_enabled=False` to the `App` constructor and add a `bot_message` listener to your `Assistant` middleware, your app can continue processing the request as shown below:
You can have your app's messages stream in to replicate conventional agent behavior. Bolt for Python provides a `say_stream` utility as a listener argument available for `app.event` and `app.message` listeners.
@@ -795,4 +523,5 @@ def handle_app_mentioned(
795
523
796
524
---
797
525
526
+
For a dedicated AI assistant experience with a side panel, see the [Assistant class guide](./assistant-class.md).
0 commit comments