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/web.md
+106Lines changed: 106 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,6 +33,8 @@ except SlackApiError as e:
33
33
assert e.response["error"] # str like 'invalid_auth', 'channel_not_found'
34
34
```
35
35
36
+
### Sending ephemeral messages
37
+
36
38
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.
:::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
+
defhandle_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
+
defcreate_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
+
defhandle_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
+
54
160
## Formatting messages with Block Kit {#block-kit}
55
161
56
162
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