Search code examples
twiliosipwebhooksgsmtwilio-api

How to connect one Twilio application instance to another via SIP?


I have an application that uses Twilio Programmable Voice.

The app is basically a call center. There are several instances deployed, each one has its own Twilio phone number and a pool of available human operators who can answer incoming calls. When GSM call is received from the customer to instance's Twilio phone number, Twilio invokes a WebHook on our side, and then we simply find an available person from the pool and return a TwiML like this:

<?xml version="1.0" encoding="utf-8"?>
<Response>
    <Say>Hello. Connecting you to the available operator.</Say>
    <Dial callerId="{callCenterNumber}">{operatorNumber}</Dial>
</Response>

Now the customer is connected to available person from the application instance's pool. So far, so good.

Now I want to handle the case when no available operators were found in the pool of the application instance that was originally called. In that case, I want the original app instance to connect to another instance's Twilio phone number and to be treated as a customer of that instance, so that the original human customer will be connected to an operator from another instance's pool. To achieve this, instance A simply returns <Dial callerId="{callCenterNumber}">{anotherCallCenterNumber}</Dial> (dialing anotherCallCenterNumber instead of operatorNumber in the example above).

So, instead of this connection: Customer => Call Center A => Operator from A's pool, I have Customer => Call Center A => Call Center B => Operator from B's pool connection.

It also works fine and I can chain even more call center instances until available operator is found somewhere.

The problem is, I want to use SIP protocol for instance-to-instance communication instead of GSM phone numbers, because I need to pass some extra data (language, etc) along the way, so that the next instance knows how to handle the call and find the right operator to answer it. SIP headers looks like a suitable solution for passing this data. Everything else will stay the same.

So, I configured a SIP endpoint for each application instance and set up Request WebHook URLs in a similar manner to regular Phone Number WebHook URLs. Now, to dial another call center instance via SIP, I return the following TwiML:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Say>Hello. No available operators were found. Connecting you to Call Center B.</Say>
    <Dial>
        <Sip>sip:[email protected]?x-customHeader=customData</Sip>
    </Dial>
</Response>

At this point, I expect that Twilio will invoke instanceB SIP Domain's WebHook, so I can handle the call and route it the same way as I used in regular Phone Number WebHooks.

Instead of that, the call is dropped immediately, and in the Twilio Debugger I see following warning:

32009 - The user you tried to dial is not registered with the corresponding SIP Domain

After some research, I realized that callcenter endpoint must be registered in instanceB.sip.us1.twilio.com SIP domain. But, it seems like the only way to do that is using a softphone client app like X-Lite or Jitsi. I installed X-Lite locally and added a callcenter account. Now when Call Center A receives a call and instructs Twilio to dial Call Center B (sip:[email protected]), my local softphone starts ringing, and if I answer it, I will be connected to a customer calling Call Center A. This is not what I want at all!

Also, I found that if I call any address from the softphone, even unregistered one like sip:[email protected], then strangely enough Twilio will invoke instanceB SIP Domain's WebHook without any complains. But that's also not what I need. I need Twilio => Twilio connection over SIP in the same way as it worked when using Phone Numbers.

So, to sum up:

  1. Is the described scenario with Twilio-to-Twilio over SIP communication possible at all, same way as Twilio-to-Twilio over Phone Numbers?
  2. Why Twilio returns an error The user you tried to dial is not registered with the corresponding SIP Domain when it receives a TwiML instructing it to dial unregistered SIP endpoint, but at the same time it doesn't return that error when I'm dialing the same unregistered endpoint from the softphone client?

Solution

  • Unfortunately you cannot do this via SIP. Twilio doesn't host SIP endpoints for you.