|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +from slack_sdk import WebClient |
| 4 | +from slack_sdk.web.chat_stream import ChatStream |
| 5 | + |
| 6 | + |
| 7 | +class BoltAgent: |
| 8 | + """Agent listener argument for building AI-powered Slack agents. |
| 9 | +
|
| 10 | + Experimental: |
| 11 | + This API is experimental and may change in future releases. |
| 12 | +
|
| 13 | + FIXME: chat_stream() only works when thread_ts is available (DMs and threaded replies). |
| 14 | + It does not work on channel messages because ts is not provided to BoltAgent yet. |
| 15 | +
|
| 16 | + @app.event("app_mention") |
| 17 | + def handle_mention(agent): |
| 18 | + stream = agent.chat_stream() |
| 19 | + stream.append(markdown_text="Hello!") |
| 20 | + stream.stop() |
| 21 | + """ |
| 22 | + |
| 23 | + def __init__( |
| 24 | + self, |
| 25 | + *, |
| 26 | + client: WebClient, |
| 27 | + channel_id: Optional[str] = None, |
| 28 | + thread_ts: Optional[str] = None, |
| 29 | + team_id: Optional[str] = None, |
| 30 | + user_id: Optional[str] = None, |
| 31 | + ): |
| 32 | + self._client = client |
| 33 | + self._channel_id = channel_id |
| 34 | + self._thread_ts = thread_ts |
| 35 | + self._team_id = team_id |
| 36 | + self._user_id = user_id |
| 37 | + |
| 38 | + def chat_stream( |
| 39 | + self, |
| 40 | + *, |
| 41 | + channel: Optional[str] = None, |
| 42 | + thread_ts: Optional[str] = None, |
| 43 | + recipient_team_id: Optional[str] = None, |
| 44 | + recipient_user_id: Optional[str] = None, |
| 45 | + **kwargs, |
| 46 | + ) -> ChatStream: |
| 47 | + """Creates a ChatStream with defaults from event context. |
| 48 | +
|
| 49 | + Each call creates a new instance. Create multiple for parallel streams. |
| 50 | +
|
| 51 | + Args: |
| 52 | + channel: Channel ID. Defaults to the channel from the event context. |
| 53 | + thread_ts: Thread timestamp. Defaults to the thread_ts from the event context. |
| 54 | + recipient_team_id: Team ID of the recipient. Defaults to the team from the event context. |
| 55 | + recipient_user_id: User ID of the recipient. Defaults to the user from the event context. |
| 56 | + **kwargs: Additional arguments passed to ``WebClient.chat_stream()``. |
| 57 | +
|
| 58 | + Returns: |
| 59 | + A new ``ChatStream`` instance. |
| 60 | + """ |
| 61 | + provided = [arg for arg in (channel, thread_ts, recipient_team_id, recipient_user_id) if arg is not None] |
| 62 | + if provided and len(provided) < 4: |
| 63 | + raise ValueError( |
| 64 | + "Either provide all of channel, thread_ts, recipient_team_id, and recipient_user_id, or none of them" |
| 65 | + ) |
| 66 | + # Argument validation is delegated to chat_stream() and the API |
| 67 | + return self._client.chat_stream( |
| 68 | + channel=channel or self._channel_id, # type: ignore[arg-type] |
| 69 | + thread_ts=thread_ts or self._thread_ts, # type: ignore[arg-type] |
| 70 | + recipient_team_id=recipient_team_id or self._team_id, |
| 71 | + recipient_user_id=recipient_user_id or self._user_id, |
| 72 | + **kwargs, |
| 73 | + ) |
0 commit comments