|
| 1 | +import warnings |
| 2 | +from typing import Optional |
| 3 | + |
| 4 | +from slack_sdk.web.async_client import AsyncWebClient |
| 5 | +from slack_sdk.web.async_chat_stream import AsyncChatStream |
| 6 | + |
| 7 | +from slack_bolt.warning import ExperimentalWarning |
| 8 | + |
| 9 | + |
| 10 | +class AsyncSayStream: |
| 11 | + client: AsyncWebClient |
| 12 | + channel: Optional[str] |
| 13 | + recipient_team_id: Optional[str] |
| 14 | + recipient_user_id: Optional[str] |
| 15 | + thread_ts: Optional[str] |
| 16 | + |
| 17 | + def __init__( |
| 18 | + self, |
| 19 | + *, |
| 20 | + client: AsyncWebClient, |
| 21 | + channel: Optional[str] = None, |
| 22 | + recipient_team_id: Optional[str] = None, |
| 23 | + recipient_user_id: Optional[str] = None, |
| 24 | + thread_ts: Optional[str] = None, |
| 25 | + ): |
| 26 | + self.client = client |
| 27 | + self.channel = channel |
| 28 | + self.recipient_team_id = recipient_team_id |
| 29 | + self.recipient_user_id = recipient_user_id |
| 30 | + self.thread_ts = thread_ts |
| 31 | + |
| 32 | + async def __call__( |
| 33 | + self, |
| 34 | + *, |
| 35 | + buffer_size: Optional[int] = None, |
| 36 | + channel: Optional[str] = None, |
| 37 | + recipient_team_id: Optional[str] = None, |
| 38 | + recipient_user_id: Optional[str] = None, |
| 39 | + thread_ts: Optional[str] = None, |
| 40 | + **kwargs, |
| 41 | + ) -> AsyncChatStream: |
| 42 | + """Starts a new chat stream with context. |
| 43 | +
|
| 44 | + Warning: This is an experimental feature and may change in future versions. |
| 45 | + """ |
| 46 | + warnings.warn( |
| 47 | + "say_stream is experimental and may change in future versions.", |
| 48 | + category=ExperimentalWarning, |
| 49 | + stacklevel=2, |
| 50 | + ) |
| 51 | + |
| 52 | + channel = channel or self.channel |
| 53 | + thread_ts = thread_ts or self.thread_ts |
| 54 | + if channel is None: |
| 55 | + raise ValueError("say_stream without channel here is unsupported") |
| 56 | + if thread_ts is None: |
| 57 | + raise ValueError("say_stream without thread_ts here is unsupported") |
| 58 | + |
| 59 | + if buffer_size is not None: |
| 60 | + return await self.client.chat_stream( |
| 61 | + buffer_size=buffer_size, |
| 62 | + channel=channel, |
| 63 | + recipient_team_id=recipient_team_id or self.recipient_team_id, |
| 64 | + recipient_user_id=recipient_user_id or self.recipient_user_id, |
| 65 | + thread_ts=thread_ts, |
| 66 | + **kwargs, |
| 67 | + ) |
| 68 | + return await self.client.chat_stream( |
| 69 | + channel=channel, |
| 70 | + recipient_team_id=recipient_team_id or self.recipient_team_id, |
| 71 | + recipient_user_id=recipient_user_id or self.recipient_user_id, |
| 72 | + thread_ts=thread_ts, |
| 73 | + **kwargs, |
| 74 | + ) |
0 commit comments