Asterisk Call Forwarding

Let me give you an example of setting call forwarding in Asterisk.
For example, on Grandstream IP phones, you can enable redirection by the functions of the phone itself, but if the phone is far away and there is no way to do it on it, but it is possible to log in using the SIP number of this phone, then to activate call forwarding, you can make a voice menu when dialing a certain number, for example, *21 to activate call forwarding and enter the phone number to which calls will be forwarded, and *22 to cancel call forwarding.

In the /etc/asterisk/extensions.conf file, I first described these two short numbers for activating and canceling forwarding:

; Bezuslovnaya pereadresaciya
exten => *21,1,Playback(hello)
same => n,Playback(vm-enter-num-to-call)
same => n,Read(cfwd)
same => n,Playback(beep)
same => n,Set(DB(REDIRECT/${CALLERID(num)})=${cfwd})
same => n,Set(DB(REDIRTIMER/TIMER)=10)
same => n,Playback(you-entered)
same => n,SayDigits(${DB(REDIRECT/${CALLERID(num))}})
same => n,Playback(enabled)

; Otmena pereadresacii
exten => *22,1,Set(NOREDIRNUM=${DB_DELETE(REDIRECT/${CALLERID(num)})})
same => n,Playback(disabled)

If you call *21 and enter the number to which you want to redirect calls, then this number will simply be stored in the Asterisk database (AstDB), if you dial *22, this number will be deleted from the database.

In the same file, I had a dialplan for local number 207:

exten => 207,1,Dial(SIP/207,60)
exten => 207,n,Hangup()

Which I changed to the following view, namely, I added a check to see if there is a number in the database and if there is, then redirect to it, if there is no number, then call the local number as usual:

exten => 207,1,Set(REDIRECTNUM=${DB(REDIRECT/${EXTEN})})
exten => 207,n,GotoIf($[${ISNULL(${REDIRECTNUM})}]?internal:redirect)
exten => 207,n(internal),Dial(SIP/${EXTEN})
exten => 207,n(redirect),Dial(SIP/goip4/1${REDIRECTNUM})
exten => 207,n,Hangup()

Goip4/1 is my gateway with SIM cards, the number 1 means that you need to call through the first SIM card, if the first SIM card is already taken by another call, you can specify several gateways to get through accurately, for example:

exten => 207,1,Set(REDIRECTNUM=${DB(REDIRECT/${EXTEN})})
exten => 207,n,GotoIf($[${ISNULL(${REDIRECTNUM})}]?internal:redirect)
exten => 207,n(internal),Dial(SIP/${EXTEN})
exten => 207,n(redirect),Dial(SIP/goip4/1${REDIRECTNUM},60)
exten => 207,n(redirect),Dial(SIP/goip4/4${REDIRECTNUM},60)
exten => 207,n(redirect),Dial(SIP/goip4new/1${REDIRECTNUM},60)
exten => 207,n(redirect),Dial(SIP/goip4new/2${REDIRECTNUM},60)
exten => 207,n,Hangup()

This is just a simple example of call forwarding, which I quickly wrote when it was often necessary to turn on the call forwarding remotely, it is still desirable to record and indicate the correct sounds for numbers *21 and *22, and you can also make conditional call forwarding, for example, when the called number does not answer for 10 seconds, etc.

You can view the data in the AstDB database using the command:

asterisk -rvv
database show
exit

See also my other articles about Asterisk

Join the Conversation

1 Comment

Leave a Reply

  1. Thank you for your configuration. With it and with some modifications I got working call-transfer. The changes was made due to symbolic caller id in SIP^ not only ciphered. Here it is:
    sip.conf
    ——————————————————————————————
    [os]
    secret = 978
    context = LocalSets
    ——————————————————————————————

    extensions.conf
    ——————————————————————————————-
    ;macros
    OS=SIP/os
    ;this is global variable have the same name as the SIP-name of caller
    os=205

    [LocalSets]

    ; Bezuslovnaya pereadresaciya
    ;${CALLERID(num)} == os
    ;${${CALLERID(num)}} == 205
    exten => _*21.,1,Set(DIRNUM=${${CALLERID(num)}})
    same => n,Set(REDIRNUM=${EXTEN:3})
    same => n,Set(DB(REDIRECT/${DIRNUM})=${REDIRNUM})
    same => n,Set(DB(REDIRTIMER/TIMER)=10)
    same => n,Playback(you-entered)
    same => n,SayDigits(${DB(REDIRECT/${DIRNUM})})
    same => n,Playback(enabled)

    ; Otmena pereadresacii
    exten => *22,1,Set(DIRNUM=${${CALLERID(num)}})
    same => n,Set(NOREDIRNUM=${DB_DELETE(REDIRECT/${DIRNUM})})
    same => n,Playback(disabled)

    ;for each extension instead of: exten => 205,1,Dial(${OS},60,tTxXkK)
    exten => 205,1,Set(REDIRECTNUM=${DB(REDIRECT/${EXTEN})})
    exten => 205,n,GotoIf(${ISNULL(${REDIRECTNUM})}?internal:redirect)
    exten => 205,n(internal),Dial(${OS},60,tTxXkK)
    exten => 205,n(redirect),Dial(LOCAL/${REDIRECTNUM}@LocalSets)
    exten => 205,n,Hangup()