@@ -158,7 +158,7 @@ def handle_webhook():
158158 topic = data .get ('topic' )
159159 logger .info (f"Webhook topic: { topic } " )
160160 if topic == 'conversation.admin.replied' :
161-
161+ # In this case, the webhook event is an admin reply
162162 # Check if the admin is a bot or human based on presence of a message marker (e.g., "🤖") in the last message
163163 last_message = data .get ('data' , {}).get ('item' , {}).get ('conversation_parts' , {}).get ('conversation_parts' , [])[- 1 ].get ('body' , '' )
164164 last_message_text = parse_html_to_text (last_message )
@@ -174,15 +174,27 @@ def handle_webhook():
174174 # Mark the conversation as replied by a human admin to skip LLM responses in the future
175175 set_conversation_human_replied (conversation_id , r )
176176 logger .info (f"Successfully marked conversation { conversation_id } as human admin-replied." )
177- elif topic == 'conversation.user.replied' :
178- # In this case, the webhook event is a user reply, not an admin reply
179- # Check if the conversation was replied previously by a human admin
180- if is_conversation_human_replied (conversation_id , r ):
181- logger .info (f"Conversation { conversation_id } already marked as human admin-replied; no action taken." )
177+
178+ elif topic == 'conversation.user.replied' or topic == 'conversation.user.created' :
179+ # In this case, the webhook event is a user reply or a new user conversation
180+ # Check if the conversation is of type email, and skip processing if so
181+ conversation_type = data .get ('data' , {}).get ('item' , {}).get ('source' , {}).get ('type' )
182+ if conversation_type == 'email' :
183+ logger .info (f"Conversation { conversation_id } is of type email; no action taken." )
182184 return 'OK'
185+
186+ # Check if it is a user reply and do the admin-replied checks if so
187+ # For new user conversations, we will skip admin-replied check to avoid false positives from Intercom auto-replies
188+ if topic == 'conversation.user.replied' :
189+ # Check if the conversation was replied previously by a human admin and skip processing if so
190+ if is_conversation_human_replied (conversation_id , r ):
191+ logger .info (f"Conversation { conversation_id } already marked as human admin-replied; no action taken." )
192+ return 'OK'
193+
183194 # Fetch the conversation and generate an LLM answer for the user
184195 logger .info (f"Detected a user reply in conversation { conversation_id } ; fetching an answer from LLM..." )
185196 answer_intercom_conversation (app .rag_system , conversation_id )
197+
186198 else :
187199 logger .info (f"Received webhook for unsupported topic: { topic } ; no action taken." )
188200 return 'OK'
0 commit comments