Lambda Function To Route Twilio Incoming Phone Calls

The absolute fastest way to create a serverless Twilio incoming call webhook Lambda:

npm install -g serverless sls install --url https://github.com/revmischa/slspy --name twilcall echo "twilio" > requirements.txt
Code language: PHP (php)

handler.py:

from twilio.twiml.voice_response import VoiceResponse from urllib.parse import parse_qsl def hello(event, context): call = dict(parse_qsl(event['body'])) resp = VoiceResponse() resp.say("hello world!", voice='alice') response = { "headers": {"content-type": "text/xml"}, "statusCode": 200, "body": str(resp), } return response
Code language: JavaScript (javascript)

Deploy, then set webhook handler for your phone number:

sls deploy

That’s all!

Customize your response with TwiML.

See this gist to check out more advanced handling with loading the Twilio API key from Secrets Manager, doing lookups with Twilio Add-Ons to detect spam/robocalling, and detailed caller lookup info that is output to a slack channel every call.

Leave a Reply