|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +from slack_sdk.web.async_client import AsyncWebClient |
| 4 | +from slack_sdk.web.async_chat_stream import AsyncChatStream |
| 5 | + |
| 6 | + |
| 7 | +class AsyncBoltAgent: |
| 8 | + """Async 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 | + @app.event("app_mention") |
| 14 | + async def handle_mention(agent): |
| 15 | + stream = await agent.chat_stream() |
| 16 | + await stream.append(markdown_text="Hello!") |
| 17 | + await stream.stop() |
| 18 | + """ |
| 19 | + |
| 20 | + def __init__( |
| 21 | + self, |
| 22 | + *, |
| 23 | + client: AsyncWebClient, |
| 24 | + channel_id: Optional[str] = None, |
| 25 | + thread_ts: Optional[str] = None, |
| 26 | + team_id: Optional[str] = None, |
| 27 | + user_id: Optional[str] = None, |
| 28 | + ): |
| 29 | + self._client = client |
| 30 | + self._channel_id = channel_id |
| 31 | + self._thread_ts = thread_ts |
| 32 | + self._team_id = team_id |
| 33 | + self._user_id = user_id |
| 34 | + |
| 35 | + async def chat_stream( |
| 36 | + self, |
| 37 | + *, |
| 38 | + channel: Optional[str] = None, |
| 39 | + thread_ts: Optional[str] = None, |
| 40 | + recipient_team_id: Optional[str] = None, |
| 41 | + recipient_user_id: Optional[str] = None, |
| 42 | + **kwargs, |
| 43 | + ) -> AsyncChatStream: |
| 44 | + """Creates an AsyncChatStream with defaults from event context. |
| 45 | +
|
| 46 | + Each call creates a new instance. Create multiple for parallel streams. |
| 47 | +
|
| 48 | + Args: |
| 49 | + channel: Channel ID. Defaults to the channel from the event context. |
| 50 | + thread_ts: Thread timestamp. Defaults to the thread_ts from the event context. |
| 51 | + recipient_team_id: Team ID of the recipient. Defaults to the team from the event context. |
| 52 | + recipient_user_id: User ID of the recipient. Defaults to the user from the event context. |
| 53 | + **kwargs: Additional arguments passed to ``AsyncWebClient.chat_stream()``. |
| 54 | +
|
| 55 | + Returns: |
| 56 | + A new ``AsyncChatStream`` instance. |
| 57 | + """ |
| 58 | + resolved_channel = channel or self._channel_id |
| 59 | + resolved_thread_ts = thread_ts or self._thread_ts |
| 60 | + if resolved_channel is None: |
| 61 | + raise ValueError( |
| 62 | + "channel is required: provide it as an argument or ensure channel_id is set in the event context" |
| 63 | + ) |
| 64 | + if resolved_thread_ts is None: |
| 65 | + raise ValueError( |
| 66 | + "thread_ts is required: provide it as an argument or ensure thread_ts is set in the event context" |
| 67 | + ) |
| 68 | + return await self._client.chat_stream( |
| 69 | + channel=resolved_channel, |
| 70 | + thread_ts=resolved_thread_ts, |
| 71 | + recipient_team_id=recipient_team_id or self._team_id, |
| 72 | + recipient_user_id=recipient_user_id or self._user_id, |
| 73 | + **kwargs, |
| 74 | + ) |
0 commit comments