Search code examples
communicationsipvoipasterisktelephony

How to route an incoming call when I have multiple phone numbers with the same SIP provider?


I have a main phone number 0120120020 given by my SIP provider for my office. I've recently asked my SIP provider for a second phone number 0230230030 on the same SIP account.

I want Asterisk to ring every phones belonging to Group A when someone calls the office by dialing the main phone number, and I want it to ring every phones belonging to Group B if the second phone number is dialed instead.

My extensions.conf contains the following lines :

[sip-incoming-calls]
exten => s,1,Dial(SIP/10&SIP/11&SIP/12&SIP/20&SIP/21,20,tr)

Extensions 10, 11 and 12 belongs to the Group A, extensions 20 and 21 belongs to the Group B. Every phones are ringing for each incoming calls coming from the main and the second phone number.

How can I do this with Asterisk 1.8 under CentOS 6.3 ?


Solution

  • For each call, Asterisk sets some variables containing informations about the current incoming call. These informations are useful to act differently if the call is coming from specific country, you can also block bad callers from their phone numbers,...

    We are looking for a way to retrieve the DID, which actually means Direct Inward Dialing Number, that's the number dialed by the caller to call your office.

    There are two ways to retrieve it :

    • By using the CALLERID(dnid) variable directly, it's value will be set to 0120120020 or 0230230030

      [sip-incoming-calls]
      exten => s,1,Set(thedid=${CALLERID(dnid)})
      
    • If the above variable is empty, then your SIP provider doesn't send any informations as needed to make Asterisk filling it correctly.

      You can however retrieve the phone number directly from the To field inside the SIP header with SIP_HEADER(To), this variable will contain <sip:[email protected]> when someone calls your office from your second phone number.

      [sip-incoming-calls]
      exten => s,1,Set(thedid=${SIP_HEADER(To)})
      exten => s,2,Set(thedid=${CUT(thedid,@,1)})
      exten => s,3,Set(thedid=${CUT(thedid,:,2)})
      

    Once you have retrieved the DID inside the variable, you have to set a condition, let's use GotoIf. It will result like this in your case :

    exten => s,4,GotoIf($["${thedid}" = "0120120020"]?6:5)
    exten => s,5,GotoIf($["${thedid}" = "0230230030"]?7)
    exten => s,6,Dial(SIP/10&SIP/11&SIP/12,20,tr)
    exten => s,7,Dial(SIP/20&SIP/21,20,tr)