Skip to content

Commit 3c9cacb

Browse files
committed
minor restructuring of code
1 parent 04fe54c commit 3c9cacb

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

app/app.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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,32 +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.")
182-
return 'OK'
183177

184-
# Check if the conversation is of type email
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
185181
conversation_type = data.get('data', {}).get('item', {}).get('source', {}).get('type')
186182
if conversation_type == 'email':
187183
logger.info(f"Conversation {conversation_id} is of type email; no action taken.")
188184
return 'OK'
189185

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+
190194
# Fetch the conversation and generate an LLM answer for the user
191195
logger.info(f"Detected a user reply in conversation {conversation_id}; fetching an answer from LLM...")
192196
answer_intercom_conversation(app.rag_system, conversation_id)
193197

194-
elif topic == 'conversation.user.created':
195-
# In this case, the webhook event is a new user conversation
196-
# Check if the conversation is of type email
197-
conversation_type = data.get('data', {}).get('item', {}).get('source', {}).get('type')
198-
if conversation_type == 'email':
199-
logger.info(f"New conversation {conversation_id} is of type email; no action taken.")
200-
else:
201-
logger.info(f"New conversation {conversation_id} is not of type email; generating an answer from LLM...")
202-
answer_intercom_conversation(app.rag_system, conversation_id)
203198
else:
204199
logger.info(f"Received webhook for unsupported topic: {topic}; no action taken.")
205200
return 'OK'

0 commit comments

Comments
 (0)