-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg.py
153 lines (139 loc) · 3.97 KB
/
msg.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import json
import requests
graph_url = "https://graph.facebook.com/v4.0/me/messages"
def send_message(recipient_id, message_text,PAT):
params = {
"access_token": PAT
}
headers = {
"Content-Type": "application/json"
}
data = json.dumps({
"recipient": {
"id": recipient_id
},
"message": {
"text": message_text
}
})
r = requests.post(graph_url, params=params, headers=headers, data=data)
if r.status_code != 200:
print(r.status_code)
print(r.text)
def send_quick_reply(recipient_id, PAT, message_text):
params = {
"access_token": PAT
}
headers = {
"Content-Type": "application/json"
}
data = json.dumps({
"recipient": {
"id": recipient_id
},
"messaging_type": "RESPONSE",
"message": {
"text": message_text,
"quick_replies": [
{
"content_type": "text",
"title": "Mountain Bike",
"payload": "MOUNTAIN_SELECTED",
"image_url": "https://cdn4.vectorstock.com/i/1000x1000/95/53/mountain-bike-climbing-round-icon-vector-16679553.jpg"
}, {
"content_type": "text",
"title": "Speed bike",
"payload": "SPEED_SELECTED",
"image_url": "https://i.pinimg.com/originals/74/4b/20/744b20a7d6444555867de6a16ecfc4bc.png"
}
]
}
})
r = requests.post(graph_url, params=params, headers=headers, data=data)
if r.status_code != 200:
print(r.status_code)
print(r.text)
def send_mobile_number(recipient_id,PAT,message_text):
params = {
"access_token": PAT
}
headers = {
"Content-Type": "application/json"
}
data = json.dumps({
"recipient": {
"id": recipient_id
},
"messaging_type": "RESPONSE",
"message": {
"text": message_text,
"quick_replies": [
{
"content_type": "user_phone_number"
}
]
}
})
r = requests.post(graph_url, params=params, headers=headers, data=data)
if r.status_code != 200:
print(r.status_code)
print(r.text)
def send_image_message(recipient_id, image_path, PAT):
if image_path is None:
print("Invalid Image Path")
raise Exception
params = {
"access_token": PAT,
}
headers = {
"Content-Type": "application/json"
}
data = json.dumps({
"recipient": {"id": str(recipient_id)},
"message": {
"attachment": {
"type": "image",
"payload": {
"url": image_path
}
}
}
})
files = {
'filename': "rendered_image.png",
'media': open(image_path, 'rb')
}
r = requests.post(graph_url, params=params, headers=headers, data=data)
if r.status_code != 200:
print(r.status_code)
print(r.text)
def send_image_url_message(recipient_id, image, PAT):
if image is None:
print("Gif not found Exception")
raise Exception
# url = request.base_url + "/" + image.split("zinkybot")[1].replace("\\", "/")
# url = url.replace("///", "/")
params = {
"access_token": PAT
}
headers = {
"Content-Type": "application/json"
}
data = json.dumps({
"recipient": {
"id": recipient_id
},
"message": {
"attachment": {
"type": "image",
"payload": {
"url": image,
"is_reusable": True
}
}
}
})
r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data)
if r.status_code != 200:
print(r.status_code)
print(r.text)