> ## 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.

# Voice Call Router

> Build a Telnyx Edge Compute function that routes incoming voice calls based on time of day, caller location, or custom business logic with TeXML responses.

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

```bash theme={null}
telnyx-edge new-func -l=python -n=call-router
cd call-router
```

## Step 2: Add Dependencies

Create `requirements.txt`:

```txt theme={null}
pytz>=2024.1
```

## Step 3: Implement Call Routing

```python theme={null}
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

```bash theme={null}
telnyx-edge ship
```

Configure your Telnyx number to use this function as the TeXML webhook.
