Skip to content

Commit 0ed27ce

Browse files
committed
have a common refactor of code
1 parent 519b0dd commit 0ed27ce

File tree

2 files changed

+19
-47
lines changed

2 files changed

+19
-47
lines changed

packages/slackBotFunction/app/slack/slack_handlers.py

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -109,28 +109,25 @@ def mention_handler(event: Dict[str, Any], ack: Ack, body: Dict[str, Any], clien
109109
- Otherwise, forward to the async processing pipeline (Q&A).
110110
"""
111111
bot_token = get_bot_token()
112-
logger.debug("Sending ack response")
112+
logger.debug("Sending ack response in mention_handler")
113113
ack()
114114
respond_with_eyes(bot_token, event)
115115
event_id = _gate_common(event, body)
116116
if not event_id:
117117
return
118118
original_message_text = (event.get("text") or "").strip()
119-
channel_id = event["channel"]
120119
user_id = event.get("user", "unknown")
121120
conversation_key, thread_root = _conversation_key_and_root(event)
122121

123122
message_text = _strip_mentions(original_message_text)
123+
logger.info(f"Processing @mention from user {user_id}", extra={"event_id": event_id})
124124
_common_message_handler(
125125
message_text=message_text,
126126
conversation_key=conversation_key,
127-
user_id=user_id,
128-
channel_id=channel_id,
129127
thread_root=thread_root,
130128
client=client,
131129
event=event,
132130
event_id=event_id,
133-
bot_token=bot_token,
134131
post_to_thread=True,
135132
)
136133

@@ -143,21 +140,17 @@ def dm_message_handler(event: Dict[str, Any], event_id, client: WebClient):
143140
"""
144141
if event.get("channel_type") != constants.CHANNEL_TYPE_IM:
145142
return # not a DM; the channel handler will evaluate it
146-
bot_token = get_bot_token()
147143
message_text = (event.get("text") or "").strip()
148-
channel_id = event["channel"]
149144
user_id = event.get("user", "unknown")
150145
conversation_key, thread_root = _conversation_key_and_root(event)
146+
logger.info(f"Processing DM from user {user_id}", extra={"event_id": event_id})
151147
_common_message_handler(
152148
message_text=message_text,
153149
conversation_key=conversation_key,
154-
user_id=user_id,
155-
channel_id=channel_id,
156150
thread_root=thread_root,
157151
client=client,
158152
event=event,
159153
event_id=event_id,
160-
bot_token=bot_token,
161154
post_to_thread=False,
162155
)
163156

@@ -173,9 +166,10 @@ def thread_message_handler(event: Dict[str, Any], event_id, client: WebClient):
173166
if event.get("channel_type") == constants.CHANNEL_TYPE_IM:
174167
return # handled in the DM handler
175168

176-
text = (event.get("text") or "").strip()
169+
message_text = (event.get("text") or "").strip()
177170
channel_id = event["channel"]
178171
thread_root = event.get("thread_ts")
172+
user_id = event.get("user", "unknown")
179173
if not thread_root:
180174
return # top-level message; require @mention to start
181175

@@ -190,36 +184,16 @@ def thread_message_handler(event: Dict[str, Any], event_id, client: WebClient):
190184
logger.error(f"Error checking thread session: {e}", extra={"error": traceback.format_exc()})
191185
return
192186

193-
if text.lower().startswith(constants.FEEDBACK_PREFIX):
194-
feedback_text = text.split(":", 1)[1].strip() if ":" in text else ""
195-
user_id = event.get("user", "unknown")
196-
try:
197-
store_feedback(
198-
conversation_key=conversation_key,
199-
feedback_type="additional",
200-
user_id=user_id,
201-
channel_id=channel_id,
202-
thread_ts=thread_root,
203-
message_ts=None,
204-
feedback_text=feedback_text,
205-
client=client,
206-
)
207-
except Exception as e:
208-
logger.error(f"Failed to store channel additional feedback: {e}", extra={"error": traceback.format_exc()})
209-
210-
try:
211-
client.chat_postMessage(
212-
channel=channel_id,
213-
text=BOT_MESSAGES["feedback_thanks"],
214-
thread_ts=thread_root,
215-
)
216-
except Exception as e:
217-
logger.error(f"Failed to post channel feedback ack: {e}", extra={"error": traceback.format_exc()})
218-
return
219-
220-
# Follow-up in a bot-owned thread (no re-mention required)
221-
bot_token = get_bot_token()
222-
trigger_async_processing({"event": event, "event_id": event_id, "bot_token": bot_token})
187+
logger.info(f"Processing thread message from user {user_id}", extra={"event_id": event_id})
188+
_common_message_handler(
189+
message_text=message_text,
190+
conversation_key=conversation_key,
191+
thread_root=thread_root,
192+
client=client,
193+
event=event,
194+
event_id=event_id,
195+
post_to_thread=True,
196+
)
223197

224198

225199
def unified_message_handler(event: Dict[str, Any], ack: Ack, body: Dict[str, Any], client: WebClient):
@@ -330,15 +304,15 @@ def _is_latest_message(conversation_key, message_ts):
330304
def _common_message_handler(
331305
message_text: str,
332306
conversation_key: str,
333-
user_id: str,
334-
channel_id: str,
335307
thread_root: str,
336308
client: WebClient,
337309
event: Dict[str, Any],
338310
event_id,
339-
bot_token,
340311
post_to_thread: bool,
341312
):
313+
channel_id = event["channel"]
314+
user_id = event.get("user", "unknown")
315+
bot_token = get_bot_token()
342316
if message_text.lower().startswith(constants.FEEDBACK_PREFIX):
343317
feedback_text = message_text.split(":", 1)[1].strip() if ":" in message_text else ""
344318
try:
@@ -376,6 +350,4 @@ def _common_message_handler(
376350
logger.error(f"Can not find pull request details: {e}", extra={"error": traceback.format_exc()})
377351
return
378352

379-
# Normal mention -> async processing
380-
logger.info(f"Processing @mention from user {user_id}", extra={"event_id": event_id})
381353
trigger_async_processing({"event": event, "event_id": event_id, "bot_token": bot_token})

packages/slackBotFunction/app/utils/handler_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,5 @@ def trigger_pull_request_processing(pull_request_id: str, event: Dict[str, Any])
8787
)
8888
logger.info("Triggered pull request lambda", extra={"lambda_arn": pull_request_lambda_arn})
8989
except Exception as e:
90-
logger.error("Failed to get cloudformation output", extra={"error": traceback.format_exc()})
90+
logger.error("Failed to trigger pull request lambda", extra={"error": traceback.format_exc()})
9191
raise e

0 commit comments

Comments
 (0)