Spaces:
Sleeping
Sleeping
imseldrith
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -138,6 +138,10 @@ def send_message(recipient_id, message_text):
|
|
138 |
print(f"Message send response: {response.status_code}, {response.text}")
|
139 |
|
140 |
def send_image(recipient_id, image_url):
|
|
|
|
|
|
|
|
|
141 |
url = f'https://graph.facebook.com/v12.0/me/messages?access_token={PAGE_ACCESS_TOKEN}'
|
142 |
headers = {'Content-Type': 'application/json'}
|
143 |
payload = {
|
@@ -156,6 +160,17 @@ def send_image(recipient_id, image_url):
|
|
156 |
response = requests.post(url, headers=headers, json=payload)
|
157 |
print(f"Image send response: {response.status_code}, {response.text}")
|
158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
def set_typing_on(recipient_id):
|
160 |
url = f'https://graph.facebook.com/v12.0/me/messages?access_token={PAGE_ACCESS_TOKEN}'
|
161 |
headers = {'Content-Type': 'application/json'}
|
|
|
138 |
print(f"Message send response: {response.status_code}, {response.text}")
|
139 |
|
140 |
def send_image(recipient_id, image_url):
|
141 |
+
if not verify_image_url(image_url):
|
142 |
+
print("Invalid or inaccessible image URL.")
|
143 |
+
return
|
144 |
+
|
145 |
url = f'https://graph.facebook.com/v12.0/me/messages?access_token={PAGE_ACCESS_TOKEN}'
|
146 |
headers = {'Content-Type': 'application/json'}
|
147 |
payload = {
|
|
|
160 |
response = requests.post(url, headers=headers, json=payload)
|
161 |
print(f"Image send response: {response.status_code}, {response.text}")
|
162 |
|
163 |
+
if response.status_code != 200:
|
164 |
+
print(f"Error sending image: {response.json()}")
|
165 |
+
|
166 |
+
def verify_image_url(image_url):
|
167 |
+
try:
|
168 |
+
response = requests.head(image_url)
|
169 |
+
return response.status_code == 200 and 'image' in response.headers['Content-Type']
|
170 |
+
except Exception as e:
|
171 |
+
print(f"Error verifying image URL: {e}")
|
172 |
+
return False
|
173 |
+
|
174 |
def set_typing_on(recipient_id):
|
175 |
url = f'https://graph.facebook.com/v12.0/me/messages?access_token={PAGE_ACCESS_TOKEN}'
|
176 |
headers = {'Content-Type': 'application/json'}
|