Skip to content

Commit a4309b4

Browse files
committed
feat: update feedback block using FeedbackButtonsElement
1 parent bd2e837 commit a4309b4

1 file changed

Lines changed: 27 additions & 20 deletions

File tree

listeners/assistant/assistant.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import logging
2-
from typing import Any, List, Dict
2+
from typing import List, Dict
33
from slack_bolt import Assistant, BoltContext, Say, SetSuggestedPrompts
44
from slack_bolt.context.get_thread_context import GetThreadContext
55
from slack_sdk import WebClient
6+
from slack_sdk.models.blocks import FeedbackButtonsElement, FeedbackButtonObject, ContextActionsBlock
67

78
from ..llm_caller import call_llm
89

910
# Refer to https://tools.slack.dev/bolt-python/concepts/assistant/ for more details
1011
assistant = Assistant()
1112

1213

13-
def create_feedback_block(user_id: str) -> Dict[str, Any]:
14+
def create_feedback_block(user_id: str) -> ContextActionsBlock:
1415
"""
1516
Create feedback block with thumbs up/down buttons
1617
@@ -20,23 +21,26 @@ def create_feedback_block(user_id: str) -> Dict[str, Any]:
2021
Returns:
2122
Block Kit context_actions block
2223
"""
23-
elements = [
24-
{
25-
"type": "feedback_buttons",
26-
"action_id": "feedback",
27-
"positive_button": {
28-
"text": {"type": "plain_text", "text": "Good Response"},
29-
"accessibility_label": "Submit positive feedback on this response",
30-
"value": "good-feedback",
31-
},
32-
"negative_button": {
33-
"text": {"type": "plain_text", "text": "Bad Response"},
34-
"accessibility_label": "Submit negative feedback on this response",
35-
"value": "bad-feedback",
36-
},
37-
}
24+
block = [
25+
ContextActionsBlock(
26+
elements=[
27+
FeedbackButtonsElement(
28+
action_id="feedback",
29+
positive_button=FeedbackButtonObject(
30+
text="Good Response",
31+
accessibility_label="Submit positive feedback on this response",
32+
value="good-feedback",
33+
),
34+
negative_button=FeedbackButtonObject(
35+
text="Bad Response",
36+
accessibility_label="Submit negative feedback on this response",
37+
value="bad-feedback",
38+
),
39+
)
40+
]
41+
)
3842
]
39-
return [{"type": "context_actions", "elements": elements}]
43+
return block
4044

4145

4246
# This listener is invoked when a human user opened an assistant thread
@@ -113,20 +117,24 @@ def respond_in_assistant_thread(
113117
messages_in_thread.append({"role": role, "content": message["text"]})
114118

115119
returned_message = call_llm(messages_in_thread)
120+
116121
client.assistant_threads_setStatus(
117122
channel_id=channel_id, thread_ts=thread_ts, status="Bolt is typing", loading_messages=loading_messages
118123
)
124+
119125
stream_response = client.chat_startStream(
120126
channel=channel_id,
121127
thread_ts=thread_ts,
122128
)
123129
stream_ts = stream_response["ts"]
130+
124131
# use of this for loop is specific to openai response method
125132
for event in returned_message:
126133
if event.type == "response.output_text.delta":
127134
client.chat_appendStream(channel=channel_id, ts=stream_ts, markdown_text=f"{event.delta}")
128135
else:
129136
continue
137+
130138
feedback_block = create_feedback_block(user_id=user_id)
131139
client.chat_stopStream(channel=channel_id, ts=stream_ts, blocks=feedback_block)
132140

@@ -138,7 +146,6 @@ def respond_in_assistant_thread(
138146
# Handle feedback buttons (thumbs up/down)
139147
def handle_feedback(ack, body, client, logger):
140148
ack()
141-
142149
try:
143150
message_ts = body["message"]["ts"]
144151
channel_id = body["channel"]["id"]
@@ -162,4 +169,4 @@ def handle_feedback(ack, body, client, logger):
162169

163170
logger.debug(f"Handled feedback: type={feedback_type}, message_ts={message_ts}")
164171
except Exception as error:
165-
logger.error(f"Error handling feedback action: {error}")
172+
logger.error(f":warning: Something went wrong! {error}")

0 commit comments

Comments
 (0)