Initial script

This commit is contained in:
Thomas Wade 2018-10-08 00:55:05 +10:30
parent dac28e85da
commit b0aca94d03

78
app.py Normal file
View File

@ -0,0 +1,78 @@
#!/usr/bin/python3
from flask import Flask, render_template, request, jsonify
import requests
app = Flask('GrafanaTextNotif')
@app.route('/notify/<dstnumber>', 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)