FreePBX-ContributedModules / dynroute

GNU General Public License v3.0
1 stars 3 forks source link

AGI script does not appear to run in v17 edge module #7

Closed chopsywa closed 5 months ago

chopsywa commented 5 months ago

I have a PHP script called using AGI. On v16 it works perfectly. On v17 (Edge version), the script never runs (I have some embedded AGI-verbose lines embedded. If I run with agi debug on, I get the following output when I instigate the script by dialing the dynamic route extension.

dump exec set show pbxCLI> agi set debug on AGI Debugging Enabled AGI Tx >> agi_network: yes AGI Tx >> agi_network_script: /var/lib/asterisk/agi-bin/steerInbound.php <PJSIP/101-0000001d>AGI Tx >> agi_request: agi://127.0.0.1//var/lib/asterisk/agi-bin/steerInbound.php <PJSIP/101-0000001d>AGI Tx >> agi_channel: PJSIP/101-0000001d <PJSIP/101-0000001d>AGI Tx >> agi_language: en_AU <PJSIP/101-0000001d>AGI Tx >> agi_type: PJSIP <PJSIP/101-0000001d>AGI Tx >> agi_uniqueid: 1714459833.37 <PJSIP/101-0000001d>AGI Tx >> agi_version: 21.0.2 <PJSIP/101-0000001d>AGI Tx >> agi_callerid: 101 <PJSIP/101-0000001d>AGI Tx >> agi_calleridname: Mark Dutton <PJSIP/101-0000001d>AGI Tx >> agi_callingpres: 0 <PJSIP/101-0000001d>AGI Tx >> agi_callingani2: 0 <PJSIP/101-0000001d>AGI Tx >> agi_callington: 0 <PJSIP/101-0000001d>AGI Tx >> agi_callingtns: 0 <PJSIP/101-0000001d>AGI Tx >> agi_dnid: *100 <PJSIP/101-0000001d>AGI Tx >> agi_rdnis: unknown <PJSIP/101-0000001d>AGI Tx >> agi_context: dynroute-1 <PJSIP/101-0000001d>AGI Tx >> agi_extension: s <PJSIP/101-0000001d>AGI Tx >> agi_priority: 2 <PJSIP/101-0000001d>AGI Tx >> agi_enhanced: 0.0 <PJSIP/101-0000001d>AGI Tx >> agi_accountcode: <PJSIP/101-0000001d>AGI Tx >> agi_threadid: 139654043944640 <PJSIP/101-0000001d>AGI Tx >> <PJSIP/101-0000001d>AGI Rx << Exception: Zend Extension "/var/lib/asterisk/agi-bin/steerInbound.php" does not exist <PJSIP/101-0000001d>AGI Tx >> 510 Invalid or unknown command <PJSIP/101-0000001d>AGI Tx >> HANGUP pbxCLI>

I am wondering if the bolded line is correct.

The script is error free and works on v16 as is.

Script is here.

#!/usr/bin/php -q
<?php

// System variables
$clientid[0] = "clientid:****************";
$user = "***************";
$secret = "*************";
$url = "https://api-aus.myconnectwise.net/v4_6_release/apis/3.0/";
$locationID = 0;

// set up AGI env
include("phpagi.php");
$AGI = new AGI();

$calleridnum = $AGI->get_variable("CALLERID(number)");
if ($calleridnum['result'] == 1) {
        $callerid = $calleridnum['data'];
//      $callerid = '1300310310';
//      Check contacts
        $full_url = $url."company/contacts?fields=companyLocation&conditions=inactiveFlag=false";
        $full_url .= "&childconditions=communicationItems/communicationType='Phone'%20AND%20communicationItems/value='".$c                               allerid."'";

        $jsonRaw = CallAPI($full_url);
//      $AGI->verbose("Curl Result: ".$jsonRaw);
        $companyJson = json_decode($jsonRaw,true);
        $locationID = $companyJson[0]["companyLocation"]["id"];
        if ($locationID == NULL) {
//              Number wasn't in the contact, so let's try the company
                $full_url = $url."company/companies?fields=phoneNumber,territory&conditions=phoneNumber='".$callerid."'";
                $jsonRaw = CallAPI($full_url);
//              $AGI->verbose("Curl Result: ".$jsonRaw);
                $companyJson = json_decode($jsonRaw,true);
                $locationID = $companyJson[0]["territory"]["id"];
//              $AGI->verbose($locationID);
        }
        if ($locationID == NULL) {
                exit;
        }
//      Search for the queue number from the location ID
        $full_url = $url."system/locations/".$locationID."?fields=name,overrideFaxNumber";
        $jsonRaw = CallAPI($full_url);
//      $AGI->verbose("Curl Result: ".$jsonRaw);
        $companyJson = json_decode($jsonRaw,true);
        $target = $companyJson["overrideFaxNumber"];
        $AGI->set_variable("teamExt", $target);
}

exit;

function CallAPI($url)
{
        global $user, $secret, $clientid, $AGI;

//      $AGI->verbose($url);
        $curl = curl_init($url);

        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($curl, CURLOPT_USERPWD, $user.":".$secret);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $clientid);

        $result = curl_exec($curl);
        curl_close($curl);
        return $result;
}

?>

The dynamic routing page components pertaining to the script look like this.

image

kguptasangoma commented 5 months ago

easy way to debug your AGI is to "php /var/lib/asterisk/agi-bin/steerInbound.php" and confirm it executes properly. It may be some issue due to php compatibility . 17 is using PHP 8.2 which is too strict.

chopsywa commented 5 months ago

I've done this. It runs fine from the command line. I can't get any info in, or out of the AGI object obviously as it's not in scope, but there is no runtime error.

php -l shows no syntax errors and when I run the command off the command line, my rest calls return a valid result using one of my test numbers embedded in the script.

I don't think the script is failing. I just don't think the script ever runs.

I just made this script

#!/usr/bin/php -q
<?php

// set up AGI env
include("phpagi.php");
$AGI = new AGI();

$AGI->verbose("Hello world");

?>

and I get the same result.

Regards

Mark Dutton

kguptasangoma commented 5 months ago

Hi @chopsywa are you saying no AGI script is running for you?

May be try to check any open source module AGI script if that can give you some idea?

As this issue is specific to your script so requesting you to please take help from the community.

chopsywa commented 5 months ago

I don't think it is specific to my script. As you can see above, I made a script that simply instantiates the AGI object and creates verbose output of a fixed string and this does not run either. No open source script will be simpler than this.

It works fine on v16, so I am not sure what I will get from the community. Isn't John Fawcett maintaining dynamic routes for v17? I will post in the community and see what comes back.

kguptasangoma commented 5 months ago

okay i am testing your script on my system, will let you know the result asap.

kguptasangoma commented 5 months ago

Its working fine for me.

Steps I followed -

1) Copy your AGI to Asterisk AGI folder with asterisk permissions. (Note permission has be asterisk:asterisk) `root@vultr:/var/lib/asterisk/agi-bin# cat testphp.php

!/usr/bin/php -q

<?php

// set up AGI env include("phpagi.php"); $AGI = new AGI();

$AGI->verbose("Hello world");

?> `

2) Created dialplan to trigger the AGI

root@vultr:/etc/asterisk# cat extensions_custom.conf exten => 8123322572,1,Answer() same => n,AGI(testphp.php) same => n,Hangup()

3) See the Asterisk logs of AGI triggered during the call -

`vultr*CLI> == Using SIP RTP Audio TOS bits 184 == Using SIP RTP Audio TOS bits 184 in TCLASS field. == Using SIP RTP Audio CoS mark 5 -- Executing [8123322572@from-internal:1] Answer("PJSIP/2001-00000004", "") in new stack

0x7fa6b805e460 -- Strict RTP learning after remote address set to: 10.204.0.199:39346 -- Executing [8123322572@from-internal:2] AGI("PJSIP/2001-00000004", "testphp.php") in new stack -- Launched AGI Script /var/lib/asterisk/agi-bin/testphp.php <PJSIP/2001-00000004>AGI Tx >> agi_request: testphp.php <PJSIP/2001-00000004>AGI Tx >> agi_channel: PJSIP/2001-00000004 <PJSIP/2001-00000004>AGI Tx >> agi_language: en <PJSIP/2001-00000004>AGI Tx >> agi_type: PJSIP <PJSIP/2001-00000004>AGI Tx >> agi_uniqueid: 1714469509.4 <PJSIP/2001-00000004>AGI Tx >> agi_version: 21.0.2 <PJSIP/2001-00000004>AGI Tx >> agi_callerid: 2001 <PJSIP/2001-00000004>AGI Tx >> agi_calleridname: Ext-2001 <PJSIP/2001-00000004>AGI Tx >> agi_callingpres: 0 <PJSIP/2001-00000004>AGI Tx >> agi_callingani2: 0 <PJSIP/2001-00000004>AGI Tx >> agi_callington: 0 <PJSIP/2001-00000004>AGI Tx >> agi_callingtns: 0 <PJSIP/2001-00000004>AGI Tx >> agi_dnid: 8123322572 <PJSIP/2001-00000004>AGI Tx >> agi_rdnis: unknown <PJSIP/2001-00000004>AGI Tx >> agi_context: from-internal <PJSIP/2001-00000004>AGI Tx >> agi_extension: 8123322572 <PJSIP/2001-00000004>AGI Tx >> agi_priority: 2 <PJSIP/2001-00000004>AGI Tx >> agi_enhanced: 0.0 <PJSIP/2001-00000004>AGI Tx >> agi_accountcode: <PJSIP/2001-00000004>AGI Tx >> agi_threadid: 140353988445888 <PJSIP/2001-00000004>AGI Tx >> 0x7fa6b805e460 -- Strict RTP qualifying stream type: audio <PJSIP/2001-00000004>AGI Rx << VERBOSE "Hello world" 1 testphp.php: Hello world <PJSIP/2001-00000004>AGI Tx >> 200 result=1 -- <PJSIP/2001-00000004>AGI Script testphp.php completed, returning 0 -- Executing [8123322572@from-internal:3] Hangup("PJSIP/2001-00000004", "") in new stack

`

kguptasangoma commented 5 months ago

Not seeing any issue with the normal AGI in the system, now not sure if its your system or triggering from dynroute module is having some issue.

lgaetz commented 5 months ago

My testing agrees with @kguptasangoma, no issues with AGI for me.

@chopsywa are you working in a Windows environment? If so, it's critically important in AGI files for the EOL end of line chars to be UNIX style (LF) not windows (CRLF).

jcolp commented 5 months ago

A difference in testing is that FreePBX itself is executing using its FastAGI execution method, and not directly.

chopsywa commented 5 months ago

I'm doing everything in Linux via CLI with Nano. I don't think AGI itself is the issue. It's the dynamic-routes module that calls the AGI system. I tried to use that same line in my extensions_custom.conf file and it doesn't work for me, but I am not advanced enough in the dialplan syntax. I have this single line in my extensions.conf file that I have added initially through CLI, but now looking at it through freepbx web interface. exten => 121,1,Answer() same => n,AGI(test.php) same => n,Hangup()

I just get a "number is not in service error". I suspect I am supposed to add a context? I am sure it would work for me too if I got the dialplan entry right, but I am trying to use the dynamic routes module, where I select agi from the dropdown and the fullpath of the script in the AGI Lookup field.

chopsywa commented 5 months ago

OK. I copied the script from the screen and pasted into Notepad++ and it appears that Nano uses crlf. I converted the eol to Unix style and pasted back with vi and this made my 2 line file work.

chopsywa commented 5 months ago

@lgaetz you were on the money. The scripts did indeed have CRLF. I scp'd them back to Windows and stripped the CR with Notepad++ and copied back to Linux and they worked. I have to say now I am not sure if I had at some point copied and pasted from Linux into Windows Notepad++ and then copied back again. If I don't specifically tell Notepad++ to treat lines as Linux, it will put a CRLF on the end even when they are pasted directly from a putty session.

PHP always worked from the command line.

Thank you for helping here and to @kguptasangoma as well. I feel sorta bad for engaging you both on this, but on the upside, I did find an issue in the dynamic routes page which is a bug (missing description field in issue #6).