Documentation Index
Fetch the complete documentation index at: https://developers.telnyx.com/llms.txt
Use this file to discover all available pages before exploring further.
Route incoming calls based on time of day, caller location, or custom logic.
What you’ll learn:
- TeXML for call control
- Time-based routing logic
- Geographic routing
Step 1: Create the Function
telnyx-edge new-func -l=python -n=call-router
cd call-router
Step 2: Add Dependencies
Create requirements.txt:
Step 3: Implement Call Routing
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"
Step 4: Deploy
Configure your Telnyx number to use this function as the TeXML webhook.