Skip to content

Commit 93ca92f

Browse files
authored
Merge pull request #73 from DefangLabs/linda-fix-initial-message
Answer initial messages in intercom
2 parents aec6a91 + d724f75 commit 93ca92f

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

app/intercom.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,19 @@ def fetch_intercom_conversation(conversation_id):
4747
return response, response.status_code
4848

4949
# Determines the user query from the Intercom conversation response
50-
def get_user_query(response, conversation_id):
51-
# Extract conversation parts from an Intercom request response
52-
result = extract_conversation_parts(response)
53-
logger.info(f"Extracted {len(result)} parts from conversation {conversation_id}")
50+
def get_user_query(response, conversation_id, topic):
51+
joined_text = None
52+
# Determine the user query based on the type of topic
53+
if topic == 'conversation.user.replied':
54+
# Extract conversation parts from an Intercom request response
55+
result = extract_conversation_parts(response)
56+
logger.info(f"Extracted {len(result)} parts from conversation {conversation_id}")
57+
# Get and join only the latest user messages from the conversation parts
58+
joined_text = extract_latest_user_messages(result)
59+
elif topic == 'conversation.user.created':
60+
# Extract the query directly from an Intercom request response
61+
joined_text = extract_query_from_new_conversation(response)
5462

55-
# Get and join the latest user messages from the conversation parts
56-
joined_text = extract_latest_user_messages(result)
5763
if not joined_text:
5864
return "No entries made by user found.", 204
5965
return joined_text, 200
@@ -72,6 +78,14 @@ def extract_conversation_parts(response):
7278
extracted_parts.append({'body': body, 'author': author, 'created_at': created_at})
7379
return extracted_parts
7480

81+
# Extract the query (i.e. the first user message) from a new conversation in Intercom
82+
def extract_query_from_new_conversation(response):
83+
data = response.json()
84+
body = data.get('source', {}).get('body')
85+
if body:
86+
return parse_html_to_text(body)
87+
return None
88+
7589
# Joins the latest user entries in the conversation starting from the last non-user (i.e. admin) entry
7690
def extract_latest_user_messages(conversation_parts):
7791
# Find the index of the last non-user entry
@@ -158,15 +172,15 @@ def post_intercom_reply(conversation_id, response_text):
158172

159173

160174
# Returns a generated LLM answer to the Intercom conversation based on previous user message history
161-
def answer_intercom_conversation(rag, conversation_id):
175+
def answer_intercom_conversation(rag, conversation_id, topic):
162176
logger.info(f"Received request to get conversation {conversation_id}")
163177
# Retrieves the history of the conversation thread in Intercom
164178
conversation, status_code = fetch_intercom_conversation(conversation_id)
165179
if status_code != 200:
166180
return jsonify(conversation), status_code
167181

168182
# Extracts the user query (which are latest user messages joined into a single string) from conversation history
169-
user_query, status_code = get_user_query(conversation, conversation_id)
183+
user_query, status_code = get_user_query(conversation, conversation_id, topic)
170184
if status_code != 200:
171185
return jsonify(user_query), status_code
172186

0 commit comments

Comments
 (0)