Spaces:
Sleeping
Sleeping
File size: 1,463 Bytes
b59175c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import requests
import os
from dotenv import load_dotenv
load_dotenv()
PAGE_ACCESS_TOKEN = os.getenv('PAGE_ACCESS_TOKEN')
def send_message(recipient_id, message_text, image_url=None):
if message_text:
message_data = {
'recipient': {'id': recipient_id},
'message': {'text': message_text}
}
call_send_api(message_data)
if image_url:
message_data = {
'recipient': {'id': recipient_id},
'message': {
'attachment': {
'type': 'image',
'payload': {'url': image_url}
}
}
}
call_send_api(message_data)
def call_send_api(message_data):
url = 'https://graph.facebook.com/v11.0/me/messages'
params = {
'access_token': PAGE_ACCESS_TOKEN
}
headers = {
'Content-Type': 'application/json'
}
response = requests.post(url, params=params, headers=headers, json=message_data)
if response.status_code != 200:
print(f'Failed to send message: {response.status_code}, {response.text}')
def set_typing_on(recipient_id):
message_data = {
'recipient': {'id': recipient_id},
'sender_action': 'typing_on'
}
call_send_api(message_data)
def set_typing_off(recipient_id):
message_data = {
'recipient': {'id': recipient_id},
'sender_action': 'typing_off'
}
call_send_api(message_data)
|