-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
notifier.py
670 lines (564 loc) · 24.2 KB
/
notifier.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
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
import nexmo
import twilio.rest
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
from env_flag import env_flag
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
from apscheduler.schedulers.background import BackgroundScheduler
import atexit
# 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'su\xdb\xf2\x94hK\x81J\x8c\xf7\x1e\xfb\x81F\xf1'
# load environment
envpath = join(dirname(__file__), "./.env")
load_dotenv(envpath)
# Load communication client for global usage in server
if env_flag('USE_TWILIO'):
client = twilio.rest.Client(os.getenv('TWILIO_ACCOUNT_SID'),
os.getenv('TWILIO_AUTH_TOKEN'))
else:
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[]')
use_whatsapp_list_indices = request.form.getlist('whatsapp-checkbox')
use_whatsapp_list_final = []
# print(use_whatsapp_list_indices)
for i in range(len(extra_contacts)):
use_whatsapp_list_final.append('No')
for s in use_whatsapp_list_indices:
use_whatsapp_list_final[int(s)] = 'Yes'
## print(use_whatsapp_list_final)
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, extra_contacts_use_whatsapp=use_whatsapp_list_final))
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, u'extra_contacts_use_whatsapp': use_whatsapp_list_final}, request.form.get('id'))
## print(nightscouts.get_by_email(get_session("user")["email"]))
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:
## print(os.getenv("SITE_URL"))
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:
if not env_flag('USE_TWILIO'):
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()
else:
message = client.messages \
.create(
body="Dear {0} the nightscout api is not retrieving updated entries, please check your device or your service to solve any issues".format(username),
from_=os.getenv("TWILIO_NUMBER"),
to=to
)
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("NIGHTSCOUT_FAILED_PING_SMS")):
# Reset the variable
nightscout_failed_pings[to] = 0
if not env_flag('USE_TWILIO'):
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 url: {1} is not responding, please check the service".format(username, api_url)
}
}
}
).json()
if "message_uuid" in response:
return True
else:
message = client.messages \
.create(
body="Dear {0} the Nightscout api url: {1} is not responding, please check the service".format(username, api_url),
from_=os.getenv("TWILIO_NUMBER"),
to=to
)
if message.status != 'failed':
return True
return False
def sms_glucose_alert(to, username, glucose):
global client
if not env_flag('USE_TWILIO'):
# 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:
message = client.messages \
.create(
body="Alert {username} Blood Glucose is {glucose}".format(username=username, glucose=glucose),
from_=os.getenv("TWILIO_NUMBER"),
to=to
)
if message.status != 'failed':
return True
return False
def whatsapp_glucose_alert(to, username, glucose):
global client
if not env_flag('USE_TWILIO'):
# 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": "whatsapp",
"number": os.getenv("NEXMO_WHATSAPP_NUMBER"),
},
"to": {
"type": "whatsapp",
"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:
message = client.messages \
.create(
body="Alert {username} Blood Glucose is {glucose}".format(username=username, glucose=glucose),
from_='whatsapp:' + os.getenv("TWILIO_WHATSAPP_NUMBER"),
to='whatsapp:' + to
)
if message.status != 'failed':
return True
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()
if not env_flag('USE_TWILIO'):
# 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:
call = client.calls.create(
method='GET',
twiml='<Response><Say>Alert Your Blood Glucose is {0}</Say></Response>'.format(glucose),
status_callback=os.getenv("SITE_URL") + "/webhooks/twilio-on-completed",
status_callback_event=['completed'],
status_callback_method='POST',
to=to,
from_=os.getenv("TWILIO_NUMBER")
)
if call.status != 'failed':
return True
return False
def sms_request_glucose_level_nexmo(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
def sms_request_glucose_level_twilio(keyword, to, glucose):
global client
if keyword == "NIGHTSCOUT":
# "msg" stores the response to be sent\
msg = "Hey, the latest blood glucose level entry: {glucose}".format(glucose=glucose)
message = client.messages \
.create(
body=msg,
from_=os.getenv("TWILIO_NUMBER"),
to=to
)
if message.status != 'failed':
return True
return False
# Nexmo webhooks
@app.route('/webhooks/inbound-messages', methods=["POST"])
def inbound_sms():
global glucose
args = dict(request.form)
if args.get('status'):
return args['status']
uscout = nightscouts.getby_personal_phone(to)
if uscout != None:
entries = requests.get(uscout["nightscout_api"]).json()
glucose = entries[0]['sgv']
sms_request_glucose_level_nexmo(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 index, phone in enumerate(uscout["extra_contacts"]):
# print('sms simulation to: {0} {1} {2}'.format(phone, uscout["username"], glucose))
if uscout['extra_contacts_use_whatsapp'][index] is not True:
sms_glucose_alert(phone, uscout["username"], glucose)
else:
whatsapp_glucose_alert(phone, uscout["username"], glucose)
return "Event Received"
# Twilio webhooks
@app.route('/webhooks/twilio-inbound-messages', methods=["POST", "GET"])
def incoming_sms():
global glucose
keyword = request.values.get('Body', '')
to = request.values.get('From', '')
uscout = nightscouts.getby_personal_phone(to)
if uscout != None:
entries = requests.get(uscout["nightscout_api"]).json()
glucose = entries[0]['sgv']
sms_request_glucose_level_twilio(keyword, to, glucose)
return "Message Received"
@app.route('/webhooks/twilio-on-completed', methods=["POST", "GET"])
def on_completed():
global active_scouts
global glucose
phone = request.values.get('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 index, phone in enumerate(uscout["extra_contacts"]):
# print('sms simulation to: {0} {1} {2}'.format(phone, uscout["username"], glucose))
if uscout['extra_contacts_use_whatsapp'][index] is not True:
sms_glucose_alert(phone, uscout["username"], glucose)
else:
whatsapp_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():
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")
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():
# 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")
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"])
def on_shutdown():
print("Signal Detected: Killing Nightscout-Nexmo App.")
scheduler.shutdown()
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
scheduler = BackgroundScheduler()
scheduler.add_job(func=job, trigger='cron', second=30)
scheduler.add_job(func=refresh_scouts, trigger='cron', hour='*')
scheduler.start()
print("Nightscout-Nexmo Thread starts")
atexit.register(on_shutdown)
##call_glucose_alert('14049561232', 50)
##handle_nightscout_failed_update('14049561232', 'test.com', 'vmalepati1')
##sms_glucose_alert('14049561232', 'vmalepati1', 65)
##whatsapp_glucose_alert('14049561232', 'vmalepati1', 65)
##sms_request_glucose_level_twilio('NIGHTSCOUT', '14049561232', 65)