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.