forked from 96imranahmed/blackbox-public
/
bb_app.py
223 lines (198 loc) · 9.92 KB
/
bb_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
223
from flask import Flask
from flask import request
import json
import flask
import sys
sys.path.append('/pymessenger/')
from pymessenger.bot import Bot
from db import queryDB
from apiclient import discovery
from oauth2client import client
from dating import understand_sentence as u_s
from state_machine import state_machine
from StateInfo import StateInfo
URL = 'https://blackboxai.me/'
COMMANDS = ["clear", "howdy", "waddup", "neg"]
GREETINGS = ["hi", "hello", "hey", "yo", "howdy", "sup", "hey there"]
VALIDATION_TOKEN = 'super_secret_token'
ACCESS_TOKEN = 'EAACJ5DFzYO8BABXRv362CiYO2831qbhmgH0HXBWJImCunnkpoNY97U8qJNNLAzJKCJZBcxhoMKsvmobMRXQMY0yI8HZBmWJvbLOZCO9pFi4eDSBLIzQirqw2Qeoe8n9okA8nFYBZCZCKBZCgfkILIEqsqTIYhVk4p94L0NWdnaLwZDZD'
bot = Bot(ACCESS_TOKEN)
app = Flask(__name__)
app.secret_key = 'super_duper_secret_token'
app.config.update(dict(
PREFERRED_URL_SCHEME='https'
))
state_info = StateInfo(bot)
@app.route('/', methods = ['POST', 'GET'])
def welcome():
if request.method == 'GET':
try:
if request.args['hub.verify_token'] == VALIDATION_TOKEN:
return request.args['hub.challenge'], 200
else:
return 'Error - invalid token supplied', 403
except:
return 'Hey there! How about you try message me from Facebook Messenger?', 200
elif request.method == 'POST':
data = request.json
messages = data['entry'][0]['messaging']
for msg in messages:
if (msg.get('message') and msg['message'].get('text') and not 'is_echo' in msg['message'] and not 'delivery' in msg):
if not 'quick_reply' in msg['message'] and not 'attachments' in msg['message']:
message = msg['message']['text']
user_id = msg['sender']['id']
state_info.set_user(user_id)
print(message)
bot.send_mark_seen(user_id)
bot.send_typing(user_id, True)
if (message[0] == '/'):
message = message[1:].lower()
if (message in COMMANDS):
if message == "clear":
queryDB("del_user", user_id)
bot.send_text_message(user_id, "Your ID " + user_id + " has been removed from the database.")
elif message == "howdy":
bot.send_text_message(user_id, "Howdy fella")
elif message == "waddup":
bot.send_text_message(user_id, "it's ya boi")
elif message == "neg":
bot.send_text_message(user_id, "You look like you're going to spend your life having one epiphany after another, always thinking you've finally figured out what's holding you back, and how you can finally be productive and creative and turn your life around. But nothing will ever change. That cycle of mediocrity isn't due to some obstacle. It's who you are. The thing standing in the way of your dreams is; that the person having them is you.")
else:
bot.send_text_message(user_id, "Command not recognised :( ")
else:
if (queryDB('new_user_check', user_id) == False):
bot.send_text_message(user_id,
"Hey there " + user_id + " It's looking like this is your first message!")
queryDB('add_user', user_id)
if queryDB('get_user_name', user_id) == False:
bot.send_text_message(user_id,
"Hmm, I'm not a fan of calling you by your ID. How about you give me a proper name?")
else:
set = False
if queryDB('get_user_name', user_id) == '_is_updating_':
queryDB('set_user_name', [user_id, message])
bot.send_text_message(user_id, 'Great, I\'ll call you ' + message + ' from now on!')
set = True
name = queryDB('get_user_name', user_id)
if (message.lower() in GREETINGS):
if set == False:
bot.send_text_message(user_id, "Hey there " + name)
else:
# login on our server and on google
jacqueslog(user_id, bot)
# call NLP and state machine
try:
flow(message)
pass
except Exception as e:
print(e)
print('DEBUG')
bot.send_typing(user_id, False)
else:
#Handle custom reply
if 'quick_reply' in msg['message']:
payload = msg['message']['quick_reply']['payload']
print(payload)
#Quick replies
flow_hack(payload)
elif 'attachments' in msg['message']:
if msg['message']['attachments']['type'] == 'location':
payload = msg['message']['attachments']['payload']['coordinates']
latitude = payload['lat']
longitude = payload['long']
#Location
else:
pass
return 'Success!', 200
def jacqueslog(user_id, bot):
if (queryDB('google_cal_check', user_id) == False):
# edit the url below to be blackbox.../login flow
# define the function in another flask file
# this can redirect to google cloud login api
# oauth callback on google logs you in, allows the blackbox app to use the calendar
oauth_request_button = [
{
"type": "web_url",
"url": URL + "login?user_id=" + str(user_id),
"title": "Connect?",
"webview_height_ratio": "compact"
}
]
bot.send_button_message(user_id, "How about you connect your Google Calendar? :)",
oauth_request_button)
def flow(message):
state_info.processed_data, flag_empty = u_s.get_NLP(message)
print(state_info.processed_data)
intent = state_info.processed_data['intent']
print(intent)
state_info.flag_empty = flag_empty
state_machine.state_machine(state_info)
def flow_hack(message):
result = message[-1]
if result:
state_info.processed_data, flag_empty = u_s.get_intent(u_s.load_json("yes"))
else:
state_info.processed_data, flag_empty = u_s.get_intent(u_s.load_json("no"))
print(state_info.processed_data)
intent = state_info.processed_data['intent']
print(intent)
state_info.flag_empty = flag_empty
state_machine.state_machine(state_info)
@app.route('/done', methods=['GET'])
def calendar_connect_done():
return 'All done! You have succesfully connected your calendar!', 200
@app.route('/login')
def login():
user_id = request.args.get('user_id')
flask.session['user_id'] = user_id
if 'credentials' not in flask.session:
return flask.redirect(flask.url_for('oauth2callback'))
else:
credentials = None
try:
credentials = client.OAuth2Credentials.from_json(flask.session['credentials'])
except:
return flask.redirect(flask.url_for('oauth2callback'))
if credentials.access_token_expired:
return flask.redirect(flask.url_for('oauth2callback'))
else:
cur_cred = json.loads(flask.session['credentials'])
print(cur_cred)
refresh_token = cur_cred['refresh_token']
cur_token = cur_cred['access_token']
if refresh_token == None:
print(str(cur_token))
else:
queryDB('google_token_enter', [flask.session['user_id'], str(cur_token) + '||' + str(refresh_token)])
bot.send_text_message(flask.session['user_id'],
"Google calendar successfully setup - we're good to go woot! :D ")
return flask.redirect(flask.url_for('calendar_connect_done'))
@app.route('/oauth2callback')
def oauth2callback():
flow = client.flow_from_clientsecrets(
'client_secrets.json',
scope='https://www.googleapis.com/auth/calendar',
redirect_uri=URL + 'oauth2callback')
flow.params['approval_prompt'] = 'force'
flow.params['access_type'] = 'offline'
if 'code' not in flask.request.args:
auth_uri = flow.step1_get_authorize_url()
return flask.redirect(auth_uri)
else:
auth_code = flask.request.args.get('code')
credentials = flow.step2_exchange(auth_code)
flask.session['credentials'] = credentials.to_json()
cur_cred = json.loads(flask.session['credentials'])
refresh_token = cur_cred['refresh_token']
cur_token = cur_cred['access_token']
if refresh_token == None:
flask.session.pop('credentials', None)
bot.send_text_message(flask.session['user_id'], "Suis désolé - do you mind logging in again?")
return flask.redirect(flask.url_for('login?user_id=' + flask.session['user_id']))
else:
bot.send_text_message(flask.session['user_id'],
"Google calendar successfully setup - we're good to go woot! :D ")
queryDB('google_token_enter', [flask.session['user_id'], str(cur_token) + '||' + str(refresh_token)])
return flask.redirect(flask.url_for('calendar_connect_done'))
if __name__ == '__main__':
app.run(host="0.0.0.0", port=3000)