/
app.py
308 lines (228 loc) · 10.9 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
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
import os
import sys
import json
import requests
from flask import Flask, request
from witwrap import configure_wit, parse_message
app = Flask(__name__)
w = configure_wit()
def exception_handler(e_type, e_str, trace):
return "ok", 200
sys.excepthook = exception_handler
@app.route('/', methods=['GET'])
def verify():
# when the endpoint is registered as a webhook, it must echo back
# the 'hub.challenge' value it receives in the query arguments
if request.args.get("hub.mode") == "subscribe" and request.args.get("hub.challenge"):
if not request.args.get("hub.verify_token") == os.environ["VERIFY_TOKEN"]:
return "Verification token mismatch", 403
return request.args["hub.challenge"], 200
return "Hello world", 200
@app.route('/', methods=['POST'])
def webhook():
# endpoint for processing incoming messaging events
data = request.get_json()
if data["object"] == "page":
for entry in data["entry"]:
for messaging_event in entry["messaging"]:
if messaging_event.get("message"): # someone sent us a message
sender_id = messaging_event["sender"]["id"] # the facebook ID of the person sending you the message
try:
message_text = messaging_event["message"]["text"] # the message's text
except:
send_message(sender_id, "Sorry, this format is not supported.")
return "ok", 200
if message_text.lower() == "refresh":
requests.post("http://www.anyonetrades.com/alerts.php", verify=False)
return "ok", 200
# get output from wit.ai
try:
wit_out = parse_message(message_text, w)
except NameError:
send_message(sender_id, "I could not find a stock with that name.")
return "ok", 200
except:
send_message(sender_id, "Sorry, couldnt quite figure that out, would you mind rephrasing?")
return "ok", 200
log(wit_out)
# add our password and user id to the json
wit_out['password'] = os.environ["PHPPASSWORD"]
wit_out['u_id'] = sender_id
# adding rule
if wit_out.has_key('change'):
if wit_out.has_key('percent'):
del wit_out['percent']
wit_out['type'] = 'percent'
elif wit_out['change'] == 'reaches':
wit_out['type'] = 'absolute'
elif wit_out['change'] == 'up' or wit_out['change'] == 'down':
wit_out['type'] = 'relative'
# metric was specified
if wit_out.has_key('metric'):
wit_out['a'] = key_to_lang(wit_out.pop('metric'))
# if needs time, add it
if wit_out['a'] == 'move' or wit_out['a'] == 'weight':
if wit_out.has_key('number') and type(wit_out['number']) is list:
wit_out['a'] += str(wit_out['number'][0])
else:
wit_out['a'] += '10'
else:
wit_out['a'] = 'value'
if wit_out.has_key('number') and type(wit_out['number']) is list:
wit_out['b'] = wit_out['number'][-1]
del wit_out['number']
else:
try:
wit_out['b'] = wit_out.pop('number')
except:
wit_out['b'] = 0
log(wit_out)
r = requests.post("http://www.anyonetrades.com/api/create_alert.php",
data=wit_out, verify=False)
send_message(sender_id, r.text)
elif wit_out.has_key('lesser') and wit_out.has_key('greater'):
wit_out['type'] = 'variables'
if type(wit_out['greater']) == list:
wit_out['a'] = key_to_lang(wit_out['greater'][0]) + str(wit_out['greater'][1])
elif type(wit_out['greater']) == int or type(wit_out['greater']) == float:
wit_out['a'] = wit_out.pop('greater')
wit_out['type'] = 'absolute'
else:
wit_out['a'] = key_to_lang(wit_out['greater']) + '10'
if type(wit_out['lesser']) == list:
wit_out['b'] = key_to_lang(wit_out['lesser'][0]) + str(wit_out['lesser'][1])
elif type(wit_out['lesser']) == int or type(wit_out['lesser']) == float:
wit_out['b'] = wit_out.pop('lesser')
wit_out['type'] = 'absolute'
else:
wit_out['b'] = key_to_lang(wit_out['lesser']) + '10'
log(wit_out)
r = requests.post("http://www.anyonetrades.com/api/create_alert.php",
data=wit_out, verify=False)
send_message(sender_id, r.text)
# getting stats
elif wit_out.has_key('utils'):
r = requests.post("http://www.anyonetrades.com/api/get_alerts.php",
data=wit_out, verify=False).json()
send_message(sender_id, "You have %s alerts. For more info, visit %s" % (r['num'],r["url"]))
# querying current values
elif wit_out.has_key('stock'):
if not wit_out.has_key('metric'):
wit_out['metric'] = 'value'
s = "%s %s" % (wit_out['stock'], wit_out['metric'])
wit_out['metric'] = key_to_lang(wit_out['metric'])
if (wit_out['metric'] == 'move' or wit_out['metric'] == 'weight'):
if not wit_out.has_key('number'):
wit_out['number'] = 10
s += " over %s days" % (wit_out['number'])
wit_out['metric'] += str(wit_out['number'])
del wit_out['number']
log(wit_out)
r = requests.post("http://www.anyonetrades.com/api/get_info.php",
data=wit_out, verify=False).text
if r:
s += " is %s" % (r)
send_message(sender_id, s)
else:
send_message(sender_id, "Sorry, I could not find that stock")
else:
send_message(sender_id, "Sorry, couldnt quite figure that out, would you mind rephrasing?")
return "ok", 200
@app.route('/notify', methods=['POST'])
def notifyhook():
u_id = request.form.get('u_id')
text = request.form.get('text')
send_message(u_id, text)
return "ok", 200
@app.route('/update', methods=['POST'])
def updatehook():
sender_id = request.form.get('u_id')
message_text = request.form.get('query') # the message's text
# need to send text to wit
try:
wit_out = parse_message(message_text, w)
except:
return "Failed", 400
log(wit_out)
# add our password and user id to the json
wit_out['password'] = os.environ["PHPPASSWORD"]
wit_out['u_id'] = sender_id
# adding rule
if wit_out.has_key('change'):
if wit_out.has_key('percent'):
del wit_out['percent']
wit_out['type'] = 'percent'
elif wit_out['change'] == 'reaches':
wit_out['type'] = 'absolute'
elif wit_out['change'] == 'up' or wit_out['change'] == 'down':
wit_out['type'] = 'relative'
# metric was specified
if wit_out.has_key('metric'):
wit_out['a'] = key_to_lang(wit_out.pop('metric'))
# if needs time, add it
if wit_out['a'] == 'move' or wit_out['a'] == 'weight':
if type(wit_out['number']) is list:
wit_out['a'] += str(wit_out['number'][0])
else:
wit_out['a'] += '10'
else:
wit_out['a'] = 'value'
if type(wit_out['number']) is list:
wit_out['b'] = wit_out['number'][-1]
del wit_out['number']
else:
wit_out['b'] = wit_out.pop('number')
log(wit_out)
r = requests.post("http://www.anyonetrades.com/api/create_alert.php",
data=wit_out, verify=False)
elif wit_out.has_key('lesser') and wit_out.has_key('greater'):
wit_out['type'] = 'variables'
if type(wit_out['greater']) == list:
wit_out['a'] = key_to_lang(wit_out['greater'][0]) + str(wit_out['greater'][1])
else:
wit_out['a'] = key_to_lang(wit_out['greater']) + '10'
if type(wit_out['lesser']) == list:
wit_out['b'] = key_to_lang(wit_out['lesser'][0]) + str(wit_out['lesser'][1])
else:
wit_out['b'] = key_to_lang(wit_out['lesser']) + '10'
del wit_out['lesser']
del wit_out['greater']
log(wit_out)
r = requests.post("http://www.anyonetrades.com/api/create_alert.php",
data=wit_out, verify=False)
else:
return "Failed", 400
send_message(sender_id, r.text)
return "ok", 200
def send_message(recipient_id, message_text):
# log("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:
log(r.status_code)
log(r.text)
def key_to_lang(key):
if key == 'weighted moving average':
return 'weight'
elif key == 'simple moving average':
return 'move'
else:
return key
def log(message): # simple wrapper for logging to stdout on heroku
print str(message)
sys.stdout.flush()
if __name__ == '__main__':
app.run(debug=True)