Friday, October 29, 2010

Asterisk cmd Set

Sets variable to value

Version differences:
This command is not available in Asterisk 1.0.9. Use SetVar instead.
As of v1.2 SetVar is deprecated and we are back to Set.
As of v1.4 the use of Set() to set multiple variables at once and the g flag have both been deprecated. Please use multiple Set() calls and the GLOBAL() dialplan function instead.
As of v1.6 only if you have a corresponding "compat" setting, then Set() does not strip surrounding quotes from the right hand side as it did previously.


 Set(variablename=value|options]) (starting with Asterisk 1.4)
 Set(variablename=value[|variable2=value2][|options]) (up to and including Asterisk 1.2)

This application can be used to set the value of channel variables or dialplan functions. It will accept up to 24 name/value pairs upto Asterisk 1.2, but only one name/value pair in Asterisk 1.4 or later.
When setting variables, if the variable name is prefixed with _ the variable will be inherited into channels created from the current channel. If the variable name is prefixed with __ the variable will be inherited into channels created from the current channel and all children channels.
Next to the Set() command there is also the SET function available.

g: set a global variable (valid in the entire dialplan, not just the channel)   exten => 100,1,Set(GLOBAL(FOO)=456)

extensions.conf:
; If clearglobalvars is not set, then global variables will persist
; through reloads, and even if deleted from the extensions.conf or
; one if its included files, will remain set to the previous value.
;
clearglobalvars=no

Note that Set() changes behaviour in Asterisk 1.6 which can be controlled via asterisk.conf:

 [compat]
 app_set=1.6

If (and only if), in /etc/asterisk/asterisk.conf, you have a compat category, and you have app_set = 1.6 under that, then the behavior of this app changes, and does not strip surrounding quotes from the right hand side as it did previously.

 Set(numTries=4)
 Set(CALLERID(number)=000000)
 Set(CALLERID(name)="The Name")
 Set(NIGHTMODE=1,g) ; set a global variable

To increment a variable, you can use:

 Set(total=$${total} + 1)

To set inherited variable:

 Set(_CALLID=${UNIQUEID})
 Set(__CALLID=${UNIQUEID})

NOTES:
Variable names are not case sensitive. Each channel gets its own variable space. There is no chance of collisions between different calls, and the variable is automatically trashed when the channel is hangup. Make sure you do not put spaces around the equals sign in the assignment. Set(numTries = 4),with a space on either side of the "=", will set numtries to "". If trying to zero out the CALLERID(name) do not use empty quotes, use Set(CALLERID(name)=)
Try using the variable in your dialplan:

 Playback(${variablename})
 SayDigits(${variablename})


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

No comments:

Post a Comment