diff options
author | Anonymous <anon@anon.com> | 2021-12-06 15:46:34 +0100 |
---|---|---|
committer | Anonymous <anon@anon.com> | 2021-12-06 15:46:34 +0100 |
commit | 35236b9804b505291c39ebf870f66d8477ce1cd2 (patch) | |
tree | 0c17bf6df73990162b2d23354a95d45ebda13310 /agenda | |
parent | 8c7396cd5556e8339a553e5b9315fd5e95d69316 (diff) |
Created build sms gateway on puyak.r with gammu (markdown)
Diffstat (limited to 'agenda')
-rw-r--r-- | agenda/build sms gateway on puyak.r with gammu.md | 63 |
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 |