@@ -47,13 +47,19 @@ def fetch_intercom_conversation(conversation_id):
47
47
return response , response .status_code
48
48
49
49
# 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 )
54
62
55
- # Get and join the latest user messages from the conversation parts
56
- joined_text = extract_latest_user_messages (result )
57
63
if not joined_text :
58
64
return "No entries made by user found." , 204
59
65
return joined_text , 200
@@ -72,6 +78,14 @@ def extract_conversation_parts(response):
72
78
extracted_parts .append ({'body' : body , 'author' : author , 'created_at' : created_at })
73
79
return extracted_parts
74
80
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
+
75
89
# Joins the latest user entries in the conversation starting from the last non-user (i.e. admin) entry
76
90
def extract_latest_user_messages (conversation_parts ):
77
91
# Find the index of the last non-user entry
@@ -158,15 +172,15 @@ def post_intercom_reply(conversation_id, response_text):
158
172
159
173
160
174
# 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 ):
162
176
logger .info (f"Received request to get conversation { conversation_id } " )
163
177
# Retrieves the history of the conversation thread in Intercom
164
178
conversation , status_code = fetch_intercom_conversation (conversation_id )
165
179
if status_code != 200 :
166
180
return jsonify (conversation ), status_code
167
181
168
182
# 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 )
170
184
if status_code != 200 :
171
185
return jsonify (user_query ), status_code
172
186
0 commit comments