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.
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;
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.
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;
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.
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
Once it was saved, I was able to reference the variable as ${DYNROUTE_sid-name} anywhere along the call flow
I think it was such an unnecessary approach compared to lgaetz but it does the job!