📜 ⬆️ ⬇️

Redirection for Asterisk

Call forwarding is an interesting thing and there are a lot of call forwarding scenarios.
In the case described by me, the redirection is performed unconditionally and is activated at the request of the subscriber (in general, this example is very easy to convert to conditional redirection and if I get my hands on it I will try to solve this topic), but everything is complicated by the fact that the server that Asterisk registers with access to the city does not know process SIP message 302-Moved Temporarily (SIP channel is used, but dialplan can be applied to other technologies). The disadvantage of the method that I will describe here is that it takes 2 Trunk'a or 1, but multichannel (<2 channels). The method uses Asterisk astDB's own database, so I advise you to read about it, since I will not dwell on this point in detail.

And so proceed: here is the whole piece of dialplan, which is responsible for call forwarding:

exten => #21,1,Set(DB(REDIRECT/${CALLERID(num)})=${EXTEN})

exten => #21#,1,Set(NOREDIRNUM=${DB_DELETE(REDIRECT/${CALLERID(num)})})

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


Now let's analyze in parts what is what and what is responsible for.
')
As I said, redirection is performed at the request of the client. That is, in order to use redirection you need to enable it:

exten => #21,1,Set(DB(REDIRECT/${CALLERID(num)})=${EXTEN})

This command remembers the dialed number and the number from which it was dialed, and saves it in the Asterisk database.

Here, after dialing the combination # 21, you need to dial the number for forwarding. I put in the REDIRECT family the dialed number $ {EXTEN}, which will be available by key $ {CALLERID (num)}. That is, in simple terms, the entered number will be stored in the REDIRECT table in the $ {CALLERID (num)} row or available at REDIRECT / $ {CALLERID (num)} in the Asterisk database (DB command). The variable $ {CALLERID (num)} stores the number from which the call originates.

Disable redirection occurs in a similar way:

exten => #21#,1,Set(NOREDIRNUM=${DB_DELETE(REDIRECT/${CALLERID(num)})})

The subscriber, dialing the combination # 21 # using the DB_DELETE utility, clears the cell REDIRECT / $ {CALLERID (num)}

Here I used the NOREDIRNUM variable to simply perform any actions with via Set () because it cursed the lack of the "=" sign. But in general, you can try this:

exten => #21#,1,Set(DB_DELETE(REDIRECT/${CALLERID(num)}))

The next few lines are engaged in just the most basic - the implementation of the call. Let's look at them in more detail:

exten => s,1,Set(REDIRECTNUM=${DB(REDIRECT/${EXTEN})})

This line extracts the contents of the cell REDIRECT / $ {EXTEN} (redirect number) and assigns its value to the variable REDIRECTNUM, in order to check whether the redirection number is specified for this subscriber. That is, if, say, we call the subscriber at number 100, then this command assigns the value of the REDIRECT / 100 cell to the variable REDIRECTNUM.

exten => s,n,GotoIf($[${ISNULL(${REDIRECTNUM})}]?internal:redirect)

Next, we check the existence of the redirect number: If the REDIRECTNUM variable is ISNULL (empty / undefined), then we go to the line labeled internal and call the subscriber by its main number:

exten => s,n(internal),Dial(SIP/${EXTEN})

If the variable REDIRECTNUM is not empty, then call the number specified in it.

exten => s,n(redirect),Dial(SIP/TRUNK/${REDIRECTNUM})

I hope someone will help in the exploration of Asterisk.

Source: https://habr.com/ru/post/139178/


All Articles