Upgrade 3CX to v18 and get it hosted free!

Asterisk cmd GotoIf

Author image

Synopsis

Conditional goto

Description


   GotoIf(condition?[label1][:label2])
   GotoIf(condition?[[[context1,]extension1,]priority1]:[[[context2,]extension2,]priority2])

Either label1 or label2 may be omitted (in that case, we just don’t take the particular branch), but not both.

   GotoIf(condition?label1)

If “condition” is true, go to “label1”. If condition is false, then simply proceed to the next instruction.

   GotoIf(condition?label1:label2)

If “condition” is true, go to “label1”. If condition is false, go to “label2”.

    GotoIf(condition?:label2)

If “condition” is true, go to the next instruction. If “condition” is false, go to “label2”.

condition is just a string. If the string is empty or “0”, the condition is considered to be false. If it is anything else, the condition is true. This is designed to be used together with expression syntax.

Labels take the form ‘[context,]extension,]priority’, so they can be (a) a priority, (b) an extension and a priority, or (c) a context, an extension and a priority. This is the same syntax as for the Goto command.

Example 1


  exten => 206,1,GotoIf($["${CALLERID(num)}" = "303"]?dial1)
  exten => 206,n,GotoIf($["${CALLERID(num)}" != "304"]?moh:dial2)
  exten => 206,n(dial1),Dial(${SPHONE1},15,rt)
  exten => 206,n,Hangup()
  exten => 206,n(dial2),Dial(${PHONE2},15,rt)
  exten => 206,n,Hangup()
  exten => 206,n(moh),MusicOnHold(default)

20050427 – Note that ${CALLERID(num)} can contain spaces (e.g. “No CID available” from some Zap channels), and the example above has been corrected to cope with this situation – without the quotes around ${CALLERID(num)} it doesn’t work!

Example 2


 ; Are the first three digits of the returned value of ${ENUM} are
 ;  either SIP or IAX?  If not,  we ignore and pass to normal
 ;  dialing path at priority 4.
 ;
 exten => _011X.,2,GotoIf($[$["${ENUM:0:3}" = "SIP"] | $["${ENUM:0:3}" = "IAX"]]?3:4)

Example 3

  
  ; This example checks for blank caller ID or 800 numbers.
  ; Callers from 800 numbers (usually telemarketers) or those 
  ; with no caller ID are asked to press 1 to speak with me.
  exten => s,1,NoOp(${CALLERID}) ; log callerID string
  ; check for callerID. If none,  make them hit 1.
  exten => s,n,GotoIf($["${CALLERID(num)}" = ""]?1000)
  ; If 800 number, make them hit 1.
  exten => s,n,GotoIf($["${CALLERID(num):0:3}" = "877"]?1000)
  exten => s,n,GotoIf($["${CALLERID(num):0:3}" = "800"]?1000)
  ; OK, we have valid caller ID and it's not an 800 number.
  ; Let's ring our phones now:
  exten => s,n,Dial(SIP/604&SIP/602,25,tr)
  exten => s,1000,Background(press1tospeaktome)

Example 4 (by Tikal Networks )


Important!!!
If from any reason gotoif dose not work with floating numbers try to cut the floating number and compare it with the first argument
exten => charge_set_up_fee,1,Noop(=== NOW STARTING 'set up fee'  ===)
exten => charge_set_up_fee,n,Set(fee_credit=20.36)
exten => charge_set_up_fee,n,ExecIf($[${fee_credit} < 0.50],Playback,custom/credit_less_then_0_5)
exten => charge_set_up_fee,n,ExecIf($[${fee_credit} < 0.50],Macro,hangupcall,EXIT); play msg and exit if user have lass the 0.5 cent
;exten => charge_set_up_fee,n,GotoIf($[${fee_credit} > 20]?continue) ; this will not work 
exten => charge_set_up_fee,n,GotoIf($[${CUT(fee_credit,\.,1)} > 20]?continue) ; this will work
exten => charge_set_up_fee,n,Set(lala=blabla)
exten => charge_set_up_fee,n(continue),Set(CALL_START_DATE=${STRFTIME(${EPOCH},,%Y-%m-%d %H:%M:%S)})
~pp~


!!Example 5 with ((Asterisk func func_odbc|func_odbc))
__in func_odbc.conf:__
~pp~
  [ISLOCAL]
  dsn=foo
  read=SELECT COUNT(*) FROM localexchanges WHERE prefix='${ARG1:0:6}'

in extensions.conf:


  exten => _011-1-NXX-NXX-XXXX,1,GotoIf(${ODBC_ISLOCAL(${EXTEN:4})}?${EXTEN:4},1:${EXTEN:3},1)

Hints

It is worthwhile adding a test for null values, even though modern versions of Asterisk and/or Bison are known to work,
It seems that some still require a hack to test for null values:

For example, assume that the function CALLERID(num) returns an empty string

  GotoIf($[${CALLERID(num)} = 123456789]?3:2)

will cause an error. Instead, use either

  GotoIf($[foo${CALLERID(num)} = foo123456789]?3:2)

or

  GotoIf($["${CALLERID(num)}" = "123456789"]?3:2)

Note that if CALLERID(num) contains a space, the first alternative (foo${CALLERID(num)}) will fail with an error. The use of speech-marks (“”) is therefore the recommended method.

Likewise you might want to add a leading 0 if you need to perform a numeric comparison:

  $[0${GROUP_COUNT(numcalls)} > 40]

Also, the use of spacing is very important if you’re not using quoted values on both sides of the comparison. So, when testing characters 2 and 3 of ARG1 when ARG1 = “u6432” :


  ; will correctly evaluate to false (and carry on at step 3):
   exten => s,2,GotoIf($[${ARG1:1:2} = 61]?6100)
  ; will always evaluate true (and wrongly jump to step 6100):
   exten => s,2,GotoIf($[${ARG1:1:2}=61]?6100)    
  ; will complain about a mismatched number of characters in 
  ; the comparison (notice the space), thus: 
   exten => s,2,GotoIf($[${ARG1:1:2} =61]?6100) 
ast_yyerror: ast_yyerror(): syntax error: syntax error; Input: 64 =61

See also


Asterisk | Applications | Functions | Variables | Expressions | Asterisk FAQ


Article Reviews

Write a Review

Your email address will not be published. Required fields are marked *

Required Field. Minimum 5 characters.

Required Field. Minimum 5 characters, maximum 50.

Required field.There is an error with this field.

Required Field.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

There are no reviews for this article. Be the first one to write a review.

Related Posts:

Get 3CX - Absolutely Free!
Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.