Asterisk extension, conditional execution? - asterisk

I have an this extension in file /etc/asterisk/extensions_custom.conf:
exten => _XXXX,1,NoOp("-- from internal custom --")
exten => _XXXX,n,Set(CURL_RESULT=${CURL(https://your.domain.com/sip_webhook?callid=${EXTEN}&sourceid=${CALLERID(num)})})
exten => _XXXX,n,Wait(3)
exten => _XXXX,n,Dial(PJSIP/${EXTEN},60)
exten => _XXXX,n,Hangup()
The second line sends a request to a webhook in my server and stores the response in CURL_RESULT
I want to execute Dial only if CURL_RESULT was successful is there any way to achieve conditional execution of an extension?
something like:
if(CURL_RESULT=="OK")
exten => _XXXX,n,Dial(PJSIP/${EXTEN},60)
else
exten => _XXXX,n,Hangup()

Yes, as arheops indicated, this is actually a common built-in concept in the dialplan: conditional executing and conditional branching.
There are a lot of ways you could this - to give you some ideas:
#1: Conditional execute if true
exten => _XXXX,1,ExecIf($["${CURL_RESULT}" = "OK" ]?Dial(PJSIP/${EXTEN},60))
same => n,Hangup()
#2: Conditional branch if false
exten => _XXXX,1,GotoIf($["${CURL_RESULT}" != "OK" ]?exit)
same => n,Dial(PJSIP/${EXTEN},60)
same => n(exit),Hangup()
#3: Conditional branch if true/false:
exten => _XXXX,1,GotoIf($["${CURL_RESULT}" = "OK" ]?dial:exit)
same => n(dial),Dial(PJSIP/${EXTEN},60)
same => n(exit),Hangup()
That last example doesn't add anything in this case, but providing a true and false label is often useful.
If you want a true If/Else application in Asterisk, that works similar to in conventional programming languages, you can use the If module: https://github.com/InterLinked1/phreakscript/blob/master/apps/app_if.c (this has since been merged into Asterisk, you don't need to patch separately).
It's not currently included in mainline Asterisk so you'd need to drop that into your source, compile, and reinstall.
With that, you could do something like:
same => n,If($["${CURL_RESULT}" = "OK" ])
same => n,Dial(PJSIP/${EXTEN},60)
same => n,NoOp(${DIALSTATUS})
same => n,Else()
same => n,Playback(goodbye) ; do something else
same => n,EndIf()
same => n,Hangup() ; something after both
Each type of conditional syntax tends to work slightly nicer than some of the others in particular circumstances.

Related

Asterisk, Goto+Hungup

There is an extension, for which I need a random sound file to be played, and once it's played the call should be hung up. I have the below dialplan which plays, for example, the 2nd playback and then 3rd one is played etc., but it should hangup after playing back one sound file
exten => 123.,7,Goto(${RAND(8,12)})
exten => 123.,8,Playback(/var/lib/asterisk/sounds/customer_Menu1)
exten => 123.,9,Playback(/var/lib/asterisk/sounds/customer_Menu2)
exten => 123.,10,Playback(/var/lib/asterisk/sounds/customer_Menu3); after playback it supposed rejected..
exten => 123.,11,Playback(/var/lib/asterisk/sounds/customer_Menu4)
exten => 123.,12,Playback(/var/lib/asterisk/sounds/customer_Menu5)
exten => 123.,13,Hangup()
exten => 123.,14,Hangup(34)
How can I make this work; calling, playing only one random menu and hanging up?
Just give the hangup command a priority label and add a Goto() pointing to it after playing each item.
Also:
if you want to use pattern matching for an extension (I assume that's why you have a dot after 123) you should prefix it with an underscore
same can be used in place of exten => ... for the extension when you're repeating the same thing
n can be used for the next priority so you don't have to worry about keeping all the numbers straight
Now if we get rid of the priority numbers, what do you use in place of Goto(${RAND(8,12)})? More labels:
[mycontext]
exten => _123.,1,NoOp("start of context")
; ...
same => n,Goto(menu${RAND(1,5)})
same => n(menu1),Playback(/var/lib/asterisk/sounds/customer_Menu1)
same => n,Goto(goodbye)
same => n(menu2),Playback(/var/lib/asterisk/sounds/customer_Menu2)
same => n,Goto(goodbye)
same => n(menu3),Playback(/var/lib/asterisk/sounds/customer_Menu3)
same => n,Goto(goodbye)
same => n(menu4),Playback(/var/lib/asterisk/sounds/customer_Menu4)
same => n,Goto(goodbye)
same => n(menu5),Playback(/var/lib/asterisk/sounds/customer_Menu5)
same => n(goodbye),Hangup(34)
Or, more simply, use the RAND() function to choose the name of the file to play:
[mycontext]
exten => _123.,1,NoOp("start of context")
; ...
same => n,Playback(/var/lib/asterisk/sounds/customer_Menu${RAND(1,5)})
same => n,Hangup(34)
Should be something like this:
exten => _123.,7,Set(FILES=customer_Menu1:customer_Menu2:customer_Menu3)
same => n,Playback(${CUT(FILES,:,${RANDOM(1,3)}); CUT will get nth argument delimited by :
same => n,Hangup(34)
Or like miken32 show you if your files are always same name.
Also nice idea is move configure variables like FILES in [general] section or special file included in code.
You DO NOT need /var/lib/asterisk/sounds/ before every file. Just check that in your /etc/asterisk/asterisk.conf sound path is /var/lib/asterisk/sounds(default).

How to allow a skip or goto at any time during a context in Asterisk

I am trying to make an enable alarm function in my pbx, so far I have this context going which works. I would like to allow a number or # being pressed at any time during the countdown (playtones) to skip to the Alarm is now armed moment (or maybe the other extension, either way works).
The issue I am facing is that Read or WaitExten only works for the defined time, but I would like to continue the dialplan and allow the button press at any time in the channel.
exten => s,1,NoOp(enable alarm)
same => n,Set(DB(alarm/status)=Enabled)
same => n,Wait(1)
same => n,Answer()
same => n,Playback(customsounds/arming-in-45-seconds)
same => n,Playtones(825/150,0/1550)
same => n,Wait(10)
same => n,Playtones(825/150,0/1000)
same => n,Wait(10)
same => n,Playtones(825/150,0/510)
same => n,Wait(10)
same => n,Playtones(825/150,0/100)
same => n,Wait(5)
same => n,Playtones(825/1500)
same => n,Wait(3)
same => n,StopPlaytones()
same => n,Playback(customsounds/the-alarm-is-now-armed)
same => n,NoOp(do shell exec here)
same => n,HangUp()
exten => 1,1,NoOp(skipped countdown)
same => n,Playback(customsounds/the-alarm-is-now-armed)
same => n,NoOp(do shell exec here)
same => n,HangUp()
You may build up your context using Asterisks Read() dialplan application instead of Wait(). The Read() application does provide a timeout parameter which you can set to your specific value. If a user presses # during the Read(), the reading will be interrupted and you can continue. Otherwise the reading will be aborted after the passed timeout. To play your tones in background you can use the filename parameter of the Read() application which can be used to play some audiofiles in the background while waiting for an DTMF input.
In this case a solution may look like this:
exten => s,1,NoOp(enable alarm)
same => n,Set(DB(alarm/status)=Enabled)
same => n,Wait(1)
same => n,Answer()
same => n,Playback(customsounds/arming-in-45-seconds)
same => n,Read(get,customsounds/waitingsound,,,,45)
same => n,Playback(customsounds/the-alarm-is-now-armed)
same => n,NoOp(do shell exec here)
same => n,HangUp()
Maybe the syntax is not 100% correct but it should work like that in your case. You can read more about the Read() application here: http://the-asterisk-book.com/1.6/applikationen-read.html
Or here: https://www.voip-info.org/asterisk-cmd-read/

Asterisk block incoming call from specific country only

I am trying to make a dial plan to block incoming call coming from let say bangladesh where country code is 88
Here is my dial plan
exten => _X.,1,NoOp(${CALLERID(num)})
same => n,Set(regx=^(88)[0-9]$)
same => n,GotoIf($[${REGEX("${regx}" ${CALLERID(num)})} = 1]?blacklisted,s,1)
same => n,Dial(SIP/8.8.8.8/${EXTEN}
[blacklisted]
exten => s,1,Wait(9)
what i want to do is anything that comes from 88 should be sent to blacklist. At the moment if I test call with Caller ID 88 it works but if the call comes from 88XXXXXXX this does not work what can I do to make my dial plan block anything coming from 88XXXXXXXX to goto black list
Why do you use the complex REGEX function? Just do something like that:
exten => _X.,1,NoOp(CallerID is: ${CALLERID(num)})
exten => _X.,n,Set(number=${CALLERID(num)})
exten => _X.,n,ExecIf($[${number:2} = 88],Hangup())
exten => _X.,n,NoOp(Call is being continued)
This will save the callerid num into the "number" variable. Then it will check if the first 2 characters of the variable are equal to 88. If so, the call will be hung up. You can also use GoToIf instead of ExecIf if you want to send the blacklisted call to a specific context where you want to do something else with it.
Asterisk dialplan is regexp itself. Why you use other regexp?
exten => _X.,1,NoOp(${CALLERID(num)})
same => n,Gosub(cid-blacklist,${CALLERID(num)},1)
same => n,Dial(SIP/8.8.8.8/${EXTEN}
[cid_blacklist]
exten => _88.,1,Noop(bangladesh)
same => n,Wait(100)
; this works only when not found match in context.
include=> cid_blacklist_not_found
[cid_blacklist_not_found]
exten => _X.,1,Return;not found in cid blacklist

Asterisk & Freepbx : Multiple Day Night Toggles?

I am in a situation where I often need to change the active call flow control (which can only be one..right?).
I find that netsting call flow controls is very unhandy if you have 4 different scenarios.
So what I would like to do is to create an 4 different extensions that set the appropriate call flow as the inbound route destination.
To make an example:
I have
CALL FLOW NR 1
CALL FLOW NR 2
CALL FLOW NR 3
CALL FLOW NR 4
I would like to create:
extension *881 -> Toggles Call Flow nr 1 and sets it as destination
of the inbound route
extension *882 -> Toggles Call Flow nr 2 and sets it as destination
of the inbound route
extension *883 -> Toggles Call Flow nr 3 and sets it as destination
of the inbound route
extension *884 -> Toggles Call Flow nr 4 and sets it as destination
of the inbound route
Is something like that possible?
If so...how would you achieve it?
My system is based asterisk 11 and freepbx 2.11.
Thank you in advance for your time and effort.
You can put as many time conditions as needed.
You can use special code to turn on/off time condition(used for holidays). You can check exact code on timecondition page.
You also can create custom module which will change flow, more info see on dev.freepbx.org. As option you can hire someone do your condition module.
I figured it out
edit /etc/asterisk/extensions_custom.com and paste the following
[from-internal-custom]
include => enable-switch
[check-active-switch]
exten => s,1,NoOp("This context loops through all the switches and checks which one is ON. Than it follows its destination")
exten => s,n,GoSub(sub-get-nr-of-switches,s,1)
exten => s,n,Set(nrofs=${GOSUB_RETVAL})
exten => s,n,Set(i=0)
exten => s,n,Set(default-dest=app-daynight,${switchnr},1)
exten => s,n,Set(switchnr="NULL")
exten => s,n,While($[$[${i} < ${nrofs}] & $[${switchnr} = "NULL"]])
exten => s,n,Set(switchnr=${IF($["${DB(DAYNIGHT/C${i})}" = "NIGHT"]?${i}:"NULL")})
exten => s,n,Set(i=$[${i} + 1])
exten => s,n,EndWhile
exten => s,n,GotoIf($[${switchnr}!="NULL"]?app-daynight,${switchnr},1:${default-dest})
exten => s,n,Hangup()
[sub-get-nr-of-switches]
exten => s,1,Set(nr_of_s=0)
exten => s,n,Set(exten_state="NOT_ACQUIRED")
exten => s,n,While($[${exten_state}!=0])
exten => s,n,Set(exten_state=$[${VALID_EXTEN(app-daynight,${nr_of_s},1)}])
exten => s,n,NoOp(Exten nr ${nr_of_s} of app-daynight is ${exten_state})
exten => s,n,Set(nr_of_s=${IF($[${exten_state}=1]?$[${nr_of_s}+1]:${nr_of_s})})
exten => s,n,EndWhile
exten => s,n,NoOp(${nr_of_s} switches found)
exten => s,n,Return(${nr_of_s})
[enable-switch]
exten =>_*20X,1,NoOp("Abilita Switch selezionato e disabilita altre")
same => n,GoSub(sub-get-nr-of-switches,s,1)
same => n,Set(nrofs=${GOSUB_RETVAL})
same => n,Set(switchtoactivate=${EXTEN:3})
same => n,NoOp(Activated Switch nr ${switchtoactivate})
same => n,Set(i=0)
same => n,While($[${i}<${nrofs}])
same => n,Set(DB(DAYNIGHT/C${i})=DAY)
same => n,Set(i=$[${i}+1])
same => n,EndWhile
same => n,Set(freepbx_toggle_dest=*28${switchtoactivate})
same => n,Goto(app-daynight-toggle,${freepbx_toggle_dest},1)
same => n,Hangup
Next go to freepbx and create a custom destination -> check-active-switch,s,1 and set it as destination of the inbound route

Can't combine DongleStatus with different dial plans

We have 2 two major mobile carriers in our country - (1) beginning with 818927, 818937, 818929 and (2) beginning with 818917, 818919, 818987, 818989.
I have 4 usb modems (huawei) and I want 2 different lines to be used for each of those mobile carriers.
I am using DongleStatus because 2 simultaneous calls must be allowed on the same prefix (e.g. when there's two the same SIP agents are calling the same direction e.g. both of them trying to call two different numbers from 818927* range simultaneously)
Here I mention the extensions configuration which doesn't works!
But if you will remove all exten lines and only 1 of them will be remaining, then the whole DongleStatus script mentioned bellow works perfectly (it allows to use 4 lines subsequently by 4 SIP agents simultaneously)
I need your help to find a mistake in mentioned bellow configuration or your suggestion on alternate ways to achieve the same goal. I'm relatively new to asterisk, and I would appreciate not over-complicated answers.
; buklau
exten => _818927XXXXXXX,1,DongleStatus(GSM-001,Dongle0_Status)
exten => _818937XXXXXXX,1,DongleStatus(GSM-001,Dongle0_Status)
exten => _818929XXXXXXX,1,DongleStatus(GSM-001,Dongle0_Status)
exten => _818917XXXXXXX,1,DongleStatus(GSM-003,Dongle2_Status)
exten => _818919XXXXXXX,1,DongleStatus(GSM-003,Dongle2_Status)
exten => _818987XXXXXXX,1,DongleStatus(GSM-003,Dongle2_Status)
exten => _818989XXXXXXX,1,DongleStatus(GSM-003,Dongle2_Status)
same => n,DongleStatus(GSM-002,Dongle1_Status)
same => n,DongleStatus(GSM-003,Dongle2_Status)
same => n,DongleStatus(GSM-004,Dongle3_Status)
same => n,GotoIf($[${Dongle0_Status} = 2]?dongle0dial:dongle1check)
same => n(dongle0dial),Dial(Dongle/GSM-001/${EXTEN:2},60,tT)
same => n,Hangup
same => n(dongle1check),GotoIf($[${Dongle1_Status} = 2]?dongle1dial:dongle2check)
same => n(dongle1dial),Dial(Dongle/GSM-002/${EXTEN:2},60,tT)
same => n,Hangup
same => n(dongle2check),GotoIf($[${Dongle2_Status} = 2]?dongle2dial:dongle3check)
same => n(dongle2dial),Dial(Dongle/GSM-003/${EXTEN:2},60,tT)
same => n,Hangup
same => n(dongle3check),GotoIf($[${Dongle3_Status} = 2]?dongle3dial:utel)
same => n(dongle3dial),Dial(Dongle/GSM-004/${EXTEN:2},60,tT)
same => n,Hangup
This part:
exten => _818989XXXXXXX,1,DongleStatus(GSM-003,Dongle2_Status)
same => n,DongleStatus(GSM-002,Dongle1_Status)
is equal to
exten => _818989XXXXXXX,1,DongleStatus(GSM-003,Dongle2_Status)
exten => _818989XXXXXXX,2,DongleStatus(GSM-002,Dongle1_Status)
So it will not work with any other patern used above. If you need part be for all above, use something like
exten => _8189XXXXXXXXX,2,DongleStatus(GSM-002,Dongle1_Status)
Hint: You always can ask asterisk show how it understand dialplan, for that do
asterisk -rx "dialplan show number#context"

Resources