Hang up the call after x seconds after the called party has answered the call using ARI in Asterisk - 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)

Related

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.

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 - Macro not available

I need some help here.
I try to configure dolibarr's plugin "Calling" on my asterisk.
I need to configure my extensions.conf so that all incoming calls on my asterisk return information to my dolibarr in order to have call notifications...
When I call from outside my softphone rings correctly, but as soon as I get off I get the following error message:
WARNING[3314][C-0000000d]: app.c:326 ast_app_exec_macro: Cannot run 'Macro(dolinotif)'. The application is not available.
Here my Extensions.conf :
[Entrant] exten => s,1,Goto(dolibarr_notification,s,1) ;Redirection
exten => _X.,1,NoOp()
exten => _X.,n,Set(__DOLI_CALLER=${CUT(CUT(SIP_HEADER(From),#,1),:, 2)})
exten => _X.,n,Set(__DOLI_CALLE=${CUT(CUT(SIP_HEADER(To),#,1),:, 2)})
[macro-dolinotif]
exten => s,1,SET(${CURL(https://url.com/htdocs/custom/calling/calling.php?account=${EXTEN}&caller=${DOLI_CALLER}&callee=${DOLI_CALLE}&type=CONNECT&callref=${DOLI_CALLER}&version=1)})
[dolibarr_notification]
exten => s,1,SET(CALLERID(name)=${CURL(https://url.com/htdocs/custom/calling/calling.php?account=${USERNAME}&caller=${DOLI_CALLER}&callee=${DOLI_CALLE}&type=SETUP&callref=${DOLI_CALLER}&version=1)})
exten => s,n,Answer()
exten => s,n,Dial(SIP/MY_NUMBER,60,etTrM(dolinotif))
exten => h,1,SET(CALLERID(name)=${CURL(https://url.com/htdocs/custom/calling/calling.php?account=${USERNAME}&caller=${DOLI_CALLER}&callee=${DOLI_CALLE}&type=RELEASE&callref=${DOLI_CALLER}&version=1)})
exten => h,2,Hangup(16)
I'm a begineer on Asterisk, maybe it's just a dumb problem... If you need more informations ask me.
Thank a lot.
Use older asterisk version or rewrite your dialplan to use Gosub instead of macros.

How to execute an AGI script during a call by pressing a key

I want to trigger an AGI script (to activate a door opener) while calling. Preferably the script executes on pressing the #-key.
How to embed this behavior in a dialplan? All examples I have found are not dependent on a key press.
I am using FreePBX 2.8.1.4. This is what I have tried:
exten => s,1,Wait(1)
exten => s,n,AGI(test.py)
exten => s,n,Dial(SIP/mk55/203,20,tr)
exten => #,n,AGI(/home/pi/.scripts/dooropen.py)
exten => s,n,Hangup()
and this:
exten => s,n,Read(inPut,,1)
exten => s,n,Dial(console/dsp)
exten => s,n,GotoIf($["${inPut}" = "#"]?keypressed,1)
exten => keypressed,1,AGI(/home/pi/.scripts/dooropen.py)
exten => s,4,Hangup
In deed AGI(script_name, args...) is the right application to use https://wiki.asterisk.org/wiki/display/AST/Application_AGI
To retrieve a DTMF press you can use WaitExten https://wiki.asterisk.org/wiki/display/AST/Application_WaitExten
You can find here a sample of WaitExten usage
Seek Help concerning IVR Menu in Asterisk
Regards

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