Created
January 4, 2020 20:42
-
-
Save FHRNet/e9a64324edba95f70efb40989256e1a5 to your computer and use it in GitHub Desktop.
HetrixTools - Make Twilio calls through webhooks with AWS Lambda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# This microservice links HetrixTools and Twilio together through webhooks. | |
# If HetrixTools detects downtime, this microservice is called, which calls your phone/SIP number | |
# and announces which monitor detected an issue using TTS. | |
# | |
# GET Parameters: | |
# - token = Must match SecretKey | |
# - repeat = How many times should the message be repeated (optional, default 2) | |
# - to = Which number / SIP destination to call | |
# | |
# Environment variables: | |
# - ApplicationSid = Twilio ApplicationSid | |
# - TwilioAuthToken = self explanatory | |
# - SecretKey = An auth token, see the get parameter "token" | |
# - From = A Twilio verified phone number, alternatively an arbitrary string if calling SIP | |
# | |
# Runtime: Python2.7. | |
# Have to manually install requests, Lambda doesn't support it by default | |
# `pip install requests -t .` and then upload the whole directory (including lambda_function.py) | |
# to Lambda in a ZIP. | |
import json | |
import logging | |
import os | |
import requests | |
from requests.auth import HTTPBasicAuth | |
secret_key = os.environ['SecretKey'] | |
logger = logging.getLogger() | |
logger.setLevel(logging.INFO) | |
def respond(err, res=None): | |
return { | |
'statusCode': '400' if err else '200', | |
'body': err.message if err else json.dumps(res), | |
'headers': { | |
'Content-Type': 'application/json', | |
}, | |
} | |
def lambda_handler(event, context): | |
decoded = json.loads(event['body']) | |
if(secret_key != event['queryStringParameters']['token']): | |
return respond("Invalid token") | |
if 'repeat' not in event['queryStringParameters']: | |
repeats = 2 | |
else: | |
repeats = int(event['queryStringParameters']['repeat']) | |
# TTS response | |
msg = '<Response><Say voice="alice">' | |
msg += 'Hello Administrator, a downtime event has been detected......' | |
for i in range(0, repeats): | |
msg += 'Warning.....' | |
msg += 'Monitor %s is OFFLINE...' % decoded['monitor_name'] | |
msg += '...' | |
msg += 'Hope you have a wonderful day.' | |
msg += '</Say></Response>' | |
data = { | |
'To': event['queryStringParameters']['to'], | |
'From': os.environ['From'], | |
'Twiml': msg | |
} | |
auth = HTTPBasicAuth(os.environ['ApplicationSid'], os.environ['TwilioAuthToken']) | |
response = requests.post('https://api.twilio.com/2010-04-01/Accounts/%s/Calls.json' % os.environ['ApplicationSid'], data=data, auth=auth) | |
print(response) | |
return respond(None, "OK") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment