Hi! What's the recommended pattern for routing cal...
# ask-ai
s
Hi! What's the recommended pattern for routing calls from multiple phone numbers to different agents using a single Inbound SIP Trunk? Goal: • Call to
+1-555-0101
->
Agent_A
• Call to
+1-555-0102
->
Agent_B
• ...all on the same trunk. My understanding is that I can't create a separate dispatch rule for each number on the same trunk, because the rules would conflict. It seems the
inboundNumbers
field is an allowlist for the caller's number (
From
), not the number being called (
To
).
t
Thanks for reaching out, manik. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To route calls from multiple phone numbers to different agents using a single Inbound SIP Trunk, here’s a simple way to do it: 1. Create an Inbound SIP Trunk: Set up the trunk so it accepts calls to all your phone numbers by listing them in the
numbers
array when you create it. 2. Use Dispatch Rules: Create dispatch rules that send calls to rooms with number-specific prefixes. For example, calls to +1-555-0101 can go to a room named call-5550101, and calls to +1-555-0102 can go to call-5550102. 3. Implement Agent Dispatch Logic: In your agent worker, check the SIP headers (like the
To
header) or room names to figure out which number was called. Then, send the right agent to that room. This way, you can easily manage call routes and connect callers to the right agents based on the number they dialed. Sources: Accepting incoming calls | LiveKit Docs | SIP inbound trunk | LiveKit Docs | Agent dispatch | LiveKit Docs | SIP inbound trunk | LiveKit Docs | livekit/livekit_sip.pb.go
s
Can you show an example of the dispatch rule for this use case?
t
Here is an example of a dispatch rule for routing calls based on the called number:
Copy code
{
  "rule": {
    "dispatchRuleCallee": {
      "roomPrefix": "number-",
      "randomize": false
    }
  },
  "name": "My dispatch rule"
}
Explanation: - This rule creates a room named number-<called-number> for each call. - The randomize field is set to false, so the room names stay predictable and easy to follow. If you'd like to send specific agents to these rooms, you can add a roomConfig with their details:
Copy code
{
  "rule": {
    "dispatchRuleCallee": {
      "roomPrefix": "number-",
      "randomize": false
    }
  },
  "name": "My dispatch rule",
  "roomConfig": {
    "agents": [
      {
        "agentName": "inbound-agent",
        "metadata": "job dispatch metadata"
      }
    ]
  }
}
This way, calls get routed to rooms based on the number called, and you can easily assign the right agents if you want. Let me know if you need a hand setting this up! Sources: SIP dispatch rule | LiveKit Docs
s
can you show an example of two full different dispatch rules that map to the same trunk but go to different agents?
t
Could you help me understand a bit more about how you'd like to tell the two dispatch rules apart? Maybe by specific called numbers, caller info, or something else? I'm here to make sure we get it just right!
s
Rule 1: If a call comes in for
+1-555-0101
, it should use a rule that dispatches
Agent_A
. Rule 2: If a call comes in for
+1-555-0102
on the same trunk, it should use a different rule that dispatches
Agent_B
.
t
Here are two examples of dispatch rules that use the same trunk but send calls to different agents depending on the number being called: Dispatch Rule 1: For calls to +1-555-0101, route to Agent_A:
Copy code
{
  "dispatch_rule": {
    "trunk_ids": [
      "trunk-123"
    ],
    "inbound_numbers": [
      "+15550101"
    ],
    "rule": {
      "dispatchRuleDirect": {
        "roomName": "agent-a-room"
      }
    },
    "roomConfig": {
      "agents": [
        {
          "agentName": "Agent_A",
          "metadata": "Handles calls for +1-555-0101"
        }
      ]
    }
  }
}
Dispatch Rule 2: For calls to +1-555-0102, route to Agent_B:
Copy code
{
  "dispatch_rule": {
    "trunk_ids": [
      "trunk-123"
    ],
    "inbound_numbers": [
      "+15550102"
    ],
    "rule": {
      "dispatchRuleDirect": {
        "roomName": "agent-b-room"
      }
    },
    "roomConfig": {
      "agents": [
        {
          "agentName": "Agent_B",
          "metadata": "Handles calls for +1-555-0102"
        }
      ]
    }
  }
}
Explanation: - Both rules use the same trunk (trunk-123). - Each rule has its own inbound_numbers to match the specific called number. - The roomConfig assigns the right agents to handle calls for each number. This way, calls coming into different numbers on the same trunk get routed to the right agents, making sure everything runs smoothly. Sources: SIP dispatch rule | LiveKit Docs | SIP inbound trunk | LiveKit Docs | sip/sip.go
s
Isn't inbound_numbers the call From which the numbers are coming? Not To?
t
The inbound_numbers field in LiveKit dispatch rules is all about the called number (To), not the caller's number (From). So, it helps you match the number that’s being dialed, making it easier to route calls based on where they’re headed. Hope that clears things up! Sources: livekit/livekit_sip.pb.go | livekit-protocol/src/livekit.rs
s
I checked and it is for the caller's number and not the called number
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!