-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmessenger_webhook.py
67 lines (58 loc) · 2.48 KB
/
messenger_webhook.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
import os
from pymessenger.bot import Bot
from modifyingMessages import textDecider
from flask import request
#VERIFY_TOKEN = 'COVID_CHATS'
#ACCESS_TOKEN = 'EAAMd3g3exIMBAD4cejiCObrARbzSwpq79ZCeGWtZAmW2nSj4bk8rYZCDZCWJctU0aMygZA2VQu7sqJMGn6vq4DyUbXIzFIHXgRlvQMGzEbDRylKevM2MfBRvd9yN7T5fSsSijyxWdTcIGWRLOPmva0PhS9GOZCjec3XMe2v6AbbQZDZD '
ACCESS_TOKEN = os.environ['ACCESS_TOKEN']
VERIFY_TOKEN = os.environ['VERIFY_TOKEN']
bot = Bot(ACCESS_TOKEN)
def messenger_function():
if request.method == 'GET':
token_sent = request.args.get("hub.verify_token")
return verify_token(token_sent)
else:
output = request.get_json()
print(output)
for event in output['entry']:
messaging = event['messaging']
for message in messaging:
if message.get('message'):
recipient_id = message['sender']['id']
if message['message'].get('text'):
response_sent_text = textDecider(message['message'].get('text'))
# response_sent_text = length_of_text_consideration(response_sent_text)
send_message(recipient_id, response_sent_text)
# in case of gif or text
if message['message'].get('attachments'):
response_non_text = textDecider()
send_message(recipient_id, response_non_text)
return "Message Processed"
def verify_token(token_sent):
if token_sent == VERIFY_TOKEN:
return request.args.get("hub.challenge")
return "Invalid argument"
def length_of_text_consideration(response_sent_text):
list_of_text = []
if type(response_sent_text) == list:
for info in response_sent_text:
if len(info) > 1230:
list_sub_text = info.split('\n\n')
list_of_text += list_sub_text
else:
list_of_text.append(info)
return list_of_text
else:
if len(response_sent_text) > 1270:
list_of_text = response_sent_text.split('\n\n')
return list_of_text
else:
return response_sent_text
def send_message(recipient_id, response):
# sends user the text message provided via input response parameter
if type(response) == list:
for list_entity in response:
bot.send_text_message(recipient_id, list_entity)
else:
bot.send_text_message(recipient_id, response)
return "success"