How can I pass a variable to an agi script using dynamic routes

I have a dynamic route so that when someone calls, they have to dial their unique id number. Then, the input DTMF value is saved as a variable called “customer_id”.

Once the input DTMF is saved as a variable, then within the same Dynamic Route, I have a Dynamic lookup using an AGI script. This script will do a simple GET API request to an external server using the “customer_id” variable.

The script is written in PHP and I was able to get a test response from the API, but I had to manually hardcode the “customer_id” within the script (This was only for testing purposes). Of course, In production, I need to pass the saved DTMF variable, named “customer_id” to the PHP script.

Is there a way to do this? If so, I would really love to know. Thanks!

I did an example a while back on how to pass variables from the AGI to Asterisk, but it didn’t include how to get values from Asterisk to the AGI. I have just updated it to include this: Using Dynamic Routes with an AGI file

You can use that technique by assigning the DTMF input to a variable. Note that to later reference that variable, you refer to it as DYNROUTE_<varname>, check the tool tip in the GUI.

Thank you for the update!

So at the moment, I was able to get it working the return value from the AGI script.

I also was able to pass an asterisk variable to the AGI script. (CALLERID) However, I am still unable to find a way to pass the input DTMF to the script.

This is what i have at the moment:

<?php

// FreePBX Bootstrap environment (if needed)
include_once('/etc/asterisk/freepbx.conf');

// set up AGI env
include("phpagi.php");   // assumes this agi is saved in same dir as phpagi.php
$AGI = new AGI();

// log something in /var/log/asterisk/full
$AGI->verbose("dynroutes-test.agi starting up");

// Set variable input from DTMF (123456 = only for testing)
$item = " 123456 ";  // <---- I NEED TO REPLACE THIS VARIABLE SO THAT $ITEM equals the input DTMF. 
 
$AGI->verbose("Customer ID" .$item);

// set an asterisk channel variable with the result
$AGI->set_variable("agi-return", $item);

// not needed for random number example, but demonstrates how to get values from Asterisk
// channel variables or functions into the AGI

$calleridnum = $AGI->get_variable("CALLERID(number)");
if ($calleridnum['result'] == 1) {
    $AGI->verbose("Value of function CALLERID(number): ".$calleridnum['data']);
}

exit;

This is my agi debug log:

<PJSIP/Trunk3.com.au-00000a48>AGI Tx >> agi_language: en
<PJSIP/Trunk3.com.au-00000a48>AGI Tx >> agi_type: PJSIP
<PJSIP/Trunk3.com.au-00000a48>AGI Tx >> agi_uniqueid: 1667776826.4111
<PJSIP/Trunk3.com.au-00000a48>AGI Tx >> agi_version: 16.16.1~dfsg-1+deb11u1
<PJSIP/Trunk3.com.au-00000a48>AGI Tx >> agi_callerid: 04507xxxxx
<PJSIP/Trunk3.com.au-00000a48>AGI Tx >> agi_calleridname: 04507xxxxx
<PJSIP/Trunk3.com.au-00000a48>AGI Tx >> agi_callingpres: 0
<PJSIP/Trunk3.com.au-00000a48>AGI Tx >> agi_callingani2: 0
<PJSIP/Trunk3.com.au-00000a48>AGI Tx >> agi_callington: 0
<PJSIP/Trunk3.com.au-00000a48>AGI Tx >> agi_callingtns: 0
<PJSIP/Trunk3.com.au-00000a48>AGI Tx >> agi_dnid: 07557xxxxx
<PJSIP/Trunk3.com.au-00000a48>AGI Tx >> agi_rdnis: unknown
<PJSIP/Trunk3.com.au-00000a48>AGI Tx >> agi_context: dynroute-7
<PJSIP/Trunk3.com.au-00000a48>AGI Tx >> agi_extension: s
<PJSIP/Trunk3.com.au-00000a48>AGI Tx >> agi_priority: 7
<PJSIP/Trunk3.com.au-00000a48>AGI Tx >> agi_enhanced: 0.0
<PJSIP/Trunk3.com.au-00000a48>AGI Tx >> agi_accountcode:
<PJSIP/Trunk3.com.au-00000a48>AGI Tx >> agi_threadid: 14049457225xxxx
<PJSIP/Trunk3.com.au-00000a48>AGI Tx >>
<PJSIP/Trunk3.com.au-00000a48>AGI Rx << VERBOSE "dynroutes-test.agi starting up" 1
 agi://127.0.0.1//var/lib/asterisk/agi-bin/dynroutes-test.agi: dynroutes-test.agi starting up
<PJSIP/Trunk3.com.au-00000a48>AGI Tx >> 200 result=1
<PJSIP/Trunk3.com.au-00000a48>AGI Rx << VERBOSE "Customer ID 123456 " 1
 agi://127.0.0.1//var/lib/asterisk/agi-bin/dynroutes-test.agi: Customer ID 123456
<PJSIP/Trunk3.com.au-00000a48>AGI Tx >> 200 result=1
<PJSIP/Trunk3.com.au-00000a48>AGI Rx << SET VARIABLE agi-return " 123456 "
<PJSIP/Trunk3.com.au-00000a48>AGI Tx >> 200 result=1
<PJSIP/Trunk3.com.au-00000a48>AGI Rx << GET VARIABLE CALLERID(number)
<PJSIP/Trunk3.com.au-00000a48>AGI Tx >> 200 result=1 (04507xxxxx)
<PJSIP/Trunk3.com.au-00000a48>AGI Rx << VERBOSE "Value of function CALLERID(number): 04507xxxxx" 1
 agi://127.0.0.1//var/lib/asterisk/agi-bin/dynroutes-test.agi: Value of function CALLERID(number): 04507xxxxx

I was able to get the CALLERID variable from Asterisk to the AGI script; however, I am still struggling to get the input DTMF variable, which i called “stdt_id” from the Dynamic Routes.

Here is what it looks like in FreePBX:

Is there a way to do this?

https://wiki.freepbx.org/display/FPG/Dynamic+Routes+User+Guide

To reference the variable in Asterisk dialplan (e.g. custom applications) it is necessary to prefix it with “DYNROUTE_” i.e. ${DYNROUTE_name}

1 Like

Yes, what @mahbell said. At it’s most basic, your script will look like:

<?php

// set up AGI env
include("phpagi.php");   // assumes this agi is saved in same dir as phpagi.php
$AGI = new AGI();

$userinput = $AGI->get_variable("DYNROUTE_ stdt_id");
$AGI->verbose("User DTMF input: ".$userinput['data']);
exit;
1 Like

Wow, that approach it’s so much simpler that my workaround.

I didn’t know I was able to call the DYNROUTES_variable within PHP, I thought it was only accessible from the GUI. As for this, I end up up finding and alternative way to send a variable from the Dynamic Routes to PHP.

image

Basically, I created a separate Dynamic Route that only does API lookups and then I added the [stdt_id] as a parameter from the previous Dynamic Route. Then I called the script using this AGI lookup: /var/lib/asterisk/agi-bin/dynroutes-api.agi,[stdt_id]

Afterwards, within PHP script, I saved the parameter [stdt_id] as a variable:

// parsing dtmf argument to variable
$item = "$argv[1]";
$AGI->verbose("dtmf value " .$item);

// converting argv to integer for api lookup
$sid = (int)$item;

And at the end, I saved the AGI result as a variable:

$AGI->set_variable("sid-name", $id);

I noticed that to reference this saved variable [sid-name] I had to add it under the AGI Result Variable box, as well as the Saved result variable name box
image
image

Once it was saved, I was able to reference the variable as ${DYNROUTE_sid-name} anywhere along the call flow :smiley:

I think it was such an unnecessary approach compared to lgaetz but it does the job!

Thank you anyway for the help! Appreciate it!

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.