How to allow a skip or goto at any time during a context in Asterisk - 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/

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).

Asterisk extension, conditional execution?

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.

Hang up the call after x seconds after the called party has answered the call using ARI in Asterisk

We've been using Asterisk's Dial plans for quite some time and now we've decided to switch to ARI. I'm wondering is there a way to hang up the call after x seconds after the called party has answered the call using originate or any other method from /channels resource in ARI.
Note that I can do this using dial plan with L(x:y:z) or S(duration) functions but unfortunately can not find a way to use this feature in Stasis mode. here is a part of my dial plan in extensions.conf
exten => 8952XXXX,1,NoOp(${CALLERID(num)})
same => n,Set(ENDPOINT=${SIP_HEADER(X-Endpoint)})
same => n,Stasis(myapp,incoming,${EXTEN},unknown,unknown,none)
same => n,Hangup()
[context1]
exten => 8952XXXX,1,NoOp(${CALLERID(num)})
same => n,Set(__ENDPOINT=${SIP_HEADER(X-Endpoint)})
same => n,Dial(Local/${EXTEN}#context2/n,,L(x:y:z))
same => n,Hangup()
[context2]
exten => 8952XXXX,1,NoOp(${CALLERID(num)})
same => n,Stasis(myapp,incoming,${EXTEN},unknown,unknown,none)
same => n,Hangup()
Other option is use TIMEOUT(absolute)

Asterisk increase timeout between dtmf tones

I'm working on a dial plan where a user is prompted for a 4 digit number the below dialplan works fine for what I need under normal conditions. The problem occurs when a user takes longer than 5 seconds to hit the next dtmf tone.
Example user presses 111(waits > 5 seconds) I am prompted to the invalid audio track and repeat the process. Is there a way to increase the timeout to say 10 seconds?
[Example_IVR_Start]
exten => s,1,Verbose(1, Starting the IVR example)
same => n,Set(CORRECTNUM=1111)
same => n(menu),Background(example_start)
same => n,WaitExten(5)
same => n,Background(long_silence)
same => n,Goto(Timeout_hangup,s,1)
exten => _XXXX,1,GotoIf($[${EXTEN}=${CORRECTNUM}]?Example_IVR_TWO,s,1:i,invalid)
exten => *,1,Goto(s,menu)
exten => i,1(invalid),Playback(invalid)
exten => i,n,Goto(s,menu)
exten => t,1,Goto(Timeout_hangup,s,1)
Yes, you have function timeout
TIMEOUT(digit) - set timeout between keypress
TIMEOUT(absolute) - set overal timeout before hangup(not forget reset after input done).
https://wiki.asterisk.org/wiki/display/AST/Function_TIMEOUT
Fixed, calling TIMEOUT(digit) before Background fixes allows you to adjust the TIMEOUT period.
[Example_IVR_Start]
exten => s,1,Verbose(1, Starting the IVR example)
same => n,Set(CORRECTNUM=1111)
same => n,Set(TIMEOUT(digit)=10) ;needs to come before Background
same => n(menu),Background(example_start)
same => n,WaitExten(5)
same => n,Background(long_silence)
same => n,Goto(Timeout_hangup,s,1)
exten => _XXXX,1,GotoIf($[${EXTEN}=${CORRECTNUM}]?Example_IVR_TWO,s,1:i,invalid)
exten => *,1,Goto(s,menu)
exten => i,1(invalid),Playback(invalid)
exten => i,n,Goto(s,menu)
exten => t,1,Goto(Timeout_hangup,s,1)

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