-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfacebook2api.py
More file actions
59 lines (50 loc) · 1.7 KB
/
facebook2api.py
File metadata and controls
59 lines (50 loc) · 1.7 KB
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
55
56
57
58
59
import requests
from requests import codes
import config
import logger
LOGGER = logger.getLogger(__name__)
def _send(message_data):
response = requests.post(config.facebook['URL'], json=message_data,
params={'access_token': config.facebook['PAGE_ACCESS_TOKEN']})
if response.status_code != codes.ok:
try:
LOGGER.error('Failed to sent the message data to Facebook API, status: %d', response.status_code)
LOGGER.error('Error message: %s', response.json()['error'])
except ValueError as error:
LOGGER.error('Failed to retrieved the error message, error: %s', str(error))
return
body = response.json()
recipient_id = body['recipient_id']
message_id = body['message_id']
if message_id:
LOGGER.info('Successfully sent message with id %s to recipient %s', message_id, recipient_id)
else:
LOGGER.info('Successfully called Facebook API to recipient %s', recipient_id)
def send_text_message(recipient_id, message_text):
message_data = {
'recipient': {
'id': recipient_id
},
'message': {
'text': message_text,
'metadata': 'DOTA2_TEXT_MESSAGE'
}
}
_send(message_data)
def send_generic_message(recipient_id, elements):
message_data = {
'recipient': {
'id': recipient_id
},
'message': {
'attachment': {
'type': 'template',
'payload': {
'template_type': 'generic',
'elements': elements
}
},
'metadata': 'DOTA2_GENERIC_MESSAGE'
}
}
_send(message_data)