Check out our upcoming events and meetups! View events →
Route calls based on time of day and caller location.
telnyx-edge new-func -l=python -n=call-router cd call-router
requirements.txt
pytz>=2024.1
from datetime import datetime import pytz class Function: async def handler(self, request): webhook = await request.json() caller = webhook.get("from", "") # Determine routing destination = self.get_destination(caller) # Generate TeXML response texml = f"""<?xml version="1.0" encoding="UTF-8"?> <Response> <Say>Please hold while we connect your call.</Say> <Dial timeout="30"> <Number>{destination}</Number> </Dial> <Say>We're sorry, no one is available. Please try again later.</Say> <Hangup/> </Response>""" return { "status": 200, "headers": {"Content-Type": "application/xml"}, "body": texml } def get_destination(self, caller): # Time-based routing est = pytz.timezone("America/New_York") now = datetime.now(est) hour = now.hour # Business hours: 9am-5pm EST if 9 <= hour < 17: # Route to main office return "+15551234567" else: # Route to after-hours support return "+15559876543" # Geographic routing example: # if caller.startswith("+44"): # return "+441onal support" # return "+1 US support"
telnyx-edge ship
Was this page helpful?