forked from superdiana/nexmo-scout
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnotifier.py
More file actions
487 lines (414 loc) · 16.8 KB
/
notifier.py
File metadata and controls
487 lines (414 loc) · 16.8 KB
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
import nexmo
import json, ast
import os
from flask import Flask, request, jsonify, render_template, session, redirect, url_for
from flask_cors import CORS
from google.oauth2 import id_token
# from google.auth.transport import requests
import google.auth.transport.requests
import requests
from requests.auth import HTTPBasicAuth
from os.path import join, dirname
from dotenv import load_dotenv
import schedule
import time
# For UTC ISOFORMAT CALCULATIONS - Nightscout use another isoformat so we use the next modules to
# convert propertly and get most exact calculations
from datetime import datetime
import calendar
# We are going to use this to create an individual Thread for our Job
from multiprocessing import Process
# We detect if the user press ctl+c to stop the application
# If this keys are pressed then stop schedule threaded task
import signal
# We will use this to get a unique identifier for the schedule process
import uuid
# Init the flask App
app = Flask(__name__)
# Import Scout models for the app context
with app.app_context():
import models
from models import model, scouts, scout
# enable cors
cors = CORS(app)
# define secret_key to flask app to manage sessions
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
# load environment
envpath = join(dirname(__file__), "./.env")
load_dotenv(envpath)
# Load nexmo client for global usage in server
client = nexmo.Client(
application_id=os.getenv('NEXMO_APPLICATION_ID'),
private_key=os.getenv('NEXMO_PRIVATE_KEY').replace('\\n','\n')
)
active_scouts = []
# Login Logic
def get_session(key):
value = None
if key in session:
value = session[key]
return value
@app.route('/', methods=['GET', 'POST'])
def home():
# Create scouts instance
nightscouts = scouts()
if get_session("user") != None:
if request.method == "POST":
extra_contacts = request.form.getlist('extra_contacts[]')
if request.form.get("cmd") == "new":
nightscouts.add(scout(email=get_session("user")["email"], username=get_session("user")["username"], nightscout_api=request.form.get(
'nightscout_api'), phone=request.form.get('phone'), emerg_contact=request.form.get('emerg_contact'), extra_contacts=extra_contacts))
else:
nightscouts.update({u'nightscout_api': request.form.get('nightscout_api'), u'phone': request.form.get(
'phone'), u'emerg_contact': request.form.get('emerg_contact'), u'extra_contacts': extra_contacts}, request.form.get('id'))
return render_template("home.html", client_id=os.getenv("GOOGLE_CLIENT_ID"), user=get_session("user"), scout=nightscouts.get_by_email(get_session("user")["email"]))
else:
return render_template("login.html", client_id=os.getenv("GOOGLE_CLIENT_ID"), site_url=os.getenv("SITE_URL"))
# get the token_id and if valid then create the server session for persistance login
@app.route('/login', methods=["POST"])
def login():
try:
token = request.form.get("idtoken")
client_id = os.getenv("GOOGLE_CLIENT_ID")
infoid = id_token.verify_oauth2_token(
token, google.auth.transport.requests.Request(), client_id)
if infoid['iss'] not in ['accounts.google.com', 'https://accounts.google.com']:
raise ValueError('Wrong issuer.')
userid = infoid['sub']
# Here is a good place to create the session
session["user"] = {"userid": userid, "username": request.form.get(
"username"), "email": request.form.get("email")}
return userid
except ValueError:
return "Error"
pass
# user logout
@app.route('/logout')
def logout():
# Remove the session
session.pop("user", None)
return redirect(url_for('home', _external=True, _scheme='https'))
# handle the notifications when nightscout api is not sending new entries.
# when this mark is 1 the sms is sent, when the mark is equal to WAIT_AFTER_SMS_MARK the mark is restarted to 1
nightscout_failed_update_wait_mark = {}
def handle_nightscout_failed_update(to, api_url, username):
global client
global nightscout_failed_update_wait_mark
if to not in nightscout_failed_update_wait_mark:
nightscout_failed_update_wait_mark[to] = 1
else:
nightscout_failed_update_wait_mark[to] += 1
if nightscout_failed_update_wait_mark[to] == 1:
response = requests.post(
'https://api.nexmo.com/v0.1/messages',
auth=HTTPBasicAuth(os.getenv("NEXMO_API_KEY"),
os.getenv("NEXMO_API_SECRET")),
headers={
"Content-Type": "application/json",
"Accept": "application/json"
},
json={
"from": {
"type": "sms",
"number": os.getenv("NEXMO_NUMBER"),
},
"to": {
"type": "sms",
"number": to
},
"message": {
"content": {
"type": "text",
"text": "Dear {0} the nightscout api is not retrieving updated entries, please check your device or your service to solve any issues".format(username)
}
}
}
).json()
elif nightscout_failed_update_wait_mark[to] == int(os.getenv("WAIT_AFTER_SMS_MARK")):
nightscout_failed_update_wait_mark[to] = 0
# handle the number of failed attempts to customer nightscout api. If the number of intents exceeds the
# limit then a sms is sent to the customer
nightscout_failed_pings = {}
def handle_nightscout_failed_pings(to, api_url, username):
global client
global nightscout_failed_pings
if to not in nightscout_failed_pings:
nightscout_failed_pings[to] = 1
else:
nightscout_failed_pings[to] += 1
# print('Intent: {0} for {1}'.format(nightscout_failed_pings[to],to))
if nightscout_failed_pings[to] == int(os.getenv("NEXMO_FAILED_PING_SMS")):
response = requests.post(
'https://api.nexmo.com/v0.1/messages',
auth=HTTPBasicAuth(os.getenv("NEXMO_API_KEY"),
os.getenv("NEXMO_API_SECRET")),
headers={
"Content-Type": "application/json",
"Accept": "application/json"
},
json={
"from": {
"type": "sms",
"number": os.getenv("NEXMO_NUMBER"),
},
"to": {
"type": "sms",
"number": to
},
"message": {
"content": {
"type": "text",
"text": "Dear {0} the nexmo api url: {1} is not responding, please check the service".format(username, api_url)
}
}
}
).json()
# Reset the variable
nightscout_failed_pings[to] = 0
if "message_uuid" in response:
return True
return False
def sms_glucose_alert(to, username, glucose):
global client
# We send our sms using the messages api not the sms api
response = requests.post(
'https://api.nexmo.com/v0.1/messages',
auth=HTTPBasicAuth(os.getenv("NEXMO_API_KEY"),
os.getenv("NEXMO_API_SECRET")),
headers={
"Content-Type": "application/json",
"Accept": "application/json"
},
json={
"from": {
"type": "sms",
"number": os.getenv("NEXMO_NUMBER"),
},
"to": {
"type": "sms",
"number": to
},
"message": {
"content": {
"type": "text",
"text": "Alert {username} Blood Glucose is {glucose}".format(username=username, glucose=glucose)
}
}
}
).json()
if "message_uuid" in response:
return True
else:
return False
last_call = {}
def call_glucose_alert(to, glucose):
if to in last_call:
if int(time.time()-last_call[to]) < int(os.getenv("WAIT_AFTER_CALL")):
print("The number {0} was called recently.. Please wait a little longer: {1}".format(
to, int(time.time()-last_call[to])))
return False
# print('Call {0} {1}'.format(to, glucose))
last_call[to] = time.time()
# We make our call using the voice API
response = client.create_call(
{
"to": [{
"type": "phone",
"number": to
}],
"from": {
"type": "phone",
"number": os.getenv('NEXMO_NUMBER')
},
"ncco": [
{
"action": "talk",
"text": "Alert Your Blood Glucose is {0}".format(glucose)
}
],
"eventUrl": [
"{url_root}/webhooks/events".format(
url_root=os.getenv("SITE_URL"))
]
}
)
if "uuid" in response:
return True
else:
return False
def sms_request_glucose_level(args, glucose):
global client
args = ast.literal_eval(json.dumps(args))
# Parse incoming values
keyword = args['keyword'][0]
to = args["msisdn"][0]
response = None
if keyword == "NIGHTSCOUT":
# "msg" stores the response to be sent\
msg = "Hey, the latest blood glucose level entry: {glucose}".format(glucose=glucose)
response = requests.post(
'https://api.nexmo.com/v0.1/messages',
auth=HTTPBasicAuth(os.getenv("NEXMO_API_KEY"),
os.getenv("NEXMO_API_SECRET")),
headers={
"Content-Type": "application/json",
"Accept": "application/json"
},
json={
"from": {
"type": "sms",
"number": os.getenv('NEXMO_NUMBER')
},
"to": {
"type": "sms",
"number": to
},
"message": {
"content": {
"type": "text",
"text": msg
}
}
}).json()
if response is not None and "message_uuid" in response:
return True
else:
return False
@app.route('/webhooks/inbound-messages', methods=["POST"])
def inbound_sms():
args = dict(request.form)
if args.get('status'):
return args['status']
sms_request_glucose_level(args, glucose)
return "Message Received"
@app.route('/webhooks/events', methods=["POST", "GET"])
def events():
global client
global active_scouts
global glucose
req = request.get_json()
# Create scouts instance
nightscouts = scouts()
print(req)
if "status" in req:
if req["status"] == "completed":
phone = req["to"]
# The next line is not recomended its functional but use the global active_scouts variable
# This variable is updated by the daemon process.. that run in another context
# Not the flask context, for that reason we are going to use firebase to get
# fresh data
# uscout = [active_scout for active_scout in active_scouts if active_scout['phone'] == phone]
uscout = nightscouts.getby_personal_phone(phone)
if uscout != None:
entries = requests.get(uscout["nightscout_api"]).json()
glucose = entries[0]['sgv']
sms_glucose_alert(
uscout["emerg_contact"], uscout["username"], glucose)
# print('sms simulation to: {0} {1} {2}'.format(uscout["emerg_contact"], uscout["username"], glucose))
for phone in uscout["extra_contacts"]:
# print('sms simulation to: {0} {1} {2}'.format(phone, uscout["username"], glucose))
sms_glucose_alert(phone, uscout["username"], glucose)
return "Event Received"
# Schedule Logic
# thread global var
thread = None
class ApplicationKilledException(Exception):
pass
# Signal handler
def signal_handler(signum, frame):
raise ApplicationKilledException("Killing signal detected")
start_scouts = False
def refresh_scouts(id):
global active_scouts
global start_scouts
# Import Scout models for thread
import models
from models import model, scouts, scout
try:
nightscouts = scouts()
active_scouts = nightscouts.get_all()
print("Refresh Scouts Job " + id + "")
if start_scouts == False:
start_scouts = True
except:
print("Error when refresh scouts")
# This job is executed certain periods of time to get the last nightscout entry
# If the glucose is not between the range then make a call to the personal phone notifiying the level
# If the call is not answered then events url is going to serve a NCCO sending SMS to the emergency number
# And if applicable any extra contacts
def job(id):
# Calling nemo global client variable
global client
global active_scouts
global nightscout_failed_pings
global nightscout_failed_update_wait_mark
global start_scouts
print("Alerts Job " + id + "")
if active_scouts != None:
if start_scouts == False:
refresh_scouts('Starting_Scouts')
for active_scout in active_scouts:
print(active_scout["nightscout_api"])
try:
entries = requests.get(active_scout["nightscout_api"]).json()
glucose = entries[0]['sgv']
# No failed connection, so initialise the failed pings to zero for the active user
nightscout_failed_pings[active_scout["phone"]] = 0
# Check if the last NIGHTSCOUT entry doesn't exceed the NIGHTSCOUT NOT_UPDATE SECONDS limit.
# In other words, this code verifies if nightscout entries get updates
current_isoformat_datetime = datetime.utcnow().isoformat()
nightscout_datetime = entries[0]['dateString']
# Little trick to make UTC isodate from nodejs moment library compatible with python isoformat
nightscout_isoformat_datetime = nightscout_datetime.replace(
"Z", "+00:00")
# Difference in seconds between current time and last nightscout entry
time_difference = calendar.timegm(datetime.fromisoformat(current_isoformat_datetime).utctimetuple(
)) - calendar.timegm(datetime.fromisoformat(nightscout_isoformat_datetime).utctimetuple())
print(time_difference, " ", os.getenv(
"NIGHTSCOUT_NOT_UPDATE_SECONDS"))
if time_difference > int(os.getenv("NIGHTSCOUT_NOT_UPDATE_SECONDS")):
raise Exception("entry_update")
else:
nightscout_failed_update_wait_mark[active_scout["phone"]] = 0
# We add a dynamic attribute called glucose to pass glucose info to events url
if 70 <= glucose <= 240:
print("{0} is inside the range for {1}".format(
glucose, active_scout["username"]))
else:
print("Executing emergency call and loading sms NCCO if needed")
call_glucose_alert(active_scout["phone"], glucose)
except KeyError:
print("Key error")
except Exception as instance:
if instance.args[0] == 'entry_update':
handle_nightscout_failed_update(
active_scout["phone"], active_scout["nightscout_api"], active_scout["username"])
print(
"No updated entries for user, executing the failed update handler for user " + active_scout["username"])
else:
handle_nightscout_failed_pings(
active_scout["phone"], active_scout["nightscout_api"], active_scout["username"])
print("Server could not establish connection with " +
active_scout["nightscout_api"])
# Manage individual schedule Thread Loop
def run_schedule():
global thread
while True:
try:
schedule.run_pending()
time.sleep(1)
except ApplicationKilledException:
print("Signal Detected: Killing Nightscout-Nexmo App.")
# clean the schedule
schedule.clear()
return "Thread Killed"
if __name__ == "notifier":
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
# run job at second 30 of each minute
schedule.every().minute.at(':30').do(job, str(uuid.uuid4()))
# update scouts each our at minute 00
schedule.every().hour.at(':00').do(refresh_scouts, str(uuid.uuid4()))
thread = Process(target=run_schedule)
thread.start()
print("Nightscout-Nexmo Thread starts")