-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathassistant_utilities.py
More file actions
93 lines (78 loc) · 3.43 KB
/
assistant_utilities.py
File metadata and controls
93 lines (78 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import warnings
from typing import Optional
from slack_sdk.web import WebClient
from slack_bolt.context.assistant.thread_context_store.store import AssistantThreadContextStore
from slack_bolt.context.assistant.thread_context_store.default_store import DefaultAssistantThreadContextStore
from slack_bolt.context.context import BoltContext
from slack_bolt.context.say import Say
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
class AssistantUtilities:
payload: dict
client: WebClient
channel_id: str
thread_ts: str
thread_context_store: AssistantThreadContextStore
def __init__(
self,
*,
payload: dict,
context: BoltContext,
thread_context_store: Optional[AssistantThreadContextStore] = None,
):
self.payload = payload
self.client = context.client
self.thread_context_store = thread_context_store or DefaultAssistantThreadContextStore(context)
if has_channel_id_and_thread_ts(self.payload):
# assistant_thread_started
thread = self.payload["assistant_thread"]
self.channel_id = thread["channel_id"]
self.thread_ts = thread["thread_ts"]
elif self.payload.get("channel") is not None and self.payload.get("thread_ts") is not None:
# message event
self.channel_id = self.payload["channel"]
self.thread_ts = self.payload["thread_ts"]
else:
# 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]:
thread_context = self.get_thread_context()
if thread_context is not None:
return {"event_type": "assistant_thread_context", "event_payload": thread_context}
return None
return Say(
self.client,
channel=self.channel_id,
thread_ts=self.thread_ts,
build_metadata=build_metadata,
)
@property
def get_thread_context(self) -> GetThreadContext:
return GetThreadContext(self.thread_context_store, self.channel_id, self.thread_ts, self.payload)
@property
def save_thread_context(self) -> SaveThreadContext:
return SaveThreadContext(self.thread_context_store, self.channel_id, self.thread_ts)