forked from fbsamples/whatsapp-api-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_helper.py
89 lines (82 loc) · 2.25 KB
/
message_helper.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
Copyright (c) Meta Platforms, Inc. and affiliates.
All rights reserved.
This source code is licensed under the license found in the
LICENSE file in the root directory of this source tree.
"""
import aiohttp
import json
from flask import current_app
async def send_message(data):
headers = {
"Content-type": "application/json",
"Authorization": f"Bearer {current_app.config['ACCESS_TOKEN']}",
}
async with aiohttp.ClientSession() as session:
url = 'https://graph.facebook.com' + f"/{current_app.config['VERSION']}/{current_app.config['PHONE_NUMBER_ID']}/messages"
try:
async with session.post(url, data=data, headers=headers) as response:
if response.status == 200:
print("Status:", response.status)
print("Content-type:", response.headers['content-type'])
html = await response.text()
print("Body:", html)
else:
print(response.status)
print(response)
except aiohttp.ClientConnectorError as e:
print('Connection Error', str(e))
def get_text_message_input(recipient, text):
return json.dumps({
"messaging_product": "whatsapp",
"preview_url": False,
"recipient_type": "individual",
"to": recipient,
"type": "text",
"text": {
"body": text
}
})
def get_templated_message_input(recipient, flight):
return json.dumps({
"messaging_product": "whatsapp",
"to": recipient,
"type": "template",
"template": {
"name": "sample_flight_confirmation",
"language": {
"code": "en_US"
},
"components": [
{
"type": "header",
"parameters": [
{
"type": "document",
"document": {
"filename": "FlightConfirmation.pdf",
"link": flight['document']
}
}
]
},
{
"type": "body",
"parameters": [
{
"type": "text",
"text": flight['origin']
},
{
"type": "text",
"text": flight['destination']
},
{
"type": "text",
"text": flight['time']
}
]
}
]
}
})