-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalert_module.py
102 lines (87 loc) · 2.67 KB
/
alert_module.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
from dotenv import load_dotenv
import requests
import json
import os
class Whatsapp_Cloud_API:
#------------------------------------------------------------------------------------------
'''
.env file required with
PHONE_ID = <Value>
TOKEN = <Value>
'''
#------------------------------------------------------------------------------------------
def __init__(self):
load_dotenv()
phone_id = os.getenv("PHONE_ID")
token = os.getenv("TOKEN")
self.__url__ = "https://graph.facebook.com/v13.0/"+ phone_id +"/messages"
self.__headers__ = {
'Authorization' : 'Bearer '+token,
'Content-Type' : 'application/json'
}
#------------------------------------------------------------------------------------------
def validate(self,text):
return ('messaging_product' in text)
#------------------------------------------------------------------------------------------
def alert(self,Phone,Image_Link,Name,Profile_Link,Address,Time):
payload = json.dumps({
"messaging_product": "whatsapp",
"to": Phone,
"type": "template",
"template": {
"name": "alert",
"language": {
"code": "en"
},
"components": [
{
"type": "header",
"parameters": [
{
"type" : "image",
"image": {
"link": Image_Link
}
}
]
},
{
"type": "body",
"parameters": [
{
"type": "text",
"text": Name
},
{
"type": "text",
"text": Profile_Link
},
{
"type": "text",
"text": Address
},
{
"type": "text",
"text": Time
}
]
}
]
}
})
response = requests.request("POST", self.__url__, headers=self.__headers__, data=payload)
print(response.text)
return self.validate(response.text)
#------------------------------------------------------------------------------------------
'''
ILLUSTRATION
------------
Phone = "919549408165"
Image_Link = "https://avatars.githubusercontent.com/u/68856476?s=400&u=3d5c281a14da0a6d2efa7cd9345e30d3e872ab8f&v=4"
Name = "Garvit Chouhan"
Profile_Link = "https://avatars.githubusercontent.com/u/68856476?s=400&u=3d5c281a14da0a6d2efa7cd9345e30d3e872ab8f&v=4"
Address = "Udaipur Rajasthan"
Time = "5:16 pm 17/06/2022"
api = Whatsapp_Cloud_API()
print(api.alert(Phone,Image_Link,Name,Profile_Link,Address,Time))
'''