Use of AGI 'SET VARIABLE'


#1

I’m trying to set a variable with AGI.
The variable is for caller id and needs to be in the format:
“Elvis Presley” <5551212>

However, when a variable in my script holds this value, and I use:

SET VARIABLE var value

…the quotes are causing AGI to stop processing the variable and omit everything after the quotes.

for example, in python:
sys.stderr.write(“SET VARIABLE %s %s” % (var, val))

or even:
sys.stderr.write(“SET VARIABLE “%s” %s” % (var, val))

ends up setting the variable var to:
“Elvis Presley”

I’ve also tried passing the following possible alternatives to Asterisk:
“Elvis Prelsey” <5551212>
‘Elvis Presley’ <5551212>

In either case, all Asterisk gets is ‘Elvis’

Anyone know how to get around this? Once again, I’m just trying to set a variable in Asterisk and hitting walls. sigh


#2

This is not an Asterisk issue but your knowledge of the scripting language you are using. Might I suggest a Python forum?

While I do not use Python, I have had no problems with PHP, Perl or Java.


#3

Its not only a problem of the scripting language you use.
Asterisk also needs escaping and quoting so thats what you have to add in addition to the escaping that your scripting language needs.

In your example you must send the following to Asterisk:

In Java the code to do that looks like this:

    protected String escapeAndQuote(String s)
    {
        String tmp;

        if (s == null)
        {
            return "\"\"";
        }

        tmp = s;
        tmp = tmp.replaceAll("\\\"", "\\\\\""); // escape quotes
        tmp = tmp.replaceAll("\\\n", ""); // filter newline
        return "\"" + tmp + "\""; // add quotes
    }

#4

[quote=“srt”]Its not only a problem of the scripting language you use.
Asterisk also needs escaping and quoting so thats what you have to add in addition to the escaping that your scripting language needs.[/quote]

Indeed, but all of this is documented, just have to know how to do it in the language of choice. Details here:

voip-info.org/wiki-Asterisk+AGI
home.cogeco.ca/~camstuff/agi.html#SETVARIABLE

If you want double quotes inside the value then you have to escape them. For example to create a variable CID whose value is "John Doe"<555-1212> you could use: SET VARIABLE CID ""John Doe "<555-1212>