SHELL + cURL In Dialplan

FreePBX 13.0.188.9
Asterisk 13.7.2

Dialplan:

exten => s,1,GotoIf($["${CHANNEL(state)}" = "Up"]?begin)
exten => s,n,Answer
exten => s,n,Wait(1)
exten => s,n(begin),Noop(Playing announcement howdy)
exten => s,n,Playback(custom/howdyhowdyhowdy)
exten => s,n,Set(result=${SHELL(curl -s 'https://somewebsite.com/bcms' |     python -c "import sys, json; print json.load(sys.stdin)['AUX']")})
exten => s,n,SayDigits(${result})
exten => s,n,Hangup

when I curl -s ‘https://somewebsite.com/bcms’ I get a JSON response with multiple fields. I need the value of only one field.

In the Linux command line, when I run:
curl -s ‘https://somewebsite.com/bcms’ | python -c “import sys, json; print json.load(sys.stdin)[‘AUX’]”)})

It works. Linux returns a number result (e.g. 10)

When I try to bake that into the dial plan I get an error:
[2016-10-26 15:40:36] WARNING[13399][C-00002c77]: pbx.c:4771 pbx_substitute_variables_helper_full: Error in extension logic (missing ‘}’)
[2016-10-26 15:40:36] WARNING[13399][C-00002c77]: pbx.c:4274 func_args: Can’t find trailing parenthesis for function ‘SHELL(curl -s ‘https://somewebsite.com/bcms’ | python -c "import sys, jso’?

It seems like I am missing a parentheses, but I don’t see where. Any ideas?

Possibly because your call to SHELL returns an included newline?

http://www.voip-info.org/wiki/view/Asterisk+func+shell

always include a noop or two while debugging

(asterisk also has a curl command builtin)

1 Like

Thanks dicko, got me pointed in the right direction.

curl -s ‘https://somewebsite.com/bcms’ | python -c “import sys, json; print json.load(sys.stdin)[‘AUX’]”)})

In the shell the ; is being treated as a comment indicator. You have to escape it by putting \ in front of it:

Set(result=${SHELL(curl -s ‘https://somewebsite.com/bcms’ | python -c “import sys, json; print json.load(sys.stdin)[‘AUX’]”)})

This returns the correct result (in this example just 10). I was exploring the asterisk cURL command, but was having trouble getting just the one data point (the JSON package has 10).

Thanks again!

1 Like