Skip to content

Commit 4bcb4db

Browse files
go
1 parent 82aad20 commit 4bcb4db

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

docs/english/web.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ except SlackApiError as e:
3333
assert e.response["error"] # str like 'invalid_auth', 'channel_not_found'
3434
```
3535

36+
### Sending ephemeral messages
37+
3638
Sending an ephemeral message, which is only visible to an assigned user in a specified channel, is nearly the same as sending a regular message but with an additional `user` parameter.
3739

3840
``` python
@@ -51,6 +53,110 @@ response = client.chat_postEphemeral(
5153

5254
See the [`chat.postEphemeral`](/reference/methods/chat.postEphemeral) API method for more details.
5355

56+
### Sending streaming messages {#sending-streaming-messages}
57+
58+
You can have your app's messages stream in to replicate conventional AI chatbot behavior. This is done through three Web API methods:
59+
60+
* [`chat_startStream`](/reference/methods/chat.startstream)
61+
* [`chat_appendStream`](/reference/methods/chat.appendstream)
62+
* [`chat_stopStream`](/reference/methods/chat.stopstream)
63+
64+
:::tip [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.]
65+
66+
See the [_Streaming messages_](/tools/bolt-python/concepts/message-sendin#streaming-messages) section of the Bolt for Python docs for implementation instructions.
67+
68+
:::
69+
70+
#### Starting the message stream
71+
72+
First you need to begin the message stream:
73+
74+
```python
75+
# Example: Stream a response to any message
76+
@app.message()
77+
def handle_message(message, client):
78+
channel_id = payload["channel"]
79+
thread_ts = payload["thread_ts"]
80+
81+
# Start a new message stream
82+
stream_response = client.chat_startStream(
83+
channel=channel_id,
84+
thread_ts=thread_ts,
85+
)
86+
stream_ts = stream_response["ts"]
87+
```
88+
89+
#### Appending content to the message stream
90+
91+
With the stream started, you can then append text to it in chunks to convey a streaming effect.
92+
93+
The structure of the text coming in will depend on your source. The following code snippet uses OpenAI's response structure as an example:
94+
95+
```python
96+
# continued from above
97+
for event in returned_message:
98+
if event.type == "response.output_text.delta":
99+
client.chat_appendStream(
100+
channel=channel_id,
101+
ts=stream_ts,
102+
markdown_text=f"{event.delta}"
103+
)
104+
else:
105+
continue
106+
```
107+
108+
#### Finishing the message stream
109+
110+
Your app can then end the stream with the `chat_stopStream` method:
111+
112+
```python
113+
# continued from above
114+
client.chat_stopStream(
115+
channel=channel_id,
116+
ts=stream_ts
117+
)
118+
```
119+
120+
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.
121+
122+
```python
123+
def create_feedback_block() -> List[Block]:
124+
blocks: List[Block] = [
125+
ContextActionsBlock(
126+
elements=[
127+
FeedbackButtonsElement(
128+
action_id="feedback",
129+
positive_button=FeedbackButtonObject(
130+
text="Good Response",
131+
accessibility_label="Submit positive feedback on this response",
132+
value="good-feedback",
133+
),
134+
negative_button=FeedbackButtonObject(
135+
text="Bad Response",
136+
accessibility_label="Submit negative feedback on this response",
137+
value="bad-feedback",
138+
),
139+
)
140+
]
141+
)
142+
]
143+
return blocks
144+
145+
@app.message()
146+
def handle_message(message, client):
147+
# ... previous streaming code ...
148+
149+
# Stop the stream and add interactive elements
150+
feedback_block = create_feedback_block()
151+
client.chat_stopStream(
152+
channel=channel_id,
153+
ts=stream_ts,
154+
blocks=feedback_block
155+
)
156+
```
157+
158+
See [Formatting messages with Block Kit](#block-kit) below for more details on using Block Kit with messages.
159+
54160
## Formatting messages with Block Kit {#block-kit}
55161

56162
Messages posted from apps can contain more than just text; they can also include full user interfaces composed of blocks using [Block Kit](/block-kit).

0 commit comments

Comments
 (0)