Skip to content

Commit 14a4273

Browse files
go
1 parent 34530f7 commit 14a4273

File tree

2 files changed

+50
-68
lines changed

2 files changed

+50
-68
lines changed

docs/english/concepts/message-sending.md

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,37 +43,64 @@ def show_datepicker(event, say):
4343

4444
## Streaming messages {#streaming-messages}
4545

46-
You can have your app's messages stream in to replicate conventional AI chatbot behavior. This is done through three Web API methods:
46+
You can have your app's messages stream in to replicate conventional agent behavior. This is done through three Web API methods:
4747

4848
* [`chat_startStream`](/reference/methods/chat.startStream)
4949
* [`chat_appendStream`](/reference/methods/chat.appendStream)
5050
* [`chat_stopStream`](/reference/methods/chat.stopStream)
5151

52-
The Python Slack SDK provides a [`chat_stream()`](https://docs.slack.dev/tools/python-slack-sdk/reference/web/client.html#slack_sdk.web.client.WebClient.chat_stream) helper utility to streamline calling these methods. Here's an excerpt from our [Assistant template app](https://github.com/slack-samples/bolt-python-assistant-template):
52+
Bolt for Python provides a `say_stream` listener argument available on `app.event` and `app.message` listeners.
5353

54-
```python
55-
streamer = client.chat_stream(
56-
channel=channel_id,
57-
recipient_team_id=team_id,
58-
recipient_user_id=user_id,
59-
thread_ts=thread_ts,
60-
)
61-
62-
# Loop over OpenAI response stream
63-
# https://platform.openai.com/docs/api-reference/responses/create
64-
for event in returned_message:
65-
if event.type == "response.output_text.delta":
66-
streamer.append(markdown_text=f"{event.delta}")
67-
else:
68-
continue
69-
70-
feedback_block = create_feedback_block()
71-
streamer.stop(blocks=feedback_block)
54+
The `say_stream` utility streamlines calling the Python Slack SDK's [`WebClient.chat_stream`](https://docs.slack.dev/tools/python-slack-sdk/reference/web/client.html#slack_sdk.web.client.WebClient.chat_stream) helper utility by sourcing parameter values from the relevant event payload.
55+
56+
| Parameter | Value |
57+
|---|---|
58+
| `channel_id` | Sourced from the event payload.
59+
| `thread_ts` | Sourced from the event payload. Falls back to the `ts` value if available.
60+
| `recipient_team_id` | Sourced from the event `team_id` (`enterprise_id` if the app is installed on an org).
61+
| `recipient_user_id` | Sourced from the `user_id` of the event.
62+
63+
If neither a `channel_id` or `thread_ts` can be sourced, then the utility will merely be `None`.
64+
65+
For information on calling the `chat_*Stream` API methods directly, see the [_Sending streaming messages_](/tools/python-slack-sdk/web#sending-streaming-messages) section of the Python Slack SDK docs.
66+
67+
### Example {#example}
68+
69+
```py
70+
import os
71+
72+
from slack_bolt import App, SayStream
73+
from slack_bolt.adapter.socket_mode import SocketModeHandler
74+
from slack_sdk import WebClient
75+
76+
app = App(token=os.environ.get("SLACK_BOT_TOKEN"))
77+
78+
@app.event("app_mention")
79+
def handle_app_mention(client: WebClient, say_stream: SayStream):
80+
stream = say_stream()
81+
stream.append(markdown_text="Someone rang the bat signal!")
82+
stream.stop()
83+
84+
@app.message("")
85+
def handle_message(client: WebClient, say_stream: SayStream):
86+
stream = say_stream()
87+
88+
stream.append(markdown_text="Let me consult my *vast knowledge database*...)
89+
stream.stop()
90+
91+
if __name__ == "__main__":
92+
SocketModeHandler(app, os.environ.get("SLACK_APP_TOKEN")).start()
7293
```
7394

74-
In that example, a [feedback buttons](/reference/block-kit/block-elements/feedback-buttons-element) block element is passed to `streamer.stop` to provide feedback buttons to the user at the bottom of the message. Interaction with these buttons will send a block action event to your app to receive the feedback.
95+
#### Adding feedback buttons after a stream
7596

76-
```python
97+
You can pass a [feedback buttons](/reference/block-kit/block-elements/feedback-buttons-element) block element to `stream.stop` to provide feedback buttons to the user at the bottom of the message. Interaction with these buttons will send a block action event to your app to receive the feedback.
98+
99+
```py
100+
stream.stop(blocks=feedback_block)
101+
```
102+
103+
```py
77104
def create_feedback_block() -> List[Block]:
78105
blocks: List[Block] = [
79106
ContextActionsBlock(
@@ -95,6 +122,4 @@ def create_feedback_block() -> List[Block]:
95122
)
96123
]
97124
return blocks
98-
```
99-
100-
For information on calling the `chat_*Stream` API methods without the helper utility, see the [_Sending streaming messages_](/tools/python-slack-sdk/web#sending-streaming-messages) section of the Python Slack SDK docs.
125+
```

docs/english/experiments.md

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -33,46 +33,3 @@ def handle_mention(agent: BoltAgent):
3333
### Limitations
3434

3535
The `chat_stream()` method currently only works when the `thread_ts` field is available in the event context (DMs and threaded replies). Top-level channel messages do not have a `thread_ts` field, and the `ts` field is not yet provided to `BoltAgent`.
36-
37-
## `say_stream` utility {#say-stream}
38-
39-
The `say_stream` utility is a listener argument available on `app.event` and `app.message` listeners.
40-
41-
The `say_stream` utility streamlines calling the Python Slack SDK's [`WebClient.chat_stream`](https://docs.slack.dev/tools/python-slack-sdk/reference/web/client.html#slack_sdk.web.client.WebClient.chat_stream) helper utility by sourcing parameter values from the relevant event payload.
42-
43-
| Parameter | Value |
44-
|---|---|
45-
| `channel_id` | Sourced from the event payload.
46-
| `thread_ts` | Sourced from the event payload. Falls back to the `ts` value if available.
47-
| `recipient_team_id` | Sourced from the event `team_id` (`enterprise_id` if the app is installed on an org).
48-
| `recipient_user_id` | Sourced from the `user_id` of the event.
49-
50-
If neither a `channel_id` or `thread_ts` can be sourced, then the utility will merely be `None`.
51-
52-
### Example {#example}
53-
54-
```py
55-
import os
56-
57-
from slack_bolt import App, SayStream
58-
from slack_bolt.adapter.socket_mode import SocketModeHandler
59-
from slack_sdk import WebClient
60-
61-
app = App(token=os.environ.get("SLACK_BOT_TOKEN"))
62-
63-
@app.event("app_mention")
64-
def handle_app_mention(client: WebClient, say_stream: SayStream):
65-
stream = say_stream()
66-
stream.append(markdown_text="Someone rang the bat signal!")
67-
stream.stop()
68-
69-
@app.message("")
70-
def handle_message(client: WebClient, say_stream: SayStream):
71-
stream = say_stream()
72-
73-
stream.append(markdown_text="Let me consult my *vast knowledge database*...)
74-
stream.stop()
75-
76-
if __name__ == "__main__":
77-
SocketModeHandler(app, os.environ.get("SLACK_APP_TOKEN")).start()
78-
```

0 commit comments

Comments
 (0)