|
1 | 1 | import logging |
2 | | -from typing import List, Dict |
| 2 | +from typing import Any, List, Dict |
3 | 3 | from slack_bolt import Assistant, BoltContext, Say, SetSuggestedPrompts |
4 | 4 | from slack_bolt.context.get_thread_context import GetThreadContext |
5 | 5 | from slack_sdk import WebClient |
|
10 | 10 | assistant = Assistant() |
11 | 11 |
|
12 | 12 |
|
| 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 | + |
13 | 42 | # This listener is invoked when a human user opened an assistant thread |
14 | 43 | @assistant.thread_started |
15 | 44 | def start_assistant_thread( |
@@ -60,6 +89,7 @@ def respond_in_assistant_thread( |
60 | 89 | say: Say, |
61 | 90 | ): |
62 | 91 | try: |
| 92 | + user_id = payload["user"] |
63 | 93 | channel_id = payload["channel"] |
64 | 94 | thread_ts = payload["thread_ts"] |
65 | 95 |
|
@@ -97,12 +127,39 @@ def respond_in_assistant_thread( |
97 | 127 | client.chat_appendStream(channel=channel_id, ts=stream_ts, markdown_text=f"{event.delta}") |
98 | 128 | else: |
99 | 129 | 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) |
105 | 132 |
|
106 | 133 | except Exception as e: |
107 | 134 | logger.exception(f"Failed to handle a user message event: {e}") |
108 | 135 | 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