@@ -112,7 +112,7 @@ def app_mention_handler(event, ack, body, client):
112112 try :
113113 client .chat_postMessage (
114114 channel = channel_id ,
115- text = BOT_MESSAGES . get ( "feedback_thanks" , "Thank you for your feedback." ) ,
115+ text = BOT_MESSAGES [ "feedback_thanks" ] ,
116116 thread_ts = thread_root ,
117117 )
118118 except Exception as e :
@@ -124,18 +124,14 @@ def app_mention_handler(event, ack, body, client):
124124 trigger_async_processing ({"event" : event , "event_id" : event_id , "bot_token" : bot_token })
125125
126126
127- def dm_message_handler (event , ack , body , client ):
127+ def dm_message_handler (event , event_id , body , client ):
128128 """
129129 Direct messages:
130130 - 'feedback:' prefix -> store as conversation-scoped additional feedback (no model call).
131131 - otherwise -> forward to async processing (Q&A).
132132 """
133133 if event .get ("channel_type" ) != "im" :
134134 return # not a DM; the channel handler will evaluate it
135- ack ()
136- event_id = _gate_common (event , body )
137- if not event_id :
138- return
139135
140136 text = (event .get ("text" ) or "" ).strip ()
141137 channel_id = event ["channel" ]
@@ -161,7 +157,7 @@ def dm_message_handler(event, ack, body, client):
161157 try :
162158 client .chat_postMessage (
163159 channel = channel_id ,
164- text = BOT_MESSAGES . get ( "feedback_thanks" , "Thank you for your feedback." ) ,
160+ text = BOT_MESSAGES [ "feedback_thanks" ] ,
165161 thread_ts = thread_root ,
166162 )
167163 except Exception as e :
@@ -172,7 +168,7 @@ def dm_message_handler(event, ack, body, client):
172168 trigger_async_processing ({"event" : event , "event_id" : event_id , "bot_token" : bot_token })
173169
174170
175- def channel_message_handler (event , ack , body , client ):
171+ def channel_message_handler (event , event_id , body , client ):
176172 """
177173 Channel messages:
178174 - Ignore top-level messages (policy: require @mention to start).
@@ -182,10 +178,6 @@ def channel_message_handler(event, ack, body, client):
182178 """
183179 if event .get ("channel_type" ) == "im" :
184180 return # handled in the DM handler
185- ack ()
186- event_id = _gate_common (event , body )
187- if not event_id :
188- return
189181
190182 text = (event .get ("text" ) or "" ).strip ()
191183 channel_id = event ["channel" ]
@@ -197,7 +189,9 @@ def channel_message_handler(event, ack, body, client):
197189 try :
198190 resp = table .get_item (Key = {"pk" : conversation_key , "sk" : "session" })
199191 if "Item" not in resp :
192+ logger .info (f"No session found for thread: { conversation_key } " )
200193 return # not a bot-owned thread; ignore
194+ logger .info (f"Found session for thread: { conversation_key } " )
201195 except Exception as e :
202196 logger .error (f"Error checking thread session: { e } " )
203197 return
@@ -222,7 +216,7 @@ def channel_message_handler(event, ack, body, client):
222216 try :
223217 client .chat_postMessage (
224218 channel = channel_id ,
225- text = BOT_MESSAGES . get ( "feedback_thanks" , "Thank you for your feedback." ) ,
219+ text = BOT_MESSAGES [ "feedback_thanks" ] ,
226220 thread_ts = thread_root ,
227221 )
228222 except Exception as e :
@@ -233,6 +227,68 @@ def channel_message_handler(event, ack, body, client):
233227 trigger_async_processing ({"event" : event , "event_id" : event_id , "bot_token" : bot_token })
234228
235229
230+ def unified_message_handler (event , ack , body , client ):
231+ """Handle all message events - DMs and channel messages"""
232+ ack ()
233+ event_id = _gate_common (event , body )
234+ if not event_id :
235+ return
236+
237+ # Route to appropriate handler based on message type
238+ if event .get ("channel_type" ) == "im" :
239+ # DM handling
240+ dm_message_handler (event , event_id , body , client )
241+ else :
242+ # Channel message handling
243+ channel_message_handler (event , event_id , body , client )
244+
245+
246+ def feedback_yes_handler (ack , body , client ):
247+ """Handle positive feedback button clicks."""
248+ ack ()
249+ try :
250+ feedback_data = json .loads (body ["actions" ][0 ]["value" ])
251+ store_feedback (
252+ feedback_data ["ck" ],
253+ None ,
254+ "positive" ,
255+ body ["user" ]["id" ],
256+ feedback_data ["ch" ],
257+ feedback_data .get ("tt" ),
258+ feedback_data .get ("mt" ),
259+ )
260+ client .chat_postMessage (
261+ channel = feedback_data ["ch" ],
262+ text = BOT_MESSAGES ["feedback_positive_thanks" ],
263+ thread_ts = feedback_data .get ("tt" ),
264+ )
265+ except Exception as e :
266+ logger .error (f"Error handling positive feedback: { e } " )
267+
268+
269+ def feedback_no_handler (ack , body , client ):
270+ """Handle negative feedback button clicks."""
271+ ack ()
272+ try :
273+ feedback_data = json .loads (body ["actions" ][0 ]["value" ])
274+ store_feedback (
275+ feedback_data ["ck" ],
276+ None ,
277+ "negative" ,
278+ body ["user" ]["id" ],
279+ feedback_data ["ch" ],
280+ feedback_data .get ("tt" ),
281+ feedback_data .get ("mt" ),
282+ )
283+ client .chat_postMessage (
284+ channel = feedback_data ["ch" ],
285+ text = BOT_MESSAGES ["feedback_negative_thanks" ],
286+ thread_ts = feedback_data .get ("tt" ),
287+ )
288+ except Exception as e :
289+ logger .error (f"Error handling negative feedback: { e } " )
290+
291+
236292# ================================================================
237293# Registration (kept minimal to satisfy complexity checkers)
238294# ================================================================
@@ -241,8 +297,9 @@ def channel_message_handler(event, ack, body, client):
241297def setup_handlers (app ):
242298 """Register handlers. Intentionally minimal—no branching here."""
243299 app .event ("app_mention" )(app_mention_handler )
244- app .event ("message" )(dm_message_handler )
245- app .event ("message" )(channel_message_handler )
300+ app .event ("message" )(unified_message_handler )
301+ app .action ("feedback_yes" )(feedback_yes_handler )
302+ app .action ("feedback_no" )(feedback_no_handler )
246303
247304
248305# ================================================================
0 commit comments