imseldrith commited on
Commit
27dc5d5
·
verified ·
1 Parent(s): 1d6399d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -5
app.py CHANGED
@@ -56,11 +56,21 @@ def webhook():
56
  # Set typing on
57
  set_typing_on(sender_id)
58
 
59
- response_text = chat_completion(message_text, sender_id)
60
- print(response_text)
61
- #response_text = 'hello'
62
- print(f"Sending response to {sender_id}: {response_text}")
63
- send_message(sender_id, response_text)
 
 
 
 
 
 
 
 
 
 
64
 
65
  # Set typing off after responding
66
  set_typing_off(sender_id)
@@ -78,6 +88,25 @@ def send_message(recipient_id, message_text):
78
  response = requests.post(url, headers=headers, json=payload)
79
  print(f"Message send response: {response.status_code}, {response.text}")
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  def set_typing_on(recipient_id):
82
  url = f'https://graph.facebook.com/v12.0/me/messages?access_token={PAGE_ACCESS_TOKEN}'
83
  headers = {'Content-Type': 'application/json'}
 
56
  # Set typing on
57
  set_typing_on(sender_id)
58
 
59
+ # Get response with possible image URLs
60
+ response_data = chat_completion(message_text, sender_id)
61
+ print("ChatBot Response:\n", response_data)
62
+ response_text = response_data.get('msg', '')
63
+ image_urls = response_data.get('image_urls', [])
64
+
65
+ print(f"ChatBot Response Text: {response_text}")
66
+ print(f"ChatBot Response Image URLs: {image_urls}")
67
+
68
+ # Send text and/or images
69
+ if response_text:
70
+ send_message(sender_id, response_text)
71
+ if image_urls:
72
+ for url in image_urls:
73
+ send_image(sender_id, url)
74
 
75
  # Set typing off after responding
76
  set_typing_off(sender_id)
 
88
  response = requests.post(url, headers=headers, json=payload)
89
  print(f"Message send response: {response.status_code}, {response.text}")
90
 
91
+ def send_image(recipient_id, image_url):
92
+ url = f'https://graph.facebook.com/v12.0/me/messages?access_token={PAGE_ACCESS_TOKEN}'
93
+ headers = {'Content-Type': 'application/json'}
94
+ payload = {
95
+ 'recipient': {'id': recipient_id},
96
+ 'message': {
97
+ 'attachment': {
98
+ 'type': 'image',
99
+ 'payload': {
100
+ 'url': image_url,
101
+ 'is_reusable': True
102
+ }
103
+ }
104
+ }
105
+ }
106
+ print(f"Sending image request: {payload}")
107
+ response = requests.post(url, headers=headers, json=payload)
108
+ print(f"Image send response: {response.status_code}, {response.text}")
109
+
110
  def set_typing_on(recipient_id):
111
  url = f'https://graph.facebook.com/v12.0/me/messages?access_token={PAGE_ACCESS_TOKEN}'
112
  headers = {'Content-Type': 'application/json'}