imseldrith commited on
Commit
6c4b302
·
verified ·
1 Parent(s): c14b31d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -26
app.py CHANGED
@@ -50,23 +50,46 @@ def webhook():
50
  print(f"Messaging event: {messaging_event}")
51
 
52
  if 'message' in messaging_event:
 
 
 
 
 
53
  sender_id = messaging_event['sender']['id']
54
  message_text = messaging_event['message'].get('text', '')
55
 
 
 
 
 
56
  print(f"Received message from {sender_id}: {message_text}")
57
 
58
  # Set typing on
59
  set_typing_on(sender_id)
60
- # Get response with possible image URLs
61
- response_data = chat_completion(message_text, sender_id)
62
- print("ChatBot Response:\n", response_data)
63
- # Convert response_data to dictionary if it's a string
64
- if isinstance(response_data, str):
65
- response_data = json.loads(response_data)
66
- print(type(response_data))
67
  try:
68
-
 
 
 
69
  # Ensure response_data is a dictionary
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  if isinstance(response_data, dict):
71
  response_text = response_data.get('msg', '')
72
  image_urls = response_data.get('image_urls', [])
@@ -80,26 +103,13 @@ def webhook():
80
  if image_urls:
81
  for url in image_urls:
82
  send_image(sender_id, url)
83
- elif isinstance(response_data, str):
84
- # Extract text and URLs from the string
85
- response_text, image_urls = extract_text_and_urls(response_data)
86
-
87
- print(f"Extracted Text: {response_text}")
88
- print(f"Extracted Image URLs: {image_urls}")
89
-
90
- # Send text and/or images
91
- if response_text:
92
- send_message(sender_id, response_text)
93
- if image_urls:
94
- for url in image_urls:
95
- send_image(sender_id, url)
96
-
97
  else:
98
  print("Error: ChatBot response is not a dictionary.")
99
- send_message(sender_id, response_data)
100
  except Exception as e:
101
  print(f"Exception occurred: {e}")
102
-
 
103
  # Mark message as seen
104
  mark_seen(sender_id)
105
  # Set typing off after responding
@@ -167,7 +177,7 @@ def set_typing_off(recipient_id):
167
  print(f"Sending typing off request: {payload}")
168
  response = requests.post(url, headers=headers, json=payload)
169
  print(f"Typing off response: {response.status_code}, {response.text}")
170
-
171
  def mark_seen(recipient_id):
172
  url = f'https://graph.facebook.com/v12.0/me/messages?access_token={PAGE_ACCESS_TOKEN}'
173
  headers = {'Content-Type': 'application/json'}
@@ -178,6 +188,6 @@ def mark_seen(recipient_id):
178
  print(f"Sending mark seen request: {payload}")
179
  response = requests.post(url, headers=headers, json=payload)
180
  print(f"Mark seen response: {response.status_code}, {response.text}")
181
-
182
  if __name__ == '__main__':
183
  app.run(host="0.0.0.0", port=7860, debug=True)
 
50
  print(f"Messaging event: {messaging_event}")
51
 
52
  if 'message' in messaging_event:
53
+ # Ignore echo messages
54
+ if messaging_event['message'].get('is_echo'):
55
+ print("Echo message received. Ignoring.")
56
+ return jsonify({'status': 'ok'})
57
+
58
  sender_id = messaging_event['sender']['id']
59
  message_text = messaging_event['message'].get('text', '')
60
 
61
+ if not message_text:
62
+ print("Empty message text received. Ignoring.")
63
+ return jsonify({'status': 'ok'})
64
+
65
  print(f"Received message from {sender_id}: {message_text}")
66
 
67
  # Set typing on
68
  set_typing_on(sender_id)
69
+
 
 
 
 
 
 
70
  try:
71
+ # Get response with possible image URLs
72
+ response_data = chat_completion(message_text, sender_id)
73
+ print("ChatBot Response:\n", response_data)
74
+
75
  # Ensure response_data is a dictionary
76
+ if isinstance(response_data, str):
77
+ try:
78
+ response_data = json.loads(response_data)
79
+ except json.JSONDecodeError:
80
+ # Extract text and URLs from the string
81
+ response_text, image_urls = extract_text_and_urls(response_data)
82
+ print(f"Extracted Text: {response_text}")
83
+ print(f"Extracted Image URLs: {image_urls}")
84
+
85
+ # Send text and/or images
86
+ if response_text:
87
+ send_message(sender_id, response_text)
88
+ if image_urls:
89
+ for url in image_urls:
90
+ send_image(sender_id, url)
91
+ response_data = {}
92
+
93
  if isinstance(response_data, dict):
94
  response_text = response_data.get('msg', '')
95
  image_urls = response_data.get('image_urls', [])
 
103
  if image_urls:
104
  for url in image_urls:
105
  send_image(sender_id, url)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  else:
107
  print("Error: ChatBot response is not a dictionary.")
108
+ send_message(sender_id, "Sorry, I couldn't process your request.")
109
  except Exception as e:
110
  print(f"Exception occurred: {e}")
111
+ send_message(sender_id, "Sorry, something went wrong. Please try again later.")
112
+
113
  # Mark message as seen
114
  mark_seen(sender_id)
115
  # Set typing off after responding
 
177
  print(f"Sending typing off request: {payload}")
178
  response = requests.post(url, headers=headers, json=payload)
179
  print(f"Typing off response: {response.status_code}, {response.text}")
180
+
181
  def mark_seen(recipient_id):
182
  url = f'https://graph.facebook.com/v12.0/me/messages?access_token={PAGE_ACCESS_TOKEN}'
183
  headers = {'Content-Type': 'application/json'}
 
188
  print(f"Sending mark seen request: {payload}")
189
  response = requests.post(url, headers=headers, json=payload)
190
  print(f"Mark seen response: {response.status_code}, {response.text}")
191
+
192
  if __name__ == '__main__':
193
  app.run(host="0.0.0.0", port=7860, debug=True)