summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--agenda/build sms gateway on puyak.r with gammu.md63
1 files changed, 63 insertions, 0 deletions
diff --git a/agenda/build sms gateway on puyak.r with gammu.md b/agenda/build sms gateway on puyak.r with gammu.md
new file mode 100644
index 0000000..0b0c17e
--- /dev/null
+++ b/agenda/build sms gateway on puyak.r with gammu.md
@@ -0,0 +1,63 @@
+```
+#!/usr/bin/env python3
+
+import falcon
+import json
+import gammu
+import sys
+
+sm = gammu.StateMachine()
+sm.ReadConfig()
+sm.Init()
+
+
+class GammuSMS:
+ def on_post(self, req, resp):
+ try:
+ body = req.stream.read().decode()
+ print('raw:', body)
+ sys.stdout.flush()
+ data = json.loads(body)
+ print('json:', data, file=sys.stdout)
+ sys.stdout.flush()
+ except ValueError:
+ resp.body = json.dumps({'status': 'error', 'message': 'malformed json body'})
+ resp.status = falcon.HTTP_400
+ return
+
+ if 'number' not in data:
+ resp.body = json.dumps({'status': 'error', 'message': 'missing required parameter `number`'})
+ resp.status = falcon.HTTP_400
+
+ elif 'message' not in data:
+ resp.body = json.dumps({'status': 'error', 'message': 'missing required parameter `message`'})
+ resp.status = falcon.HTTP_400
+
+ else:
+ # replace leading '+' with '00'
+ if data['number'].startswith('+'):
+ data['number'] = "00{}".format(data['number'][2:])
+
+ if not data['number'].isdigit():
+ resp.body = json.dumps({'status': 'error', 'message': 'illegal number given: "{}"'.format(data['number'])})
+ resp.status = falcon.HTTP_400
+ return
+
+ message = {
+ 'SMSC': {'Location': 1},
+ 'Text': data['message'],
+ 'Number': data['number']
+ }
+
+ try:
+ id = sm.SendSMS(message)
+ resp.body = json.dumps({'status': 'ok', 'message': 'queued ({})'.format(id)})
+ resp.status = falcon.HTTP_200
+ except gammu.ERR_UNKNOWN as ex:
+ resp.body = json.dumps({'error': str(ex)})
+ resp.status = falcon.HTTP_500
+
+
+api = falcon.API()
+api.add_route('/sms', GammuSMS())
+``` \ No newline at end of file