From 50b6d33cbca7e2fe86a5fb5ca53870823bb73e42 Mon Sep 17 00:00:00 2001 From: Maria Hernandez Date: Sun, 26 Jul 2020 12:18:35 -0500 Subject: [PATCH 1/2] Feature added. Request glucose level entry via sms --- notifier.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/notifier.py b/notifier.py index 2e6d116..c039cf9 100644 --- a/notifier.py +++ b/notifier.py @@ -271,10 +271,69 @@ def call_glucose_alert(to, glucose): 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() From 1cc6989adb11b9c7b8f72666cf80b7e150b5339a Mon Sep 17 00:00:00 2001 From: Maria Hernandez Date: Sun, 26 Jul 2020 12:19:48 -0500 Subject: [PATCH 2/2] ast package imported --- notifier.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notifier.py b/notifier.py index c039cf9..2267bc2 100644 --- a/notifier.py +++ b/notifier.py @@ -1,5 +1,5 @@ import nexmo -import json +import json, ast import os from flask import Flask, request, jsonify, render_template, session, redirect, url_for from flask_cors import CORS