mirror of
https://github.com/KevinMidboe/cloudflare-ddns.git
synced 2025-10-29 17:40:17 +00:00
Moved all source files to src/
This commit is contained in:
42
src/notify.py
Normal file
42
src/notify.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import base64
|
||||
import requests
|
||||
from os import getenv
|
||||
from json import dumps
|
||||
from logger import logger
|
||||
|
||||
|
||||
def notify(message):
|
||||
SMS_API_KEY = getenv('SMS_API_KEY')
|
||||
SMS_RECIPIENT = getenv('SMS_RECIPIENT')
|
||||
|
||||
if not SMS_API_KEY:
|
||||
logger.info("No SMS API key found, not notifying")
|
||||
return
|
||||
|
||||
if not SMS_RECIPIENT:
|
||||
logger.info("No SMS recipient found, not notifying")
|
||||
return
|
||||
|
||||
recipient = "47{}".format(SMS_RECIPIENT)
|
||||
apiKey = base64.b64encode(SMS_API_KEY.encode("utf-8")).decode("utf-8")
|
||||
|
||||
logger.info("Notifying of IP change over SMS")
|
||||
url = "https://gatewayapi.com/rest/mtsms"
|
||||
payload = {
|
||||
"sender": "Dynamic DNS",
|
||||
"message": message,
|
||||
"recipients": [{"msisdn": recipient}]
|
||||
}
|
||||
headers = {
|
||||
"Host": "gatewayapi.com",
|
||||
"Authorization": "Basic " + apiKey,
|
||||
"Accept": "application/json",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
|
||||
r = requests.post(url, data=dumps(payload), headers=headers)
|
||||
response = r.json()
|
||||
|
||||
logger.info("Response from SMS api")
|
||||
logger.info("Status: {} {}".format(str(r.status_code), str(r.reason)))
|
||||
logger.info(response)
|
||||
Reference in New Issue
Block a user