[HELP] Evaluate and react to return code

I was looking at the examples, googling but to no avail even though my issue is very trivial.

Virtually every single command returns a return code. I want to use that return code to react to certain (expected) conditions. For some commands the +101 priority works, for others it doesn’t. I guess that once I know how to retrieve the return code I can do an GOTOIF(…).

One example: I read the extension that the caller dialed last time and suggest this extension on the next connect. IF the extension owner stored their names they will be in the voicemail directory and I play it. If they HAD NOT recorded their name I want to plainly SAYNUMBER() the extension. So what I needs is to find out if the soundfile exists. Since I don’t know how to use EXISTS() (sparse docs) nor can evaluate the return code I thought I work with the +101 behavior. That did not work.

So concluding: Each command returns 0,1, etc as return codes signaling success, failure or other conditions. How can I retrieve this very return code. (I told you its a trivial question, but without an answer I am very limited in my flexibility)

exten => 1,n,Background(the-num-i-have-is)
exten => 1,n(playname),Background(/var/spool/asterisk/voicemail/default/${toDial}/greet)
;exten => 1,n,SayNumber(${toDial})
exten => 1,n(aftername),Background(to-call-num-press)
exten => 1,n,Background(pound)
exten => 1,n,Wait(1)
exten => 1,n,Goto(repeat)

exten => 1,playname+101,SayNumber(${toDial})
exten => 1,n,Goto(aftername)

I can’t imaghine that nobody ever ran into issues in which one wants to react to return codes?

I have read through both books, wiwki’d, googled, even dogpiled to no avail.

This whole -otherwise excellent- applications system were useless if those rc’s can’t be evaluated. It were also ridiculously silly to set return codes unless they can be retrieved and used.

If the answer to my question is so totally obvious then be so kind and answer the ‘stupid’ question anyway since I really need an answer and I generally don’t go out and ask unless I have used up all other ways to find an answer. And, besides, I thought that that’s what the forum is for???!!!

So please be so kind and answer this question. Since I am not a total dummy I guarantee you that others will run against the same wall in their programming efforts. You will not only help me but other peers!!!


Would it be possible for you to do your logic within AGI? Then you could check for the existance of the file via perl, php, or whatever and handle the appropriate response that way.


you must use gotoif
this extension goto if 1 and proceed when 0
go voip-info.org/wiki-Asterisk+cmd+GotoIf

Yes, I could use AGI.
Or a shell script ( voip-info.org/wiki/view/Aste … eexistance )

There is also a function STAT() ( voip-info.org/wiki/view/Asterisk+functions ).

But it seems to me to be an overkill to use agi just to test if a soundfile exist, or for any other functions return code. Is there a system variable like RETCODE or similar that contains the current return code? ALL cmds and functions return an RC and I thought:

“If they provide it, there will be a way to use it via dialplans”

I can use the result of a function for a gotoif but NOT the result of a command like
------------------------ DOES NOT WORK ----------------------------

[color=red]exten => 1,n(playname),GotoIf($[Background(/var/spool/asterisk/voicemail/default/${toDial}/greet)]?notfd)[/color]
------------------------ DOES NOT WORK ----------------------------

Some commands allow for the n+101 logic while others don’t but all return a success code!

[quote=“meral82”]you must use gotoif
this extension goto if 1 and proceed when 0
go voip-info.org/wiki-Asterisk+cmd+GotoIf[/quote]

Okay, that would be great, what shall I use as predicate for the gotoIf?

See example above top. I try a background and then I gotoif($[???]?soundfound:soundnotfound) ?

According to the docs, the Background command always returns 0.

Hmmm, I read that background returns a 0 or a -1 if the other side hung up or the filenamne does not exist. ( O’Reily: Asterisk,The Futute of Telephony, p 234 )

I tried both exec and eval and none of them work either to receive a return code.

Heck, it just does not make sense that the success of a command can not be assessed!! This is a well thought out and sophisticated system and ther MUST be a way without AGI. Alone the gotoIf and the rc’s which each command return make me certain there is a way! I am just to ignorant to find out!

Sadly I don’t have the time to dig into the source code.

I don’t believe that I am the only one who has this problem. And I am sure that someone found a solution if there is one.

Those who did: Please share your solutions with the rest of the group. Not only I but others as well will be very thankful since the functionality of Asterisk’s dialplan will expand exponentially!