Need to make AGI script run only 10 times, not for ever)

Hi Guys. in present dialplan you can see that AGI script will run for ever untill status of PJSIP endpoint becomes available. the question is, how to make AGI script run only 10 times?

Thank you very much, BR

exten => _0XX,n,GotoIf($[${DEVICE_STATE(pjsip/${EXTEN})}=UNAVAILABLE]?absent:makecall)

;absent
exten => _0XX,n(absent),Verbose(2, The agent is not registred)
exten => _0XX,n,Verbose(2, ${EXTEN})
exten => _0XX,n,AGI(run.sh,${EXTEN},${CALLERID(num)})
exten => _0XX,n,GotoIf($[${DEVICE_STATE(pjsip/${EXTEN})}=UNAVAILABLE]?absent:makecall)
exten => _0XX,n,Dial(PJSIP/${EXTEN},120,TtkKhH)
exten => _0XX,n,Hangup()

;make call
exten => _0XX,n(makecall),Verbose(2, Call to extension)
exten => _0XX,n,Answer()
exten => _0XX,n,Dial(PJSIP/${EXTEN},120,TtkKhH)
exten => _0XX,n,Hangup()

You can do some arithmetic with Asterisk channel variables. A simple loop would solve your problem. You’d basically branch on the result of two conditions. This is the basic idea: Conditional Applications - Asterisk Project - Asterisk Project Wiki

The problem is that i dont know how to put AGI call count into expression…

added a while loop 10
and a wait as it will just spam you AGI
also you should change to use “same”
replace all “exten => _0XX,n,” with “same => n,”

It is uncommon to write an AGI in shell. Does your script actually interact with Asterisk (‘get variable,’, ‘set variable,’ etc) or would ‘system()’ be more appropriate.

Thanks Mark. I will try it, and reply. Does first line need to be same => or exten => _0XX, because extension starts with 0, so how system will understand that it must run that dialplan?

shell script is written to curl a website, that is on another server that wake up a softphone if it is in sleep mode. a bit complicated but it works)

like this

exten => _0XX,1,NoOP(FIRST LINE)
same => n,NoOP(ALL FOLLOWING LINES)

still sending 10 request within a frew ms is probably overkill
so a small wait is probably still nice, give the softphone a chance to wake up

Then ‘system()’ is more appropriate than ‘agi().’

How about:

same => n,Wait(0.1)

Or even better, why not loop in your shell script?

How about:

verbose(FIRST LINE)

It’s more featureful and obvious.

the point of the NoOP was just to be a placeholder for whatever coded he needed
not to do anything, as I was just using that to show how to use “same” after “exten”
with regards to how long to wait, well that require some testing
how long do the softphone take to registre after receiving the poke, where it be 0.1s or 5s
if you loop in the shell script you may be poking it to many time, and you still want the dialplan try to see if it has come online so you do not gain anything by looping in the script as the dialplan still have to wait and check where it is registered

but yes he should use System() and Not Agi()

It loops script 10 times and then hangup, it works like a charm! but, even when i answer a softphone it still continues to ring)))

I don’t think you meant that; I think you meant that you continue to hear ring back tone on the caller. My initial assumption would be that you have a NAT misconfiguration and the wrong addresses are being sent in the INVITE, but you would need to provide “pjsip set logger on” type output to be able to check that.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.