Skip to content

Commit b59950d

Browse files
go
1 parent 4d62d38 commit b59950d

File tree

1 file changed

+24
-64
lines changed

1 file changed

+24
-64
lines changed

docs/english/concepts/message-sending.md

Lines changed: 24 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -41,65 +41,35 @@ def show_datepicker(event, say):
4141
)
4242
```
4343

44-
## Streaming messages
44+
## Streaming messages {#streaming-messages}
4545

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

48-
* `chat_startStream`
49-
* `chat_appendStream`
50-
* `chat_stopStream`
48+
* [`chat_startStream`](/reference/methods/chat.startstream)
49+
* [`chat_appendStream`](/reference/methods/chat.appendstream)
50+
* [`chat_stopStream`](/reference/methods/chat.stopstream)
5151

52-
### Starting the message stream
53-
54-
First you need to begin the message stream.
52+
The Python Slack SDK provides a [`chat_stream()`](https://docss.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/blob/main/listeners/assistant/assistant.py)
5553

5654
```python
57-
# Example: Stream a response to any message
58-
@app.message()
59-
def handle_message(message, client):
60-
channel_id = payload["channel"]
61-
thread_ts = payload["thread_ts"]
62-
63-
# Start a new message stream
64-
stream_response = client.chat_startStream(
65-
channel=channel_id,
66-
thread_ts=thread_ts,
67-
)
68-
stream_ts = stream_response["ts"]
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+
for event in returned_message:
63+
if event.type == "response.output_text.delta":
64+
streamer.append(markdown_text=f"{event.delta}")
65+
else:
66+
continue
67+
68+
feedback_block = create_feedback_block()
69+
streamer.stop(blocks=feedback_block)
6970
```
7071

71-
### Appending content to the message stream
72-
73-
With the stream started, you can then append text to it in chunks to convey a streaming effect.
74-
75-
The structure of the text coming in will depend on your source. The following code snippet uses OpenAI's response structure as an example.
76-
77-
```python
78-
# continued from above
79-
for event in returned_message:
80-
if event.type == "response.output_text.delta":
81-
client.chat_appendStream(
82-
channel=channel_id,
83-
ts=stream_ts,
84-
markdown_text=f"{event.delta}"
85-
)
86-
else:
87-
continue
88-
```
89-
90-
### Finishing the message stream
91-
92-
Your app can then end the stream with the `chat_stopStream` method.
93-
94-
```python
95-
# continued from above
96-
client.chat_stopStream(
97-
channel=channel_id,
98-
ts=stream_ts
99-
)
100-
```
101-
102-
The method also provides you an opportunity to request user feedback on your app's responses using the [feedback buttons](/reference/block-kit/block-elements/feedback-buttons-element) block element within the [context actions](/reference/block-kit/blocks/context-actions-block) block. The user will be presented with thumbs up and thumbs down buttons
72+
In that example, a [feedback buttons](/reference/block-kit/block-elements/feedback-buttons-element) block element is passed to `streamer.stop` — this provides feedback buttons to the user at the bottom of the message.
10373

10474
```python
10575
def create_feedback_block() -> List[Block]:
@@ -123,16 +93,6 @@ def create_feedback_block() -> List[Block]:
12393
)
12494
]
12595
return blocks
96+
```
12697

127-
@app.message()
128-
def handle_message(message, client):
129-
# ... previous streaming code ...
130-
131-
# Stop the stream and add interactive elements
132-
feedback_block = create_feedback_block()
133-
client.chat_stopStream(
134-
channel=channel_id,
135-
ts=stream_ts,
136-
blocks=feedback_block
137-
)
138-
```
98+
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.

0 commit comments

Comments
 (0)