Setting/Getting Variables

Every time I try and implement something in Asterisk, it’s like pulling teeth. I don’t know why this is so difficult.

We have a situation we we’d like to override the callerid= value in sip.conf with a new value when calls are placed from this extention through our sip proxy. Calls that do not go through the proxy will keep the original callerid= value set for the extension in sip.conf

So, I figured we’d just set a variable when the context for this user was entered. Simple eh? Then, later on, when the dial command is executed to put the call through the sip proxy, we’d just set the caller id to be the value of the variable. Simple eh? Wrong!

extensions.conf:
[context_user1]
caller_id = 5551212
include => local

[context_user2]
caller_id = 5551213
include => local

[local]
exten => _1X.,1,Set(CALLERID(name)=${caller_id})
exten => _1X.,2,Dial(${EXTEN}@sip_proxy)

Guess what, this doesn’t work. The docs say that you can only set variables in the [global] context, or by using SetVar when matching an extension. Well, before we get to dialling the SIP proxy, we haven’t matched an extension yet, so how the heck do we call SetVar?

Does anyone know how to do this?

And before anyone asks why I want to do this, as I know they will. Lets assume you have a business. They have a main number that when dialled rings both their phones. That’s easy. Got that working. When they dial out to the PSTN, we don’t want their caller id to be displayed. We just want the main number to be displayed. We COULD set both phones to have the same main number’s caller id, which works. HOWEVER, when you try and do things that depend on the caller id of the phone, it breaks things because both phones appear to have the same caller id. Take queues for example. You might want to call AgentCallbacklogin and pass the caller id of the phone to the command.

I just don’t know why this has to be so difficult in Asterisk.
Help appreciated!

Doug

There is a learning curve associated to Asterisk that gives the perception to new users that it is ‘difficult’, when in reality it is quite straightforward. I would recommend picking up this book if you have not already:

voip-info.org/wiki/view/Aste … +Telephony

In terms of what you are trying to do, the only time a context is entered is when something has been dialed, therefore an extension is defined. Based on your description, it appears you are using a 3rd party SIP proxy, is this to a provder, or?

One way to do this would be somthing like this:

[code][context_usr_a]
exten => s,1,SetVar(set your var here)
exten => s,2,Goto(local|s|1)

[local]
exten => s,1,Dial(${EXTEN}@sip_proxy)[/code]

Thanks for the reply.

That’s the Oreilly book that is publically available isn’t it? I had a look at it. Like the vast majority of Asterisk resources, it barely goes into any detail at all. It barely even touches on contexts for example, except to give the same old defition that everyone else does. Oreilly should come out with an Asterisk Cook Book. That would be sweet.

The SIP proxy isn’t a provider. It’s our own installation of SER. It’s forwarding packets to an Audicodes Mediant Gateway.

I was just looking at the definition of the s extension at:
voip-info.org/wiki/index.php … +extension

For pete’s sake! What does that mean in plain english? It says that the ‘s’ extension is called when no other extension matches in the current context… well that’s because we’re putting it at the beginning of the context and nothing else has had a CHANCE to match yet. So does this mean that once the ‘s’ extension is matched, that execution in extensions.conf ends like it would if any other real extension was matched??? If so, this really makes no sense at all. Maybe it should be called ‘e’ for end extension instead of ‘s’ for start extension.

I think that’s what is bugging me about Asterisk (apart from the lack of good docs). Nothing seems to be intuitive. Everything is inferred.

If you wouldn’t mind terribly, could you explain how your example works? Thanks.

In plain English? Think of ‘s’ as a wildcard matching, that will match to any extension dialed.

This is false, and the source of your confusion. The ONLY time a context is entered into is when something has been dialed to start the process. So there is ALWAYS an extension that has triggered entry into a context and cascades (Gotos/Gototifs/etc) into follow-on contexts.

The ‘s’ is matching a real extension, just as a wildcard as opposed to pattern matching or explicit.

Arguing over semantics in an opensource project will prove fruitless. It is what it is and may be used as identified in the link you posted above.

Welcome to the world of opensource.

Welcome to technology. Have you worked your way through an in depth configuration of Apache lately?

[code][context_usr_a]
exten => s,1,SetVar(set your var here)
exten => s,2,Goto(local|s|1)

[local]
exten => s,1,Dial(${EXTEN}@sip_proxy)[/code]

User A dials a number (ie - 1415551212) and their default context defined in /etc/asterisk/sip.conf is invoked, in this case ‘context_usr_a’. At this point ‘s’ matches 14155551212, as it is a wildcard, and applies the logic in that context: apply an individual CLI and Goto the generic local context. The local context then also matches the extension and executes the dial command.

Oh my god. That makes no sense. Talk about convoluted and hard to interpret. Thanks for trying.

Would this be equivalent?

[context_usr_a]
exten => s,1,SetVar(set your var here)
include => local

[local]
exten => _1X.,1,Dial(${EXTEN}@sip_proxy)

Is the following true?
User A dials a number (ie - 1415551212) and their default context defined in /etc/asterisk/sip.conf is invoked, in this case ‘context_usr_a’. At this point ‘s’ matches 14155551212, as it is a wildcard, and applies the logic in that context (sets the variable). The context_user_a context then includes the local context, where the number dialled, 14155551212, matches _1X., and that dial command is executed.

I don’t understand why your using s in [local]. Why use the wild card again? Why not match against a real number? What if you wanted to execute different dial commands if the user dialled 1303 or 1415??? Then you couldn’t use the s extension could you? You’d have to use _1303. or _1415. Right?

Also, if s is a wild card extension, and matches any number, what happens after you execute the dial command in [local]? Does execution keep running after this command? I ask because that’s what happened further up in [context_user_a] where you used s… after setting the variable, execution kept going. So… That really makes no sense. Does execution keep going after you run the dial in the [local] context?

And… is it better to use ‘include =>’ to gain access to other contexts or ‘goto’?

This is insane.

[quote=“dgarstang”]Oh my god. That makes no sense. Talk about convoluted and hard to interpret. Thanks for trying.

Would this be equivalent?

[code][context_usr_a]
exten => s,1,SetVar(set your var here)
include => local

[local]
exten => _1X.,1,Dial(${EXTEN}@sip_proxy)[/code]

Is the following true?
User A dials a number (ie - 1415551212) and their default context defined in /etc/asterisk/sip.conf is invoked, in this case ‘context_usr_a’. At this point ‘s’ matches 14155551212, as it is a wildcard, and applies the logic in that context (sets the variable). The context_user_a context then includes the local context, where the number dialled, 14155551212, matches _1X., and that dial command is executed.[/quote]

There are a number of ways to do this, the best is for you to test various scenarios to understand how things work. My recommendation would be to actually call a Macro within one context for all users to set a custom callID using the variables available.

Asterisk Variables

This was simply an example, there are many different ways to skin the cat. Start with the basics, test, understand, then expand and add new features.

There are ways to do post-processing:

  • Call a macro on connect
  • Use an option to continue execution after disconnect
  • give your dial command a timeout, such as:

exten => s,1,Dial(SIP/name@proxy.com|30)

It will timeout after 30 seconds of no answer and go to the next line for execution.

Source

Depends on how you have configured the system, as highlighted above. Why would it not make sense to continue execution?

Depends on what you are trying to acheive and how you want to design your dialplan. Whatever works for you.

Not at all, perfectly logical and exceptionally extensible. Try doing these things on an Avaya, Nortel, Siemens, Alcatel, etc. You have to remember, Asterisk is at the inflection point between telephony and data, historically two very different worlds.

Either you accept Asterisk as it is, or you make your own modifications, you may extend via AGI/FastAGI into the language/logic of your choice, or you fight it and continue to be frustrated. Choice is yours, Asterisk is opensource/community developed and you may do what you want with it as long as you do not violate the GPL.

After doing some reading, I don’t think the s extension is going to work. Some docs I just read said that ‘s’ is NOT A WILDCARD extension. It is only called when the user has dialled an extension that is not within the current context space.

I therefore fail to see how using the ‘s’ extension will allow me to set a variable, as there WILL be other valid extensions within the context and therefore s will never be called.

[quote=“dgarstang”]After doing some reading, I don’t think the s extension is going to work. Some docs I just read said that ‘s’ is NOT A WILDCARD extension. It is only called when the user has dialled an extension that is not within the current context space.

I therefore fail to see how using the ‘s’ extension will allow me to set a variable, as there WILL be other valid extensions within the context and therefore s will never be called.[/quote]

In an effort to maintain a simple explanation, using the ‘s’ extension as wildcard analogy works as the ‘s’ extension will catch any extension that is dialed coming into the context. There of course are wildcards specific to pattern matching strings for the extensions as well that could be used (such as ‘.’, which is a ‘true’ wildcard for pattern matching).

It is possible to use the ‘s’ extension to set a variable/call a macro to do the work and then goto a secondary context that would perform the dialing based on the original ${exten} dialed. I have used this before with no problems.

So why doesn’t this work???

sip.conf:
[2001]
context=c_2001

extensions.conf:
[c_2001]
exten => s,1,Answer
exten => s,2,Playback(tt-weasels)
exten => s,3,Hangup

Any number I dial should cause playback. It does not. Asterisk returns a 404 Not Found. Why?

[quote=“dgarstang”]So why doesn’t this work???

sip.conf:
[2001]
context=c_2001

extensions.conf:
[c_2001]
exten => s,1,Answer
exten => s,2,Playback(tt-weasels)
exten => s,3,Hangup

Any number I dial should cause playback. It does not. Asterisk returns a 404 Not Found. Why?[/quote]

As I said in the other post, please post the verbose output of your CLI when this error occurs. Although this looks like a SIP configuration issue (ie - when you are dialing 2001 is not being found, or you are not dialing correctly, so also post the outbound piece of your extensions.conf) as opposed to an extensions issue.