Skip to content

Commit 66b5404

Browse files
committed
feat: feedback block
1 parent b4b2539 commit 66b5404

2 files changed

Lines changed: 65 additions & 6 deletions

File tree

listeners/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from listeners.assistant import assistant
2+
from listeners.assistant.assistant import handle_feedback
23

34

45
def register_listeners(app):
56
# Using assistant middleware is the recommended way.
67
app.assistant(assistant)
8+
app.action("feedback")(handle_feedback)
79

810
# The following event listeners demonstrate how to implement the same on your own.
911
# from listeners import events

listeners/assistant/assistant.py

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import List, Dict
2+
from typing import Any, 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
@@ -10,6 +10,35 @@
1010
assistant = Assistant()
1111

1212

13+
def create_feedback_block(user_id: str) -> Dict[str, Any]:
14+
"""
15+
Create feedback block with thumbs up/down buttons
16+
17+
Args:
18+
user_id: User ID for user-specific controls
19+
20+
Returns:
21+
Block Kit context_actions block
22+
"""
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+
}
38+
]
39+
return [{"type": "context_actions", "elements": elements}]
40+
41+
1342
# This listener is invoked when a human user opened an assistant thread
1443
@assistant.thread_started
1544
def start_assistant_thread(
@@ -60,6 +89,7 @@ def respond_in_assistant_thread(
6089
say: Say,
6190
):
6291
try:
92+
user_id = payload["user"]
6393
channel_id = payload["channel"]
6494
thread_ts = payload["thread_ts"]
6595

@@ -97,12 +127,39 @@ def respond_in_assistant_thread(
97127
client.chat_appendStream(channel=channel_id, ts=stream_ts, markdown_text=f"{event.delta}")
98128
else:
99129
continue
100-
101-
client.chat_stopStream(
102-
channel=channel_id,
103-
ts=stream_ts,
104-
)
130+
feedback_block = create_feedback_block(user_id=user_id)
131+
client.chat_stopStream(channel=channel_id, ts=stream_ts, blocks=feedback_block)
105132

106133
except Exception as e:
107134
logger.exception(f"Failed to handle a user message event: {e}")
108135
say(f":warning: Something went wrong! ({e})")
136+
137+
138+
# Handle feedback buttons (thumbs up/down)
139+
def handle_feedback(ack, body, client, logger):
140+
ack()
141+
142+
try:
143+
message_ts = body["message"]["ts"]
144+
channel_id = body["channel"]["id"]
145+
feedback_type = body["actions"][0]["value"]
146+
is_positive = feedback_type == "good-feedback"
147+
148+
if is_positive:
149+
client.chat_postEphemeral(
150+
channel=channel_id,
151+
user=body["user"]["id"],
152+
thread_ts=message_ts,
153+
text="We're glad you found this useful.",
154+
)
155+
else:
156+
client.chat_postEphemeral(
157+
channel=channel_id,
158+
user=body["user"]["id"],
159+
thread_ts=message_ts,
160+
text="Sorry to hear that response wasn't up to par :slightly_frowning_face: Starting a new chat may help with AI mistakes and hallucinations.",
161+
)
162+
163+
logger.debug(f"Handled feedback: type={feedback_type}, message_ts={message_ts}")
164+
except Exception as error:
165+
logger.error(f"Error handling feedback action: {error}")

0 commit comments

Comments
 (0)