Skip to content

Commit c623822

Browse files
committed
feat: add authorship arguments to chat stream helper
1 parent 3c63542 commit c623822

5 files changed

Lines changed: 56 additions & 0 deletions

File tree

slack_sdk/web/async_chat_stream.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ def __init__(
4040
recipient_team_id: Optional[str] = None,
4141
recipient_user_id: Optional[str] = None,
4242
task_display_mode: Optional[str] = None,
43+
icon_emoji: Optional[str] = None,
44+
icon_url: Optional[str] = None,
45+
username: Optional[str] = None,
4346
**kwargs,
4447
):
4548
"""Initialize a new ChatStream instance.
@@ -57,6 +60,9 @@ def __init__(
5760
recipient_user_id: The encoded ID of the user to receive the streaming text. Required when streaming to channels.
5861
task_display_mode: Specifies how tasks are displayed in the message. A "timeline" displays individual tasks
5962
with text and "plan" displays all tasks together.
63+
icon_emoji: Emoji to use as the icon for this message. Overrides icon_url.
64+
icon_url: Image URL to use as the icon for this message.
65+
username: The bot's username to display.
6066
buffer_size: The length of markdown_text to buffer in-memory before calling a method. Increasing this value
6167
decreases the number of method calls made for the same amount of text, which is useful to avoid rate limits.
6268
**kwargs: Additional arguments passed to the underlying API calls.
@@ -70,6 +76,9 @@ def __init__(
7076
"recipient_team_id": recipient_team_id,
7177
"recipient_user_id": recipient_user_id,
7278
"task_display_mode": task_display_mode,
79+
"icon_emoji": icon_emoji,
80+
"icon_url": icon_url,
81+
"username": username,
7382
**kwargs,
7483
}
7584
self._buffer = ""

slack_sdk/web/async_client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2977,6 +2977,9 @@ async def chat_stream(
29772977
recipient_team_id: Optional[str] = None,
29782978
recipient_user_id: Optional[str] = None,
29792979
task_display_mode: Optional[str] = None,
2980+
icon_emoji: Optional[str] = None,
2981+
icon_url: Optional[str] = None,
2982+
username: Optional[str] = None,
29802983
**kwargs,
29812984
) -> AsyncChatStream:
29822985
"""Stream markdown text into a conversation.
@@ -3005,6 +3008,9 @@ async def chat_stream(
30053008
recipient_user_id: The encoded ID of the user to receive the streaming text. Required when streaming to channels.
30063009
task_display_mode: Specifies how tasks are displayed in the message. A "timeline" displays individual tasks
30073010
with text and "plan" displays all tasks together.
3011+
icon_emoji: Emoji to use as the icon for this message. Overrides icon_url.
3012+
icon_url: Image URL to use as the icon for this message.
3013+
username: The bot's username to display.
30083014
**kwargs: Additional arguments passed to the underlying API calls.
30093015
30103016
Returns:
@@ -3031,6 +3037,9 @@ async def chat_stream(
30313037
recipient_team_id=recipient_team_id,
30323038
recipient_user_id=recipient_user_id,
30333039
task_display_mode=task_display_mode,
3040+
icon_emoji=icon_emoji,
3041+
icon_url=icon_url,
3042+
username=username,
30343043
buffer_size=buffer_size,
30353044
**kwargs,
30363045
)

slack_sdk/web/chat_stream.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ def __init__(
3030
recipient_team_id: Optional[str] = None,
3131
recipient_user_id: Optional[str] = None,
3232
task_display_mode: Optional[str] = None,
33+
icon_emoji: Optional[str] = None,
34+
icon_url: Optional[str] = None,
35+
username: Optional[str] = None,
3336
**kwargs,
3437
):
3538
"""Initialize a new ChatStream instance.
@@ -47,6 +50,9 @@ def __init__(
4750
recipient_user_id: The encoded ID of the user to receive the streaming text. Required when streaming to channels.
4851
task_display_mode: Specifies how tasks are displayed in the message. A "timeline" displays individual tasks
4952
with text and "plan" displays all tasks together.
53+
icon_emoji: Emoji to use as the icon for this message. Overrides icon_url.
54+
icon_url: Image URL to use as the icon for this message.
55+
username: The bot's username to display.
5056
buffer_size: The length of markdown_text to buffer in-memory before calling a method. Increasing this value
5157
decreases the number of method calls made for the same amount of text, which is useful to avoid rate limits.
5258
**kwargs: Additional arguments passed to the underlying API calls.
@@ -60,6 +66,9 @@ def __init__(
6066
"recipient_team_id": recipient_team_id,
6167
"recipient_user_id": recipient_user_id,
6268
"task_display_mode": task_display_mode,
69+
"icon_emoji": icon_emoji,
70+
"icon_url": icon_url,
71+
"username": username,
6372
**kwargs,
6473
}
6574
self._buffer = ""

slack_sdk/web/client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2967,6 +2967,9 @@ def chat_stream(
29672967
recipient_team_id: Optional[str] = None,
29682968
recipient_user_id: Optional[str] = None,
29692969
task_display_mode: Optional[str] = None,
2970+
icon_emoji: Optional[str] = None,
2971+
icon_url: Optional[str] = None,
2972+
username: Optional[str] = None,
29702973
**kwargs,
29712974
) -> ChatStream:
29722975
"""Stream markdown text into a conversation.
@@ -2995,6 +2998,9 @@ def chat_stream(
29952998
recipient_user_id: The encoded ID of the user to receive the streaming text. Required when streaming to channels.
29962999
task_display_mode: Specifies how tasks are displayed in the message. A "timeline" displays individual tasks
29973000
with text and "plan" displays all tasks together.
3001+
icon_emoji: Emoji to use as the icon for this message. Overrides icon_url.
3002+
icon_url: Image URL to use as the icon for this message.
3003+
username: The bot's username to display.
29983004
**kwargs: Additional arguments passed to the underlying API calls.
29993005
30003006
Returns:
@@ -3021,6 +3027,9 @@ def chat_stream(
30213027
recipient_team_id=recipient_team_id,
30223028
recipient_user_id=recipient_user_id,
30233029
task_display_mode=task_display_mode,
3030+
icon_emoji=icon_emoji,
3031+
icon_url=icon_url,
3032+
username=username,
30243033
buffer_size=buffer_size,
30253034
**kwargs,
30263035
)

tests/slack_sdk/web/test_chat_stream.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,26 @@ def test_streams_a_chunk_message(self):
242242
'[{"text": "\\n", "type": "markdown_text"}, {"text": ":space_invader:", "type": "markdown_text"}]',
243243
)
244244

245+
def test_streams_with_authorship_args(self):
246+
streamer = self.client.chat_stream(
247+
channel="C0123456789",
248+
thread_ts="123.000",
249+
username="Abacus",
250+
icon_emoji="abacus",
251+
)
252+
self.assertEqual(streamer._stream_args["username"], "Abacus")
253+
self.assertEqual(streamer._stream_args["icon_emoji"], "abacus")
254+
self.assertIsNone(streamer._stream_args["icon_url"])
255+
256+
streamer.append(markdown_text="counting...")
257+
streamer.stop()
258+
259+
if hasattr(self.thread.server, "chat_stream_requests"):
260+
start_request = self.thread.server.chat_stream_requests.get("/chat.startStream", {})
261+
self.assertEqual(start_request.get("username"), "Abacus")
262+
self.assertEqual(start_request.get("icon_emoji"), "abacus")
263+
self.assertNotIn("icon_url", start_request)
264+
245265
def test_streams_errors_when_appending_to_an_unstarted_stream(self):
246266
streamer = self.client.chat_stream(
247267
channel="C0123456789",

0 commit comments

Comments
 (0)