-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
222 lines (192 loc) · 7.31 KB
/
app.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
from flask import Flask, request, json
import requests
from datetime import datetime
from threading import Timer
import urllib.request
def send_message(recipient_id, text, buttons=False):
"""Send a response to Facebook"""
if buttons:
buttons_json = []
for button in buttons:
buttons_json.append({
"type":"postback",
"payload": button['payload'],
"title":button['title'],
})
print(buttons_json)
payload = {
"recipient":{
"id":recipient_id,
},
"message":{
"attachment":{
"type":"template",
"payload":{
"template_type":"button",
"text":text,
"buttons": buttons_json
}
}
}
}
else:
payload = {
'message': {
'text': text
},
'recipient': {
'id': recipient_id
},
'notification_type': 'regular'
}
auth = {
'access_token': PAGE_ACCESS_TOKEN
}
response = requests.post(
FB_API_URL,
params=auth,
json=payload
)
return response.json()
def get_bot_response(message):
"""This is just a dummy function, returning a variation of what
the user said. Replace this function with one connected to chatbot."""
print(message)
return "{}".format(message)
def verify_webhook(req):
if req.args.get("hub.verify_token") == VERIFY_TOKEN:
return req.args.get("hub.challenge")
else:
return "incorrect"
def respond(sender, message, buttons=False):
"""Formulate a response to the user and
pass it on to a function that sends it."""
response = get_bot_response(message)
print(response)
send_message(sender, response, buttons)
def is_user_message(message):
"""Check if the message is a message from the user"""
return (message.get('message') and
message['message'].get('text') and
not message['message'].get("is_echo"))
def is_postback(message):
"""Check if the message is a message from the user with a payload"""
return (message.get('postback') and
message['postback'].get('payload') and
not message['postback'].get("is_echo"))
def is_speech(message):
"""Check if the message is speech"""
try:
return (message.get('message') and
message['message'].get('attachments')[0]['type']=='audio')
except:
return False
def generateAuthToken():
# Generate Auth Token
payload={
'REFRESH_TOKEN': refreshToken,
}
IdToken = json.loads(requests.request("POST", generateAuthTokenUrl, data=payload).content)['AuthenticationResult']['IdToken']
print(IdToken)
# Store IdToken in config.json
with open('config.json') as json_file:
parameters = json.load(json_file)
parameters['IdToken'] = IdToken
open('config.json', 'w').write(json.dumps(parameters))
return IdToken
app = Flask(__name__)
app.secret_key = 'any random string'
with open('config.json') as json_file:
parameters = json.load(json_file)
print("Type:", type(parameters))
print(parameters)
FB_API_URL = 'https://graph.facebook.com/v2.6/me/messages'
VERIFY_TOKEN = parameters['VERIFY_TOKEN'] # <paste your verify token here>
PAGE_ACCESS_TOKEN = parameters['PAGE_ACCESS_TOKEN']# paste your page access token here>"
# API Endpoints
generateAuthTokenUrl = "https://dev-botlhale.io/generateAuthToken"
connectUrl = "https://dev-botlhale.io/connect"
sendMessageUrl = "https://dev-botlhale.io/message"
# Chatbot Constants
BotID = parameters['BotID']
refreshToken = parameters['refreshToken']
LanguageCode = parameters['LanguageCode']
parameters['MessageType'] = 'text'
MessageType = parameters['MessageType']
ResponseType = parameters['ResponseType']
open('config.json', 'w').write(json.dumps(parameters))
@app.before_first_request
def before_first_request():
generateAuthToken()
x=datetime.today()
y = x.replace(day=x.day, hour=0, minute=5, second=0, microsecond=0)
delta_t=y-x
secs=delta_t.total_seconds()
print('secs', secs)
t = Timer(secs, generateAuthToken)
t.start()
@app.route("/webhook",methods=['GET','POST'])
def listen():
"""This is the main function flask uses to
listen at the `/webhook` endpoint"""
if request.method == 'GET':
return verify_webhook(request)
if request.method == 'POST':
SpeechFile = None
text = None
payload = request.json
event = payload['entry'][0]['messaging']
for x in event:
if is_user_message(x) or is_postback(x) or is_speech(x):
with open('config.json') as json_file:
parameters = json.load(json_file)
# Configure response
if is_user_message(x):
text = x['message']['text']
sender_id = x['sender']['id']
elif is_postback(x):
text = x['postback']['payload']
sender_id = x['sender']['id']
elif is_speech(x):
sender_id = x['sender']['id']
SpeechUrl = x['message']['attachments'][0]['payload']['url']
urllib.request.urlretrieve(SpeechUrl, '{}.wav'.format(sender_id))
SpeechFile = '{}.wav'.format(sender_id)
parameters['MessageType'] = 'speech'
open('config.json', 'w').write(json.dumps(parameters))
# Chatbot Constants
with open('config.json') as json_file:
parameters = json.load(json_file)
BotID = parameters['BotID']
LanguageCode = parameters['LanguageCode']
MessageType = parameters['MessageType']
ResponseType = parameters['ResponseType']
IdToken = parameters['IdToken']
payload={
'BotID': BotID,
'LanguageCode': LanguageCode,
'FBsenderID': sender_id,
'MessageType': MessageType,
'ResponseType': ResponseType,
'TextMsg': text
}
try:
files=[
('SpeechFile',(SpeechFile,open(SpeechFile,'rb'),'audio/wav'))
]
except:
files=[
]
print('payload', payload)
headers = {"Authorization": "Bearer {}".format(IdToken)}
response = json.loads(requests.request("POST", sendMessageUrl, headers=headers, data=payload, files=files).content)
print('response',response)
for text in response['TextResponse']:
if response['TextResponse'].index(text) == len(response['TextResponse'])-1:
respond(sender_id, text, response['Buttons'])
else:
respond(sender_id, text, False)
return "ok"
if __name__ == "__main__":
app.debug = True
app.run(port='5000')