AGI script doesn't work

extensions_custom.conf

[sms-sender]
exten => s,1,Noop(send sms to ${CALLERID(num)})
exten => s,n,AGI(sms.php, ${CALLERID(number)})
exten => s,n,Goto(app-announcement-9,s,1)

sms.php

#!/usr/bin/php -q
<?php
require('phpagi.php'); 
$agi = new AGI();
$cid = $agi->request['agi_callerid'];
$user = "smsuser";
$pas = "smsuser";
$text = "message ";
$sms = str_replace(" ", "+", $text);
$prov = "1";
$meth = "2";
$url = "https://my-domain/goip/en/dosend.php?USERNAME=$user&PASSWORD=$pas&smsprovider=$prov&smsnum=%2B$cid&method=$meth&Memo=$sms";
	
if(preg_match('/^79[0-9]{9}/',$cid))	{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$out = curl_exec($ch);
	curl_close($ch);
	echo $out;
}
else	{
	echo "not mobile";
}
?>

in the console I see an error message

-- Executing [2@ivr-6:1] Set("SIP/XXXXXXXXXXX-00000232", "__ivrreturn=0") in new stack
-- Executing [2@ivr-6:2] Goto("SIP/XXXXXXXXXXX-00000232", "customdests,dest-4,1") in new stack
-- Goto (customdests,dest-4,1)
-- Executing [dest-4@customdests:1] NoOp("SIP/XXXXXXXXXXX-00000232", "Entering Custom Destination sms-sender") in new stack
-- Executing [dest-4@customdests:2] Gosub("SIP/XXXXXXXXXXX-00000232", "sms-sender,s,1()") in new stack
-- Executing [s@sms-sender:1] NoOp("SIP/XXXXXXXXXXX-00000232", "send sms to XXXXXXXXXXX") in new stack
-- Executing [s@sms-sender:2] AGI("SIP/XXXXXXXXXXX-00000232", "sms.php, XXXXXXXXXXX") in new stack
-- Launched AGI Script /var/lib/asterisk/agi-bin/sms.php
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_request: sms.php
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_channel: SIP/XXXXXXXXXXX-00000232
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_language: ru
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_type: SIP
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_uniqueid: 1613638224.1108
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_version: 16.15.1
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_callerid: XXXXXXXXXXX
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_calleridname: XXXXXXXXXXX
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_callingpres: 0
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_callingani2: 0
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_callington: 0
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_callingtns: 0
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_dnid: XXXXXXXXXXX
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_rdnis: unknown
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_context: sms-sender
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_extension: s
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_priority: 2
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_enhanced: 0.0
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_accountcode:
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_threadid: 140256500864768
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> agi_arg_1:  XXXXXXXXXXX
<SIP/XXXXXXXXXXX-00000232>AGI Tx >>
<SIP/XXXXXXXXXXX-00000232>AGI Rx << Could not open input file:  XXXXXXXXXXX
<SIP/XXXXXXXXXXX-00000232>AGI Tx >> 510 Invalid or unknown command
-- <SIP/XXXXXXXXXXX-00000232>AGI Script sms.php completed, returning 0
-- Executing [s@sms-sender:3] Goto("SIP/XXXXXXXXXXX-00000232", "app-announcement-9,s,1") in new stack
-- Goto (app-announcement-9,s,1)

where did i go wrong?

Hello @snaggy,

Try to set the whole path to the phpagi.php file.

require('/var/lib/asterisk/agi-bin/phpagi.php');

Thank you,

Daniel Friedman
Trixton LTD.

hello @ danielf

It does not help

I tried the same very simple script, but I get the same errors

#!/usr/bin/php -q
<?php
require('phpagi.php'); 
$agi = new AGI(); 
$cid = $agi->request['agi_callerid']; 
mail("[email protected]", 'now call:', $cid); 
?>
1 Like

Maybe the file doesn’t have the correct permissions.

Critically important that the AGI file have Unix end of line characters, if you are editing with windows, that could be it. Also must be owned by asterisk and must be executable.

2 Likes

Hello @snaggy,

It looks like a permission problem. Try to use this pattern in your file:
#!/usr/bin/env php
include '/etc/freepbx.conf';
include FreePBX::Config()->get('ASTAGIDIR')."/phpagi.php";
$agi = new AGI();

Thank you,

Daniel Friedman
Trixton LTD.

1 Like

i created a new file in the nano editor
the script now works correctly!
thank you

1 Like

So it was windows then. I use notepad++ to edit text files and always ensure to save files with UNIX EOL chars.

1 Like

For text editing, Visual Studio Code is the best free tool on the market right now.

It is cross platform, Windows, Mac, Linux.

4 Likes

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