#!/usr/bin/python3 from flask import Flask, render_template, request, jsonify import requests app = Flask('GrafanaTextNotif') @app.route('/notify/', methods=['POST']) def post_notify(dstnumber): try: json = request.get_json() id = request.authorization.username secret = request.authorization.password title = json['title'] message = json['message'] # print('ID: ' + id, \ # '\nSecret: ' + secret, \ # '\nTitle: ' + title, \ # '\nMessage: ' + message) headers = {'Content-Type': 'application/x-www-form-urlencoded'} data = { \ 'client_id':id, \ 'client_secret':secret, \ 'grant_type':'client_credentials' \ } response = requests.post('https://tapi.telstra.com/v2/oauth/token', headers=headers, data=data) if not response.status_code == 200: print('Failed to get token') print(response.text) return '', response.status_code access_token = response.json()['access_token'] token_type = response.json()['token_type'] ##### Get dedicated number ##################################################### headers = { \ 'Authorization':token_type + ' ' + access_token, \ 'Content-Type':'application/json' \ } response = requests.post('https://tapi.telstra.com/v2/messages/provisioning/subscriptions', headers=headers, data='{}') if not response.status_code in [201, 204]: print('Failed to get dedicated number') print(response.text) return '', response.status_code srcnumber = response.json()['destinationAddress'] ##### Send a text to the number ################################################ headers = { \ 'Authorization':token_type + ' ' + access_token, \ 'Content-Type':'application/json' \ } data = { \ 'to':dstnumber, \ 'body':title + '\n\n' + message, \ 'from':srcnumber \ } response = requests.post('https://tapi.telstra.com/v2/messages/sms', headers=headers, json=data) if not response.status_code == 201: print('Failed to send SMS') print(response.text) return '', response.status_code return '', 204 except: return '', 500 if __name__ == "__main__": app.run(port=5000)