简体   繁体   中英

Asterisk 11 GotoIf Comparison of Integers

I can not get GotoIf() to properly compare two integers.

 exten => _X.,n,Set(junky=${RAND(0,1000)})
 exten => _X.,n,GotoIf(["${junky}"<"100"]?congest)
  ...
 exten => _X.,n(congest),Log(VERBOSE,"congested " ${EXTEN})
 exten => _X.,n,Congestion()

I have tried innumerable variations on the theme such as:

 exten => _X.,n,GotoIf([${junky}<100]?congest)
 exten => _X.,n,GotoIf(${junky}<100?congest)
 exten => _X.,n,GotoIf($["${junky}"<"100"]?congest)

...and nothing seems to work. Each variation either goes to "congest" or not but it does it regardless of the value of junky.

Here we have an example of what we see in CLI:

-- Executing [26343434@ts-in:3] Set("SIP/xxx.xxx.xxx.xxx-00000431", "junky=150") in new stack
-- Executing [26343434@ts-in:4] GotoIf("SIP/xxx.xxx.xxx.xxx-00000431", "0?congest") in new stack

What is the right syntax for doing this in Asterisk 11?

The example you are providing is missing a dollar sign ($) before the opening bracket. It is also a good practice to add spaces between the operands (older versions of Asterisk may have problems parsing if not separated), it should look like this:

exten => _X.,n,GotoIf($[ "${junky}" < "100" ]?congest)

In the verbose you are providing, the evaluation returns false so the call will continue to the next priority (you can tell because there is a zero before the question mark in the GotoIf line):

-- Executing [26343434@ts-in:3] Set("SIP/xxx.xxx.xxx.xxx-00000431", "junky=150") in new stack
-- Executing [26343434@ts-in:4] GotoIf("SIP/xxx.xxx.xxx.xxx-00000431", "0?congest") in new stack

If the following priority fails (eg. a Dial) the call will continue to the next one and so on, so you will hit the congestion eventually.

I hope this helps you.

Edit:

I made an example in my server, it is running Asterisk 11.22, I removed the double quotes because we are working with integers:

exten => 998,1,Set(junky=${RAND(0,1000)})
 same => n,GotoIf($[ ${junky} < 100 ]?congest)
 same => n,Playback(demo-congrats)
 same => n(congest),Hangup()

Here is an example where GotoIf returns false:

-- Executing [998@from-internal:1] Set("SIP/1001-00009821", "junky=999") in new stack
-- Executing [998@from-internal:2] GotoIf("SIP/1001-00009821", "0?congest") in new stack
-- Executing [998@from-internal:3] Playback("SIP/1001-00009821", "demo-congrats") in new stack
-- <SIP/1001-00009821> Playing 'demo-congrats.ulaw' (language 'es')
-- Executing [998@from-internal:4] Hangup("SIP/1001-00009821", "") in new stack

Since it returns false, the dialplan will continue, it will play the file demo-congrats and then hangup

Here is an example where GotoIf returns true:

-- Executing [998@from-internal:1] Set("SIP/1001-00009834", "junky=1") in new stack
-- Executing [998@from-internal:2] GotoIf("SIP/1001-00009834", "1?congest") in new stack
-- Goto (from-internal,998,4)
-- Executing [998@from-internal:4] Hangup("SIP/1001-00009834", "") in new stack

Since the evaluation returns true the call goes to the congest label skipping the playback.

I think the problem was in the quotes, try it and let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM