Skip to content

Commit 57a133c

Browse files
author
Song Meo
committed
Add emoji reaction support: React with ✏️ (pencil2) to trigger DraftResponseAgent
1 parent 0737d23 commit 57a133c

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

src/bro/connector/slack.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,47 @@ def _process_message(self, client: BaseSocketModeClient, req: SocketModeRequest)
135135
)
136136
)
137137
return None
138+
139+
case ("events_api", "reaction_added"):
140+
response = SocketModeResponse(envelope_id=req.envelope_id)
141+
client.send_socket_mode_response(response)
142+
_logger.info(f"Received reaction_added event: {event}")
143+
144+
# Check if it's the draft emoji (pencil2 ✏️)
145+
reaction = event.get("reaction")
146+
if reaction == "pencil2":
147+
# Get the message that was reacted to
148+
message_ts = event.get("item", {}).get("ts")
149+
channel_id = event.get("item", {}).get("channel")
150+
151+
if message_ts and channel_id:
152+
try:
153+
# Fetch the original message
154+
result = self._web_client.conversations_history(
155+
channel=channel_id, latest=message_ts, inclusive=True, limit=1
156+
)
157+
158+
if result["messages"]:
159+
message_text = result["messages"][0].get("text", "")
160+
_logger.info(f"Pencil reaction on message: {message_text[:100]}")
161+
162+
# Create a synthetic message to trigger draft response
163+
user_info = self._web_client.users_info(user=user_id)["user"]
164+
user_name = user_info["name"]
165+
166+
self._unread_msgs.append(
167+
ReceivedMessage(
168+
via=Channel(name=channel_id),
169+
user=User(name=user_name),
170+
text=f"[DRAFT_RESPONSE_REQUEST]\n{message_text}",
171+
attachments=[],
172+
thread_ts=message_ts, # Reply in the same thread
173+
)
174+
)
175+
except Exception as e:
176+
_logger.error(f"Failed to handle reaction: {e}")
177+
return None
178+
138179
return None
139180

140181
def list_channels(self) -> list[Channel]:

src/bro/conversation.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,45 @@ def spin(self) -> bool:
562562
self._current_thread_ts = msg_thread_ts
563563

564564
try:
565+
# Check if this is a draft response request triggered by emoji reaction
566+
if msg.text.startswith("[DRAFT_RESPONSE_REQUEST]"):
567+
_logger.info("Draft response request detected from emoji reaction")
568+
# Extract the inquiry context from the message
569+
inquiry_text = msg.text.replace("[DRAFT_RESPONSE_REQUEST]\n", "").strip()
570+
571+
# Trigger DraftResponseAgent directly
572+
if hasattr(self._reasoner, "_draft_response_agent") and self._reasoner._draft_response_agent:
573+
try:
574+
_logger.info(f"Running DraftResponseAgent for emoji reaction")
575+
draft_result = self._reasoner._draft_response_agent.run(inquiry_text)
576+
577+
# Post draft as response in the thread
578+
response_data = textwrap.dedent(
579+
f"""\
580+
via: {msg.via.name!r}
581+
user: "Bro"
582+
attachments: []
583+
---
584+
{draft_result}
585+
"""
586+
)
587+
self._context += [
588+
{
589+
"role": "user",
590+
"content": [
591+
{"type": "input_text", "text": response_data},
592+
],
593+
},
594+
]
595+
conversation_response = self._request_inference(self._context)
596+
output = conversation_response["output"]
597+
if output:
598+
self._process_response_output(output)
599+
except Exception as e:
600+
_logger.error(f"DraftResponseAgent failed: {e}")
601+
# Skip normal processing for this message
602+
continue
603+
565604
input_data = textwrap.dedent(
566605
f"""\
567606
via: {msg.via.name!r}

0 commit comments

Comments
 (0)