Skip to content
Draft
21 changes: 0 additions & 21 deletions slack_bolt/context/assistant/assistant_utilities.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import warnings
from typing import Optional

from slack_sdk.web import WebClient
Expand All @@ -11,8 +10,6 @@
from .internals import has_channel_id_and_thread_ts
from ..get_thread_context.get_thread_context import GetThreadContext
from ..save_thread_context import SaveThreadContext
from ..set_status import SetStatus
from ..set_suggested_prompts import SetSuggestedPrompts
from ..set_title import SetTitle


Expand Down Expand Up @@ -47,28 +44,10 @@ def __init__(
# When moving this code to Bolt internals, no need to raise an exception for this pattern
raise ValueError(f"Cannot instantiate Assistant for this event pattern ({self.payload})")

def is_valid(self) -> bool:
return self.channel_id is not None and self.thread_ts is not None

@property
def set_status(self) -> SetStatus:
warnings.warn(
"AssistantUtilities.set_status is deprecated. "
"Use the set_status argument directly in your listener function "
"or access it via context.set_status instead.",
DeprecationWarning,
stacklevel=2,
)
return SetStatus(self.client, self.channel_id, self.thread_ts)

@property
def set_title(self) -> SetTitle:
return SetTitle(self.client, self.channel_id, self.thread_ts)

@property
def set_suggested_prompts(self) -> SetSuggestedPrompts:
return SetSuggestedPrompts(self.client, self.channel_id, self.thread_ts)

@property
def say(self) -> Say:
def build_metadata() -> Optional[dict]:
Expand Down
21 changes: 0 additions & 21 deletions slack_bolt/context/assistant/async_assistant_utilities.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import warnings
from typing import Optional

from slack_sdk.web.async_client import AsyncWebClient
Expand All @@ -14,8 +13,6 @@
from .internals import has_channel_id_and_thread_ts
from ..get_thread_context.async_get_thread_context import AsyncGetThreadContext
from ..save_thread_context.async_save_thread_context import AsyncSaveThreadContext
from ..set_status.async_set_status import AsyncSetStatus
from ..set_suggested_prompts.async_set_suggested_prompts import AsyncSetSuggestedPrompts
from ..set_title.async_set_title import AsyncSetTitle


Expand Down Expand Up @@ -50,28 +47,10 @@ def __init__(
# When moving this code to Bolt internals, no need to raise an exception for this pattern
raise ValueError(f"Cannot instantiate Assistant for this event pattern ({self.payload})")

def is_valid(self) -> bool:
return self.channel_id is not None and self.thread_ts is not None

@property
def set_status(self) -> AsyncSetStatus:
warnings.warn(
"AsyncAssistantUtilities.set_status is deprecated. "
"Use the set_status argument directly in your listener function "
"or access it via context.set_status instead.",
DeprecationWarning,
stacklevel=2,
)
return AsyncSetStatus(self.client, self.channel_id, self.thread_ts)

@property
def set_title(self) -> AsyncSetTitle:
return AsyncSetTitle(self.client, self.channel_id, self.thread_ts)

@property
def set_suggested_prompts(self) -> AsyncSetSuggestedPrompts:
return AsyncSetSuggestedPrompts(self.client, self.channel_id, self.thread_ts)

@property
def say(self) -> AsyncSay:
return AsyncSay(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
class AsyncSetSuggestedPrompts:
client: AsyncWebClient
channel_id: str
thread_ts: str
thread_ts: Optional[str]

def __init__(
self,
client: AsyncWebClient,
channel_id: str,
thread_ts: str,
thread_ts: Optional[str] = None,
):
self.client = client
self.channel_id = channel_id
Expand All @@ -23,6 +23,7 @@ async def __call__(
self,
prompts: Sequence[Union[str, Dict[str, str]]],
title: Optional[str] = None,
thread_ts: Optional[str] = None,
) -> AsyncSlackResponse:
prompts_arg: List[Dict[str, str]] = []
for prompt in prompts:
Expand All @@ -33,7 +34,7 @@ async def __call__(

return await self.client.assistant_threads_setSuggestedPrompts(
channel_id=self.channel_id,
thread_ts=self.thread_ts,
thread_ts=thread_ts if thread_ts is not None else self.thread_ts,
prompts=prompts_arg,
title=title,
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
class SetSuggestedPrompts:
client: WebClient
channel_id: str
thread_ts: str
thread_ts: Optional[str]

def __init__(
self,
client: WebClient,
channel_id: str,
thread_ts: str,
thread_ts: Optional[str] = None,
):
self.client = client
self.channel_id = channel_id
Expand All @@ -23,6 +23,7 @@ def __call__(
self,
prompts: Sequence[Union[str, Dict[str, str]]],
title: Optional[str] = None,
thread_ts: Optional[str] = None,
) -> SlackResponse:
prompts_arg: List[Dict[str, str]] = []
for prompt in prompts:
Expand All @@ -33,7 +34,7 @@ def __call__(

return self.client.assistant_threads_setSuggestedPrompts(
channel_id=self.channel_id,
thread_ts=self.thread_ts,
thread_ts=thread_ts if thread_ts is not None else self.thread_ts,
prompts=prompts_arg,
title=title,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@
from slack_bolt.context.assistant.thread_context_store.async_store import AsyncAssistantThreadContextStore
from slack_bolt.context.say_stream.async_say_stream import AsyncSayStream
from slack_bolt.context.set_status.async_set_status import AsyncSetStatus
from slack_bolt.context.set_suggested_prompts.async_set_suggested_prompts import AsyncSetSuggestedPrompts
from slack_bolt.middleware.async_middleware import AsyncMiddleware
from slack_bolt.request.async_request import AsyncBoltRequest
from slack_bolt.request.payload_utils import is_assistant_event, to_event
from slack_bolt.request.payload_utils import (
is_app_home_opened_event,
is_assistant_event,
is_assistant_thread_context_changed_event,
is_assistant_thread_started_event,
is_im_message_event,
to_event,
)
from slack_bolt.response import BoltResponse


Expand All @@ -25,32 +33,48 @@ async def async_process(
next: Callable[[], Awaitable[BoltResponse]],
) -> Optional[BoltResponse]:
event = to_event(req.body)
if event is not None:
if is_assistant_event(req.body):
assistant = AsyncAssistantUtilities(
payload=event,
context=req.context,
thread_context_store=self.thread_context_store,
)
req.context["say"] = assistant.say
req.context["set_title"] = assistant.set_title
req.context["set_suggested_prompts"] = assistant.set_suggested_prompts
req.context["get_thread_context"] = assistant.get_thread_context
req.context["save_thread_context"] = assistant.save_thread_context

# TODO: in the future we might want to introduce a "proper" extract_ts utility
thread_ts = req.context.thread_ts or event.get("ts")
if req.context.channel_id and thread_ts:
req.context["set_status"] = AsyncSetStatus(
client=req.context.client,
channel_id=req.context.channel_id,
thread_ts=thread_ts,
)
req.context["say_stream"] = AsyncSayStream(
client=req.context.client,
channel=req.context.channel_id,
recipient_team_id=req.context.team_id or req.context.enterprise_id,
recipient_user_id=req.context.user_id,
thread_ts=thread_ts,
)
if event is None:
return await next()
if req.context.channel_id is None:
return await next()

if is_assistant_event(req.body):
# TODO: eventually we might remove this assistant specific logic
assistant = AsyncAssistantUtilities(
payload=event,
context=req.context,
thread_context_store=self.thread_context_store,
)
req.context["say"] = assistant.say
req.context["set_title"] = assistant.set_title
req.context["get_thread_context"] = assistant.get_thread_context
req.context["save_thread_context"] = assistant.save_thread_context

if (
is_im_message_event(req.body)
or is_assistant_thread_started_event(req.body)
or is_assistant_thread_context_changed_event(req.body)
or is_app_home_opened_event(req.body, tab="messages")
):
req.context["set_suggested_prompts"] = AsyncSetSuggestedPrompts(
client=req.context.client,
channel_id=req.context.channel_id,
thread_ts=req.context.thread_ts,
)

# TODO: in the future we might want to introduce a "proper" extract_ts utility
thread_ts_or_ts = req.context.thread_ts or event.get("ts")
if thread_ts_or_ts:
req.context["set_status"] = AsyncSetStatus(
client=req.context.client,
channel_id=req.context.channel_id,
thread_ts=thread_ts_or_ts,
)
req.context["say_stream"] = AsyncSayStream(
client=req.context.client,
channel=req.context.channel_id,
recipient_team_id=req.context.team_id or req.context.enterprise_id,
recipient_user_id=req.context.user_id,
thread_ts=thread_ts_or_ts,
)
return await next()
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
from typing import Optional, Callable

from slack_bolt.context.assistant.assistant_utilities import AssistantUtilities
from slack_bolt.context.assistant.thread_context_store.store import AssistantThreadContextStore
from slack_bolt.context.say_stream.say_stream import SayStream
from slack_bolt.context.set_status.set_status import SetStatus
from slack_bolt.context.set_suggested_prompts.set_suggested_prompts import SetSuggestedPrompts
from slack_bolt.middleware import Middleware
from slack_bolt.request.payload_utils import is_assistant_event, to_event
from slack_bolt.context.assistant.assistant_utilities import AssistantUtilities
from slack_bolt.request.payload_utils import (
is_app_home_opened_event,
is_assistant_event,
is_assistant_thread_context_changed_event,
is_assistant_thread_started_event,
is_im_message_event,
to_event,
)
from slack_bolt.request.request import BoltRequest
from slack_bolt.response.response import BoltResponse

Expand All @@ -19,32 +27,48 @@ def __init__(self, thread_context_store: Optional[AssistantThreadContextStore] =

def process(self, *, req: BoltRequest, resp: BoltResponse, next: Callable[[], BoltResponse]) -> Optional[BoltResponse]:
event = to_event(req.body)
if event is not None:
if is_assistant_event(req.body):
assistant = AssistantUtilities(
payload=event,
context=req.context,
thread_context_store=self.thread_context_store,
)
req.context["say"] = assistant.say
req.context["set_title"] = assistant.set_title
req.context["set_suggested_prompts"] = assistant.set_suggested_prompts
req.context["get_thread_context"] = assistant.get_thread_context
req.context["save_thread_context"] = assistant.save_thread_context

# TODO: in the future we might want to introduce a "proper" extract_ts utility
thread_ts = req.context.thread_ts or event.get("ts")
if req.context.channel_id and thread_ts:
req.context["set_status"] = SetStatus(
client=req.context.client,
channel_id=req.context.channel_id,
thread_ts=thread_ts,
)
req.context["say_stream"] = SayStream(
client=req.context.client,
channel=req.context.channel_id,
recipient_team_id=req.context.team_id or req.context.enterprise_id,
recipient_user_id=req.context.user_id,
thread_ts=thread_ts,
)
if event is None:
return next()
if req.context.channel_id is None:
return next()

if is_assistant_event(req.body):
# TODO: eventually we might remove this assistant specific logic
assistant = AssistantUtilities(
payload=event,
context=req.context,
thread_context_store=self.thread_context_store,
)
req.context["say"] = assistant.say
req.context["set_title"] = assistant.set_title
req.context["get_thread_context"] = assistant.get_thread_context
req.context["save_thread_context"] = assistant.save_thread_context

if (
is_im_message_event(req.body)
or is_assistant_thread_started_event(req.body)
or is_assistant_thread_context_changed_event(req.body)
or is_app_home_opened_event(req.body, tab="messages")
):
req.context["set_suggested_prompts"] = SetSuggestedPrompts(
client=req.context.client,
channel_id=req.context.channel_id,
thread_ts=req.context.thread_ts,
)

# TODO: in the future we might want to introduce a "proper" extract_ts utility
thread_ts_or_ts = req.context.thread_ts or event.get("ts")
if thread_ts_or_ts:
req.context["set_status"] = SetStatus(
client=req.context.client,
channel_id=req.context.channel_id,
thread_ts=thread_ts_or_ts,
)
req.context["say_stream"] = SayStream(
client=req.context.client,
channel=req.context.channel_id,
recipient_team_id=req.context.team_id or req.context.enterprise_id,
recipient_user_id=req.context.user_id,
thread_ts=thread_ts_or_ts,
)
return next()
Loading