-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_api.py
377 lines (365 loc) · 15.1 KB
/
send_api.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# -*- coding: utf-8 -*-
import json
import logging
import os
import time
import requests
import requests_toolbelt.adapters.appengine
requests_toolbelt.adapters.appengine.monkeypatch()
def send_message(recipient_id, message_text):
logging.info("sending message to {recipient}: {text}".format(recipient=recipient_id, text=message_text))
params = {
"access_token": os.environ["PAGE_ACCESS_TOKEN"],
}
headers = {
"Content-Type": "application/json"
}
data = json.dumps({
"recipient": {
"id": recipient_id
},
"message": {
"text": message_text
}
})
r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data)
if r.status_code != 200:
logging.info(r.status_code)
logging.info(r.text)
def send_action(recipient_id, action):
logging.info("sending {action} to {recipient}".format(action=action, recipient=recipient_id))
params = {
"access_token": os.environ["PAGE_ACCESS_TOKEN"],
}
headers = {
"Content-Type": "application/json"
}
data = json.dumps({
"recipient": {
"id": recipient_id
},
"sender_action": action
})
r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data)
if r.status_code != 200:
logging.info(r.status_code)
logging.info(r.text)
def send_example(recipient_id):
logging.info("sending media_template to {recipient}".format(recipient=recipient_id))
params = {
"access_token": os.environ["PAGE_ACCESS_TOKEN"],
}
headers = {
"Content-Type": "application/json"
}
data = json.dumps({
"recipient":{
"id": recipient_id
},
"message":{
"attachment": {
"type": "template",
"payload": {
"template_type": "media",
"elements": [
{
"media_type": "image",
"attachment_id": "291089468172284"
}
]
}
}
}
})
r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data)
if r.status_code != 200:
logging.info(r.status_code)
logging.info(r.text)
def send_tutorial(recipient_id):
#send_message(recipient_id, "將有興趣的Amazon商品頁面分享給追價弓,或是直接貼上商品網址,立刻取得歷史低價資訊,像這樣:")
send_message(recipient_id, "分享Amazon商品頁面給我,立刻取得歷史低價資訊")
send_action(recipient_id, "typing_on")
time.sleep(2.5)
send_example(recipient_id)
send_action(recipient_id, "typing_on")
time.sleep(2.5)
send_message(recipient_id, "按下【追價】,每次降價立即通知你!")
def send_generic_template(recipient_id, link, meta, price_stat, monitor_key):
logging.info("sending generic_template to {recipient}".format(recipient=recipient_id))
params = {
"access_token": os.environ["PAGE_ACCESS_TOKEN"],
}
headers = {
"Content-Type": "application/json"
}
data = json.dumps({
"recipient": {
"id": recipient_id
},
"message": {
"attachment": {
"type":"template",
"payload":{
"template_type":"generic",
"sharable": "true",
"elements":[
{
"title": meta["alt"],
"image_url": meta["img"],
"subtitle": price_stat,
"default_action": {
"type": "web_url",
"url": link,
"webview_height_ratio": "full",
"messenger_extensions": False,
#"fallback_url": "https://petersfancybrownhats.com/"
}, # end of default_action
"buttons":[
# b1
{
"type": "postback",
"title": "追價",
"payload": json.dumps(
{
"key": monitor_key
}
)
},
# b2
{
"type": "postback",
"title": "檢視我的追價",
"payload": json.dumps(
{
"key": "collection"
}
)
},
#{
# "type": "web_url",
# "url": "https://www.amazon.co.jp",
# "title": "buy it",
#},
# b3
{
"type":"element_share",
"share_contents": {
"attachment": {
"type": "template",
"payload":{
"template_type":"generic",
"sharable": "true",
"elements":[
{
"title": meta["alt"],
"image_url": meta["img"],
"subtitle": price_stat,
"default_action": {
"type": "web_url",
"url": link,
"webview_height_ratio": "full",
"messenger_extensions": False,
#"fallback_url": "https://petersfancybrownhats.com/"
}, # end of default_action
"buttons":[
# b1
{
"type": "web_url",
"url": "https://www.facebook.com/Archdown追價弓-651712701861404/",
#"url": "https://m.me/651712701861404",
"title":"立刻體驗追價弓",
},
] # end of buttons
} # end of elements
] # end of elements
} # end of payload
} # end of attachment
} # end of ahare_contents
}
] # end of buttons
} # end of elements
] # end of elements
} # end of payload
} # end of attachment
} # end of message
})
r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data)
if r.status_code != 200:
logging.info(r.status_code)
logging.info(r.text)
def element_builder(entity_pair):
from datetime import datetime
monitor_entity, product_entity = entity_pair
element = {
"title": product_entity.meta["alt"],
"image_url": product_entity.meta["img"],
"subtitle": "現價: {}{}\n追價天數: {}".format(product_entity.currency.encode("utf-8"), product_entity.current, (datetime.now() - monitor_entity.timestamp).days),
"default_action": {
"type": "web_url",
"url": product_entity.link,
"webview_height_ratio": "full",
"messenger_extensions": False,
#"fallback_url": "https://petersfancybrownhats.com/"
}, # end of default_action
"buttons":[
# b1
{
"type": "postback",
"title": "移除",
"payload": json.dumps(
{
"key": monitor_entity.key.urlsafe()
}
)
},
] # end of buttons
} # end of elements
return element
def send_collection_template(recipient_id, collection):
logging.info("sending collection to {recipient}".format(recipient=recipient_id))
params = {
"access_token": os.environ["PAGE_ACCESS_TOKEN"],
}
headers = {
"Content-Type": "application/json"
}
data = json.dumps({
"recipient": {
"id": recipient_id
},
"message": {
"attachment": {
"type":"template",
"payload":{
"template_type":"generic",
"sharable": "true",
"elements": [element_builder(pair) for pair in collection]
} # end of payload
} # end of attachment
} # end of message
})
r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data)
if r.status_code != 200:
logging.info(r.status_code)
logging.info(r.text)
def send_charge_template(recipient_id):
logging.info("sending charge to {recipient}".format(recipient=recipient_id))
params = {
"access_token": os.environ["PAGE_ACCESS_TOKEN"],
}
headers = {
"Content-Type": "application/json"
}
data = json.dumps({
"recipient": {
"id": recipient_id
},
"message": {
"attachment": {
"type":"template",
"payload":{
"template_type":"button",
"text": "Oops! 追價QUOTA已用盡",
"buttons": [
# b1
{
"type": "postback",
"title": "取得更多QUOTA",
"payload": json.dumps(
{
"key": "buy"
}
)
},
# b2
{
"type": "postback",
"title": "檢視我的追價",
"payload": json.dumps(
{
"key": "collection"
}
)
}
]
} # end of payload
} # end of attachment
} # end of message
})
r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data)
if r.status_code != 200:
logging.info(r.status_code)
logging.info(r.text)
def send_alert_template(recipient_id, link, meta, price_stat, monitor_key):
logging.info("sending alert_template to {recipient}".format(recipient=recipient_id))
params = {
"access_token": os.environ["PAGE_ACCESS_TOKEN"],
}
headers = {
"Content-Type": "application/json"
}
data = json.dumps({
"recipient": {
"id": recipient_id
},
"message": {
"attachment": {
"type":"template",
"payload":{
"template_type":"generic",
"sharable": "true",
"elements":[
{
"title": "《!!!降價通知!!!》\n" + meta["alt"].encode("utf-8"),
#"title": meta["alt"],
"image_url": meta["img"],
"subtitle": price_stat,
"default_action": {
"type": "web_url",
"url": link,
"webview_height_ratio": "full",
"messenger_extensions": False,
#"fallback_url": "https://petersfancybrownhats.com/"
}, # end of default_action
"buttons":[
# b1
{
"type": "web_url",
"url": "https://archdown-tracker.appspot.com/redirect?link={link}&key={key}".format(link=link, key=monitor_key),
"title": "前往選購",
"webview_height_ratio": "full",
"messenger_extensions": False,
},
# b2
{
"type": "postback",
"title": "移除",
"payload": json.dumps(
{
"key": monitor_key
}
)
},
# b3
{
"type": "postback",
"title": "等待更低價",
"payload": json.dumps(
{
"key": monitor_key
}
)
}
] # end of buttons
} # end of elements
] # end of elements
} # end of payload
} # end of attachment
} # end of message
})
r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data)
if r.status_code != 200:
logging.info(r.status_code)
logging.info(r.text)
if __name__ == '__main__':
pass