I see a lot of people solving curl requests via additional scripts.
Typical use case goes something like:
“I need a call context to go get some information, set the result as a variable, and use the variable or argument in my next step.” Knee-jerk reaction is “hey, I can make a script for that and I know how to call a script via dial plan, problem solved!”
Yes, problem solved but not efficiently. Multiple steps makes for more points of failure, in my opinion. Especially if you’re not the only “hands in the pot”.
I’ll use an example of something gaining popularity at the moment, Jitsi meet. For those creating a PBX back-end solution, Asterisk needs to CURL an API in order to retrieve a conference room name. Part of the result is then added as a SIP Header.
API URL: https://jitsi-api.jitsi.net/conferenceMapper?id=YOUR-PIN-HERE
Sending a CURL request with a valid pin (1234567890):
curl --silent https://jitsi-api.jitsi.net/conferenceMapper?id=1234567890
Returns:
{"message":"Successfully retrieved conference mapping","id":1234567890,"conference":"[email protected]"}
It would be nice if we needed the entire string, but the SIP Header should only contain the room name. In this case, “teammeeting”.
This is where folks try to solution with a script to cut down to the desired info, but it can be done easier all in one Dial plan line.
The terminal version of extracting “teammeeting” would be:
curl --silent https://jitsi-api.jitsi.net/conferenceMapper?id=${confid} | sed -e 's/.*"conference":"\(.*\)@.*/\1/'
Key operator being that we added a stream editor command to read the curl result string and trim out what we want.
The SED example below looks for "conference":" , removes it and everything before it. Then, looks for @ removing @ and everything after it
sed -e 's/.*"conference":"\(.*\)@.*/\1/'
Wrapping this up in an Asterisk Dial plan looks something like this:
exten => s,n,Set(CURL_RESULT=${SHELL(curl --silent https://jitsi-api.jitsi.net/conferenceMapper?id=${confid} | sed -e 's/.*"conference":"\(.*\)@.*/\1/')})
exten => s,n,Verbose(0, ${CURL_RESULT});
In the Dial plan line , we’ve obtained the PIN in a previous step, being injected as ${confid}.
Reading an asterisk log would print out the desired ${CURL_RESULT} as “teammeeting”, which is what we needed. No scripts, all one line.