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
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)
55
53
56
54
```python
57
-
# Example: Stream a response to any message
58
-
@app.message()
59
-
defhandle_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)
69
70
```
70
71
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.
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