33from slack_bolt import Assistant , BoltContext , Say , SetSuggestedPrompts
44from slack_bolt .context .get_thread_context import GetThreadContext
55from slack_sdk import WebClient
6- from slack_sdk .models . blocks import Block , ContextActionsBlock , FeedbackButtonsElement , FeedbackButtonObject
6+ from slack_sdk .errors import SlackApiError
77
8+ from ..views .feedback_block import create_feedback_block
89from ..llm_caller import call_llm
10+ from ..listeners_constants import loading_messages
11+
912
1013# Refer to https://tools.slack.dev/bolt-python/concepts/assistant/ for more details
1114assistant = Assistant ()
1215
1316
14- def create_feedback_block () -> List [Block ]:
15- """
16- Create feedback block with thumbs up/down buttons
17-
18- Returns:
19- Block Kit context_actions block
20- """
21- blocks : List [Block ] = [
22- ContextActionsBlock (
23- elements = [
24- FeedbackButtonsElement (
25- action_id = "feedback" ,
26- positive_button = FeedbackButtonObject (
27- text = "Good Response" ,
28- accessibility_label = "Submit positive feedback on this response" ,
29- value = "good-feedback" ,
30- ),
31- negative_button = FeedbackButtonObject (
32- text = "Bad Response" ,
33- accessibility_label = "Submit negative feedback on this response" ,
34- value = "bad-feedback" ,
35- ),
36- )
37- ]
38- )
39- ]
40- return blocks
41-
42-
4317# This listener is invoked when a human user opened an assistant thread
4418@assistant .thread_started
4519def start_assistant_thread (
@@ -86,31 +60,49 @@ def respond_in_assistant_thread(
8660 payload : dict ,
8761 logger : logging .Logger ,
8862 context : BoltContext ,
63+ get_thread_context : GetThreadContext ,
8964 client : WebClient ,
9065 say : Say ,
9166):
9267 try :
9368 channel_id = payload ["channel" ]
69+ team_id = payload ["team" ]
9470 thread_ts = payload ["thread_ts" ]
95-
96- loading_messages = [
97- "Teaching the hamsters to type faster…" ,
98- "Untangling the internet cables…" ,
99- "Consulting the office goldfish…" ,
100- "Polishing up the response just for you…" ,
101- "Convincing the AI to stop overthinking…" ,
102- ]
103-
104- replies = client .conversations_replies (
105- channel = context .channel_id ,
106- ts = context .thread_ts ,
107- oldest = context .thread_ts ,
108- limit = 10 ,
109- )
110- messages_in_thread : List [Dict [str , str ]] = []
111- for message in replies ["messages" ]:
112- role = "user" if message .get ("bot_id" ) is None else "assistant"
113- messages_in_thread .append ({"role" : role , "content" : message ["text" ]})
71+ user_id = payload ["user" ]
72+ user_message = payload ["text" ]
73+
74+ if user_message == "Can you generate a brief summary of the referred channel?" :
75+ # the logic here requires the additional bot scopes:
76+ # channels:join, channels:history, groups:history
77+ thread_context = get_thread_context ()
78+ referred_channel_id = thread_context .get ("channel_id" )
79+ try :
80+ channel_history = client .conversations_history (channel = referred_channel_id , limit = 50 )
81+ except SlackApiError as e :
82+ if e .response ["error" ] == "not_in_channel" :
83+ # If this app's bot user is not in the public channel,
84+ # we'll try joining the channel and then calling the same API again
85+ client .conversations_join (channel = referred_channel_id )
86+ channel_history = client .conversations_history (channel = referred_channel_id , limit = 50 )
87+ else :
88+ raise e
89+ prompt = f"Can you generate a brief summary of these messages in a Slack channel <#{ referred_channel_id } >?\n \n "
90+ for message in reversed (channel_history .get ("messages" )):
91+ if message .get ("user" ) is not None :
92+ prompt += f"\n <@{ message ['user' ]} > says: { message ['text' ]} \n "
93+ messages_in_thread = [{"role" : "user" , "content" : prompt }]
94+
95+ else :
96+ replies = client .conversations_replies (
97+ channel = context .channel_id ,
98+ ts = context .thread_ts ,
99+ oldest = context .thread_ts ,
100+ limit = 10 ,
101+ )
102+ messages_in_thread : List [Dict [str , str ]] = []
103+ for message in replies ["messages" ]:
104+ role = "user" if message .get ("bot_id" ) is None else "assistant"
105+ messages_in_thread .append ({"role" : role , "content" : message ["text" ]})
114106
115107 returned_message = call_llm (messages_in_thread )
116108
@@ -120,6 +112,8 @@ def respond_in_assistant_thread(
120112
121113 stream_response = client .chat_startStream (
122114 channel = channel_id ,
115+ recipient_team_id = team_id ,
116+ recipient_user_id = user_id ,
123117 thread_ts = thread_ts ,
124118 )
125119 stream_ts = stream_response ["ts" ]
0 commit comments