Skip to content

Commit 9a2df8d

Browse files
committed
refactor: move feedback action to standalone path
1 parent 94e0d29 commit 9a2df8d

5 files changed

Lines changed: 50 additions & 44 deletions

File tree

listeners/__init__.py

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

44

55
def register_listeners(app):
6-
# Using assistant middleware is the recommended way.
7-
app.assistant(assistant)
8-
app.action("feedback")(handle_feedback)
6+
7+
actions.register(app)
8+
assistant.register(app)
99

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

listeners/actions/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from slack_bolt import App
2+
from .actions import handle_feedback
3+
4+
5+
def register(app: App):
6+
app.action("feedback")(handle_feedback)

listeners/actions/actions.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import logging
2+
3+
4+
# Handle feedback buttons (thumbs up/down)
5+
def handle_feedback(ack, body, client, logger: logging.Logger):
6+
try:
7+
ack()
8+
message_ts = body["message"]["ts"]
9+
channel_id = body["channel"]["id"]
10+
feedback_type = body["actions"][0]["value"]
11+
is_positive = feedback_type == "good-feedback"
12+
13+
if is_positive:
14+
client.chat_postEphemeral(
15+
channel=channel_id,
16+
user=body["user"]["id"],
17+
thread_ts=message_ts,
18+
text="We're glad you found this useful.",
19+
)
20+
else:
21+
client.chat_postEphemeral(
22+
channel=channel_id,
23+
user=body["user"]["id"],
24+
thread_ts=message_ts,
25+
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.",
26+
)
27+
28+
logger.debug(f"Handled feedback: type={feedback_type}, message_ts={message_ts}")
29+
except Exception as error:
30+
logger.error(f":warning: Something went wrong! {error}")

listeners/assistant/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from slack_bolt import App
12
from .assistant import assistant
23

3-
__all__ = ["assistant"]
4+
5+
def register(app: App):
6+
app.assistant(assistant)

listeners/assistant/assistant.py

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,22 @@
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
6+
from slack_sdk.models.blocks import Block, ContextActionsBlock, FeedbackButtonsElement, FeedbackButtonObject
77

88
from ..llm_caller import call_llm
99

1010
# Refer to https://tools.slack.dev/bolt-python/concepts/assistant/ for more details
1111
assistant = Assistant()
1212

1313

14-
def create_feedback_block(user_id: str) -> ContextActionsBlock:
14+
def create_feedback_block() -> List[Block]:
1515
"""
1616
Create feedback block with thumbs up/down buttons
1717
18-
Args:
19-
user_id: User ID for user-specific controls
20-
2118
Returns:
2219
Block Kit context_actions block
2320
"""
24-
block = [
21+
blocks: List[Block] = [
2522
ContextActionsBlock(
2623
elements=[
2724
FeedbackButtonsElement(
@@ -40,7 +37,7 @@ def create_feedback_block(user_id: str) -> ContextActionsBlock:
4037
]
4138
)
4239
]
43-
return block
40+
return blocks
4441

4542

4643
# This listener is invoked when a human user opened an assistant thread
@@ -93,7 +90,6 @@ def respond_in_assistant_thread(
9390
say: Say,
9491
):
9592
try:
96-
user_id = payload["user"]
9793
channel_id = payload["channel"]
9894
thread_ts = payload["thread_ts"]
9995

@@ -135,38 +131,9 @@ def respond_in_assistant_thread(
135131
else:
136132
continue
137133

138-
feedback_block = create_feedback_block(user_id=user_id)
134+
feedback_block = create_feedback_block()
139135
client.chat_stopStream(channel=channel_id, ts=stream_ts, blocks=feedback_block)
140136

141137
except Exception as e:
142138
logger.exception(f"Failed to handle a user message event: {e}")
143139
say(f":warning: Something went wrong! ({e})")
144-
145-
146-
# Handle feedback buttons (thumbs up/down)
147-
def handle_feedback(ack, body, client, logger):
148-
ack()
149-
try:
150-
message_ts = body["message"]["ts"]
151-
channel_id = body["channel"]["id"]
152-
feedback_type = body["actions"][0]["value"]
153-
is_positive = feedback_type == "good-feedback"
154-
155-
if is_positive:
156-
client.chat_postEphemeral(
157-
channel=channel_id,
158-
user=body["user"]["id"],
159-
thread_ts=message_ts,
160-
text="We're glad you found this useful.",
161-
)
162-
else:
163-
client.chat_postEphemeral(
164-
channel=channel_id,
165-
user=body["user"]["id"],
166-
thread_ts=message_ts,
167-
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.",
168-
)
169-
170-
logger.debug(f"Handled feedback: type={feedback_type}, message_ts={message_ts}")
171-
except Exception as error:
172-
logger.error(f":warning: Something went wrong! {error}")

0 commit comments

Comments
 (0)