Skip to content

Instantly share code, notes, and snippets.

@rickheil
Created November 21, 2017 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickheil/387d80caf8b6711f114084e1ca741441 to your computer and use it in GitHub Desktop.
Save rickheil/387d80caf8b6711f114084e1ca741441 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
"""This is a lambda function that accepts web hooks from UptimeRobot
and translates them into SMS messages. Useful when you don't want to
pay UptimeRobot's highway robbery SMS fees.
Twilio code liberally lifted from their blog post on Lambda."""
import os
import json
from twilio.rest import Client
TWILIO_SMS_URL = "https://api.twilio.com/2010-04-01/Accounts/{}/Messages.json"
TWILIO_ACCOUNT_SID = os.environ.get("TWILIO_ACCOUNT_SID")
TWILIO_AUTH_TOKEN = os.environ.get("TWILIO_AUTH_TOKEN")
TWILIO_FROM_NUMBER = ""
def sms_handler(to_number, sms_body):
"""Takes a to number and body and sends it via the Twilio SMS API."""
if not TWILIO_ACCOUNT_SID:
return "Unable to access Twilio Account SID."
elif not TWILIO_AUTH_TOKEN:
return "Unable to access Twilio Auth Token."
elif not TWILIO_FROM_NUMBER:
return "No from phone number provided."
elif not to_number:
return "No recipient phone number provided."
elif not sms_body:
return "No message body provided."
sms_client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
message = sms_client.messages.create(
to=to_number,
from_=TWILIO_FROM_NUMBER,
body=sms_body)
return message.sid
#pylint: disable=unused-argument
def lambda_handler(event, context):
"""Takes a dict of info from the web hook and logics it
into individual notifications."""
# uncomment next line for debug
# to_number = "+1"
event_body = json.loads(event['body'])
if event_body['alertType'] == "1":
site_status = "DOWN"
else:
site_status = "UP"
sms_body = "UPTIME ROBOT: %s is %s on alert checker %s. (Details: %s)" % (
event_body['monitorURL'],
site_status,
event_body['monitorFriendlyName'],
event_body['alertDetails'])
# send the SMS now
sms_status = sms_handler(event_body['to_number'], sms_body)
return_json = (
"{'isBase64Encoded': 'false', "
"'statusCode': '200', "
"'headers': 'application/json', "
"'body': %s }" % sms_status)
return return_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment