-1

I am trying to consume data from an API to build an IVR in asterisk 11. The CURL call works fine and I get back a JSON array of the data. However, I want to extract the value from specific elements to build an IVR.

Actually, I have an Interactive IVR but it is working with odbc connector which reads the information in a remote MySQL database, this is how it works:

  • A person dials a toll free number
  • If is a client press 2, If is not client , dial 1 and goes to a Sales queue
  • if its client, press 2, then asterisk ask for the number of client, after client number is entered, asterisk reads a field called "deuda" and it plays the amount of money on the account.
  • then the ivr plays the options to pay the debt, or ask for technical support.

I want to do this with the API, I can curl the API this way:

curl -s 'https://api.wisphub.net/api/clientes/' \  --header 'Authorization: Api-Key myapikey' |jq '.'

The result is:
Result of CURL

then based on that, I build the dialplan

exten => 815,1,Read(Servicio,Numero_Servicio,6)
same => n,Set(CURL_RESULT=${SHELL(curl -s 'https://api.wisphub.net/api/clientes/' \  --header 'Authorization: Api-Key myapikey' |jq -r '.')})
same => n,Playback(oxxo)
same => n,SayAlpha(${CURL_RESULT})

I want to build an interactive IVR getting information from an external API

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • It's very unclear what your actual question is, but it seems like it's "how do I do this?" which is way too much for anyone to help with. If you're asking how to parse the JSON, it seems you already know that `jq` is the right tool. There are lots of questions about how to use it effectively. – miken32 Apr 14 '23 at 21:45

1 Answers1

0

You should use this.

JSON_DECODE()
Synopsis

Returns the string value of a JSON object key from a string containing a JSON array.
Description

The JSON_DECODE function retrieves the value of the given variable name and parses it as JSON, returning the value at a specified key. If the key cannot be found, an empty string is returned.
Syntax

JSON_DECODE(varname,item[,separator[,options]])

Arguments

    varname - The name of the variable containing the JSON string to parse.
    item - The name of the key whose value to return.
    Multiple keys can be listed separated by a hierarchy delimeter, which will recursively index into a nested JSON string to retrieve a specific subkey's value.
    separator - A single character that delimits a key hierarchy for nested indexing. Default is a period (.)
    This value should not appear in the key or hierarchy of keys itself, except to delimit the hierarchy of keys.
    options
        c - For keys that reference a JSON array, return the number of items in the array.
        This option has no effect on any other type of value.

Or use ARI/fastAGI interfaces and external control program

arheops
  • 15,544
  • 1
  • 21
  • 27
  • Hello @arheops , actually json_decode was my first option, however it is only present in versions Asterisk 16, 18 and 19, and this pbx has asterisk 11, thanks again for your help, i think i will need to find the way to do it in AGI – Milo Torres Apr 15 '23 at 16:14
  • Using asterisk 11 is REALY bad idea. You can use SHELL/SYSTEM and bash script, but care about injections. https://stackoverflow.com/questions/1955505/parsing-json-with-unix-tools – arheops Apr 15 '23 at 20:08
  • 1
    Or agi script. Better use fastAGI if possible. AGI will be bottleneck for any production system. – arheops Apr 15 '23 at 20:09