-
Notifications
You must be signed in to change notification settings - Fork 330
/
whatsapp_quickstart.py
139 lines (110 loc) · 3.99 KB
/
whatsapp_quickstart.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import json
from dotenv import load_dotenv
import os
import requests
import aiohttp
import asyncio
# --------------------------------------------------------------
# Load environment variables
# --------------------------------------------------------------
load_dotenv()
ACCESS_TOKEN = os.getenv("ACCESS_TOKEN")
RECIPIENT_WAID = os.getenv("RECIPIENT_WAID")
PHONE_NUMBER_ID = os.getenv("PHONE_NUMBER_ID")
VERSION = os.getenv("VERSION")
APP_ID = os.getenv("APP_ID")
APP_SECRET = os.getenv("APP_SECRET")
# --------------------------------------------------------------
# Send a template WhatsApp message
# --------------------------------------------------------------
def send_whatsapp_message():
url = f"https://graph.facebook.com/{VERSION}/{PHONE_NUMBER_ID}/messages"
headers = {
"Authorization": "Bearer " + ACCESS_TOKEN,
"Content-Type": "application/json",
}
data = {
"messaging_product": "whatsapp",
"to": RECIPIENT_WAID,
"type": "template",
"template": {"name": "hello_world", "language": {"code": "en_US"}},
}
response = requests.post(url, headers=headers, json=data)
return response
# Call the function
response = send_whatsapp_message()
print(response.status_code)
print(response.json())
# --------------------------------------------------------------
# Send a custom text WhatsApp message
# --------------------------------------------------------------
# NOTE: First reply to the message from the user in WhatsApp!
def get_text_message_input(recipient, text):
return json.dumps(
{
"messaging_product": "whatsapp",
"recipient_type": "individual",
"to": recipient,
"type": "text",
"text": {"preview_url": False, "body": text},
}
)
def send_message(data):
headers = {
"Content-type": "application/json",
"Authorization": f"Bearer {ACCESS_TOKEN}",
}
url = f"https://graph.facebook.com/{VERSION}/{PHONE_NUMBER_ID}/messages"
response = requests.post(url, data=data, headers=headers)
if response.status_code == 200:
print("Status:", response.status_code)
print("Content-type:", response.headers["content-type"])
print("Body:", response.text)
return response
else:
print(response.status_code)
print(response.text)
return response
data = get_text_message_input(
recipient=RECIPIENT_WAID, text="Hello, this is a test message."
)
response = send_message(data)
# --------------------------------------------------------------
# Send a custom text WhatsApp message asynchronously
# --------------------------------------------------------------
# Does not work with Jupyter!
async def send_message(data):
headers = {
"Content-type": "application/json",
"Authorization": f"Bearer {ACCESS_TOKEN}",
}
async with aiohttp.ClientSession() as session:
url = "https://graph.facebook.com" + f"/{VERSION}/{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",
"recipient_type": "individual",
"to": recipient,
"type": "text",
"text": {"preview_url": False, "body": text},
}
)
data = get_text_message_input(
recipient=RECIPIENT_WAID, text="Hello, this is a test message."
)
loop = asyncio.get_event_loop()
loop.run_until_complete(send_message(data))
loop.close()