Dooratre commited on
Commit
f7798ea
·
verified ·
1 Parent(s): 8aa8028

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -12
app.py CHANGED
@@ -97,7 +97,6 @@ def handle_message():
97
 
98
  # Fetch the user's profile information via the proxy API
99
  user_full_name = "User" # Default fallback
100
- user_profile_img = "" # Default empty profile image URL
101
 
102
  try:
103
  proxy_params = {
@@ -110,8 +109,6 @@ def handle_message():
110
 
111
  if proxy_data["success"]:
112
  user_full_name = proxy_data["full_name"]
113
- # Also get the profile image URL if available
114
- user_profile_img = proxy_data.get("profile_pic", "")
115
  else:
116
  print(f"Proxy API error: {proxy_data['error']}")
117
  except Exception as e:
@@ -224,27 +221,31 @@ def handle_message():
224
  # Check if the AI response contains an admin message
225
  admin_message, cleaned_response = extract_admin_message(ai_response)
226
 
 
 
 
 
 
 
227
  # If there's an admin message, save it to the GitHub JSON file
228
  if admin_message:
229
  print(f"Admin message detected: {admin_message}")
230
  save_admin_message(page_id, admin_message, sender_id, user_full_name)
231
- # Use the cleaned response (without admin tags) for the user
232
- ai_response = cleaned_response
233
 
234
- # Append the AI's response to the chat history
235
- user_history['history'].append({
236
- "role": "assistant",
237
- "content": [{"type": "text", "text": ai_response}]
238
- })
239
 
240
- # Send the AI response back to the user
241
  headers = {
242
  "Authorization": f"Bearer {AI_SERVICE_TOKEN}",
243
  "Content-Type": "application/json"
244
  }
245
  payload = {
246
  "recipient_id": sender_id,
247
- "message_text": ai_response,
248
  "page_access_token": page_token
249
  }
250
  requests.post(AI_SERVICE_URL, json=payload, headers=headers)
 
97
 
98
  # Fetch the user's profile information via the proxy API
99
  user_full_name = "User" # Default fallback
 
100
 
101
  try:
102
  proxy_params = {
 
109
 
110
  if proxy_data["success"]:
111
  user_full_name = proxy_data["full_name"]
 
 
112
  else:
113
  print(f"Proxy API error: {proxy_data['error']}")
114
  except Exception as e:
 
221
  # Check if the AI response contains an admin message
222
  admin_message, cleaned_response = extract_admin_message(ai_response)
223
 
224
+ # Always add the original response to chat history (with admin tags if present)
225
+ user_history['history'].append({
226
+ "role": "assistant",
227
+ "content": [{"type": "text", "text": ai_response}]
228
+ })
229
+
230
  # If there's an admin message, save it to the GitHub JSON file
231
  if admin_message:
232
  print(f"Admin message detected: {admin_message}")
233
  save_admin_message(page_id, admin_message, sender_id, user_full_name)
 
 
234
 
235
+ # Send the cleaned response (without admin tags) to the user
236
+ response_to_send = cleaned_response
237
+ else:
238
+ # No admin message, send the original response
239
+ response_to_send = ai_response
240
 
241
+ # Send the appropriate response back to the user
242
  headers = {
243
  "Authorization": f"Bearer {AI_SERVICE_TOKEN}",
244
  "Content-Type": "application/json"
245
  }
246
  payload = {
247
  "recipient_id": sender_id,
248
+ "message_text": response_to_send,
249
  "page_access_token": page_token
250
  }
251
  requests.post(AI_SERVICE_URL, json=payload, headers=headers)