Absolute NOOB... setting up PHP AGI scripting

Hi,

I’m very new to Asterisk and Linux, I have quite a bit of experiance developing on the windows platform, so I get most concepts just need to learn to find my way here…

I’m trying to setup PHP based AGI scripting, on a clean AsteriskNow installation.
I’ve been trying all kinds of things, amonst them installing php using some command…
By executing ‘php test.php’ in my agi-bin directory
I seem to get some restults…
but things do not seem to work the way they should yet…

Could someone point me to a walkthrough for beginners on this???

try this template script:

#!/usr/bin/php -q
<?php
declare(ticks = 1);
ob_implicit_flush(true);
function sig_handler($signo){
//this function is called when the caller hangs up
}

pcntl_signal(SIGHUP,"sig_handler");

//MAIN PROGRAM

?>

and save it in /var/lib/asterisk/agi-bin
don’t forget to give the permission “chmod 755”

in extension_custom.conf write:

and call 9000 from the phone.

Thank you for helping!

Does not seem to work yet though… when calling 9000 the line just gets disconnected…

(if I replace ivr.agi with the demo file agi-test.agi it does execute…)

post me your script

The script is identical to the one you posted (copy-past…)

The only difference is that I call the script from the [default] context in the extensions.conf file.

But I’m quite sure that part works:
if I connect and then dail 6000, I get the conference
when I dail 6001 I get the default supplied agi-test.agi script (perl)
but when dailing 9000… nothing, just a disconnect. (and yes I did rename your file from ivr.agi to mytest.agi)

[default]
include=voicemenu-custom-1
exten=6000,1,MeetMe(${EXTEN}|MI)
exten=6001,1,AGI(agi-test.agi)
exten=9000,1,AGI(mytest.agi)

You must replace the comment //MAIN PROGRAM with php code!
Try this:

[code]#!/usr/bin/php -q

<?php declare(ticks = 1); ob_implicit_flush(true); function sig_handler($signo){ //this function is called when the caller hangs up //for example you can write into log db } pcntl_signal(SIGHUP,"sig_handler"); //detect caller hangs up //dont' forget this import require_once ('phpagi.php'); require_once ('phpagi-asmanager.php'); $agi = new AGI(); $agi->answer(); $agi->verbose("CALLER ID:" . $agi->request[agi_callerid]); $agi->verbose("CALLED ID:" . $agi->request[agi_extension]); $agi->exec("SAY DIGITS 12345"); //instead of this you can use another function like playback,..... $agi->exec("WAIT 1"); //wait 1 second $agi->hangup(); exit(); ?>[/code]

Duh… my bad…

thank you again for trying… no go yet though… I get the feeling PHP scripts just are not being called or processed correctly…

How can I debug this?

(and: is PHP the correct choice for me (see http://forums.digium.com/viewtopic.php?t=18749)

Thank you again…

sorry, you can use pcntl_signal if you compile php with option “–enable-pcntl”.
look at
http://www.trixbox.org/forums/trixbox-forums/open-discussion/keep-agi-php-script-running-after-hangup

So comment the line with pcntl and it must work!
You can use also another language to permorm this script, with php i realize complex ivr…

This must work:

[code]#!/usr/bin/php -q

<?php declare(ticks = 1); ob_implicit_flush(true); /* function sig_handler($signo){ //this function is called when the caller hangs up //for example you can write into log db } pcntl_signal(SIGHUP,"sig_handler"); //detect caller hangs up */ //dont' forget this import require_once ('phpagi.php'); require_once ('phpagi-asmanager.php'); $agi = new AGI(); $agi->answer(); $agi->verbose("CALLER ID:" . $agi->request[agi_callerid]); $agi->verbose("CALLED ID:" . $agi->request[agi_extension]); $agi->exec("SAY DIGITS 12345"); //instead of this you can use another function like playback,..... $agi->exec("WAIT 1"); //wait 1 second $agi->hangup(); exit(); ?>[/code]

I’ve tried your example (also installed the pgpagi files)

I still get disconnected…

When I run the php file from the command line i get these results:
(need to press enter a lot…)

[code][root@localhost agi-bin]# php mytest.agi
PHP Warning: mime_magic: type regex BEGIN[[:space:]]*[{] application/x-awk invalid in Unknown on line 0
PHP Warning: mime_magic: type search/400 \input text/x-tex invalid in Unknown on line 0
PHP Warning: mime_magic: type search/400 \section text/x-tex invalid in Unknown on line 0
PHP Warning: mime_magic: type search/400 \setlength text/x-tex invalid in Unknown on line 0
PHP Warning: mime_magic: type search/400 \documentstyle text/x-tex invalid in Unknown on line 0
PHP Warning: mime_magic: type search/400 \chapter text/x-tex invalid in Unknown on line 0
PHP Warning: mime_magic: type search/400 \documentclass text/x-tex invalid in Unknown on line 0

ANSWER

VERBOSE “CALLER ID:” 1

VERBOSE “CALLED ID:” 1

EXEC SAY DIGITS 12345

EXEC WAIT 1

HANGUP

[root@localhost agi-bin]#[/code]

Try this:

[code]#!/usr/bin/php -q

<?php ob_implicit_flush(true); set_time_limit('1800'); //the max life in seconds of the script //dont' forget this import require_once ('phpagi.php'); require_once ('phpagi-asmanager.php'); $agi = new AGI(); $agi->answer(); $agi->verbose("-----------ANSWER-------------"); $agi->exec("WAIT 1"); //wait 1 second $agi->hangup(); exit(); ?>[/code]

When you are on the CLI, before call the 9000 digits agi debug and press enter. After call 9000 and you must see -----------ANSWER-------------
If doesn’t appear post me the CLI result…

UH… on the risk that I reveal myself as a compleet NOOB…

Which CLI?

I have 2 options right now:

  1. ssh connection to my AsteriskNow installation (login as admin --> su to get root access).
    I think this is the linux console. When I run agi debug here someone named bash tells me the command is not found…

  2. the Asterisk CLI option in the AstriskNow GUI (web)
    this allows me to execute commands but does not seem to return any debug info.
    when I run agi debug, I see this:

Command> agi debug
AGI Debugging Enabled

from root user digits:
asterisk -rvvvvvvvvvvvvvvvvvvvvvvv
agi debug

call from the prhone 9000 and then post me the result

localhost*CLI> agi debug
AGI Debugging Enabled
    -- Starting simple switch on 'Zap/2-1'
[Nov  7 17:41:22] NOTICE[5691]: chan_zap.c:6373 ss_thread: Got event 18 (Ring Begin)...
[Nov  7 17:41:23] NOTICE[5691]: chan_zap.c:6373 ss_thread: Got event 2 (Ring/Answered)...
    -- Executing [s@DID_trunk_1:1] Answer("Zap/2-1", "") in new stack
    -- Executing [s@DID_trunk_1:2] BackGround("Zap/2-1", "thank-you-for-calling") in new stack
    -- <Zap/2-1> Playing 'thank-you-for-calling' (language 'en')
    -- Executing [s@DID_trunk_1:3] BackGround("Zap/2-1", "if-u-know-ext-dial") in new stack
    -- <Zap/2-1> Playing 'if-u-know-ext-dial' (language 'en')
  == CDR updated on Zap/2-1
    -- Executing [9000@DID_trunk_1:1] AGI("Zap/2-1", "mytest.agi") in new stack
    -- Launched AGI Script /var/lib/asterisk/agi-bin/mytest.agi
AGI Tx >> agi_request: mytest.agi
AGI Tx >> agi_channel: Zap/2-1
AGI Tx >> agi_language: en
AGI Tx >> agi_type: Zap
AGI Tx >> agi_uniqueid: 1194453678.50
AGI Tx >> agi_callerid: unknown
AGI Tx >> agi_calleridname: unknown
AGI Tx >> agi_callingpres: 0
AGI Tx >> agi_callingani2: 0
AGI Tx >> agi_callington: 0
AGI Tx >> agi_callingtns: 0
AGI Tx >> agi_dnid: unknown
AGI Tx >> agi_rdnis: unknown
AGI Tx >> agi_context: DID_trunk_1
AGI Tx >> agi_extension: 9000
AGI Tx >> agi_priority: 1
AGI Tx >> agi_enhanced: 0.0
AGI Tx >> agi_accountcode:
AGI Tx >>
AGI Rx << Usage: php [options] [-f] <file> [args...]
AGI Tx >> 510 Invalid or unknown command
AGI Rx <<        php [options] -r <code> [args...]
AGI Tx >> 510 Invalid or unknown command
AGI Rx <<        php [options] [-- args...]
AGI Tx >> 510 Invalid or unknown command
AGI Rx <<   -a               Run interactively
AGI Tx >> 510 Invalid or unknown command
AGI Rx <<   -c <path>|<file> Look for php.ini file in this directory
AGI Tx >> 510 Invalid or unknown command
AGI Rx <<   -n               No php.ini file will be used
AGI Tx >> 510 Invalid or unknown command
AGI Rx <<   -d foo[=bar]     Define INI entry foo with value 'bar'
AGI Tx >> 510 Invalid or unknown command
AGI Rx <<   -e               Generate extended information for debugger/profiler
AGI Tx >> 510 Invalid or unknown command
AGI Rx <<   -f <file>        Parse <file>.
AGI Tx >> 510 Invalid or unknown command
AGI Rx <<   -h               This help
AGI Tx >> 510 Invalid or unknown command
AGI Rx <<   -i               PHP information
AGI Tx >> 510 Invalid or unknown command
AGI Rx <<   -l               Syntax check only (lint)
AGI Tx >> 510 Invalid or unknown command
AGI Rx <<   -m               Show compiled in modules
AGI Tx >> 510 Invalid or unknown command
AGI Rx <<   -r <code>        Run PHP <code> without using script tags <?..?>
AGI Tx >> 510 Invalid or unknown command
AGI Rx <<   -s               Display colour syntax highlighted source.
AGI Tx >> 510 Invalid or unknown command
AGI Rx <<   -v               Version number
AGI Tx >> 510 Invalid or unknown command
AGI Rx <<   -w               Display source with stripped comments and whitespace.
AGI Tx >> 510 Invalid or unknown command
AGI Rx <<   -z <file>        Load Zend extension <file>.
AGI Tx >> 510 Invalid or unknown command
AGI Rx <<
AGI Tx >> 510 Invalid or unknown command
AGI Rx <<   args...          Arguments passed to script. Use -- args when first argument
AGI Tx >> 510 Invalid or unknown command
AGI Rx <<                    starts with - or script is read from stdin
AGI Tx >> 510 Invalid or unknown command
    -- AGI Script mytest.agi completed, returning 0
  == Auto fallthrough, channel 'Zap/2-1' status is 'UNKNOWN'
    -- Hungup 'Zap/2-1'
localhost*CLI>

Which editor have you used to permform the script?
This error indicate:

  1. the script is launched correctly by asterisk.
  2. the critical line is the first.

If you use an editor such as notepad, you must say that windows insert a special character like LF (line feed) or CR (carriage return).
Look: http://lists.digium.com/pipermail/asterisk-users/2005-June/105889.html

The first line must be

without blanks after “-q”

That seems to get me in the right direction thanks!
There was a whitespace after the -q…

One small step ahead now…
when running:

[code]#!/usr/bin/php -q

<?php ob_implicit_flush(true); set_time_limit('1800'); //the max life in seconds of the script //dont' forget this import require_once ('phpagi.php'); require_once ('phpagi-asmanager.php'); $agi = new AGI(); $agi->answer(); $agi->exec("SAY DIGITS 12345"); $agi->verbose("-----------ANSWER-------------"); $agi->exec("WAIT 1"); //wait 1 second $agi->hangup(); exit(); ?>[/code]

i get:

   -- Starting simple switch on 'Zap/2-1'
[Nov  7 20:11:02] NOTICE[6938]: chan_zap.c:6373 ss_thread: Got event 18 (Ring Begin)...
[Nov  7 20:11:03] NOTICE[6938]: chan_zap.c:6373 ss_thread: Got event 2 (Ring/Answered)...
    -- Executing [s@DID_trunk_1:1] Answer("Zap/2-1", "") in new stack
    -- Executing [s@DID_trunk_1:2] BackGround("Zap/2-1", "thank-you-for-calling") in new stack
    -- <Zap/2-1> Playing 'thank-you-for-calling' (language 'en')
    -- Executing [s@DID_trunk_1:3] BackGround("Zap/2-1", "if-u-know-ext-dial") in new stack
    -- <Zap/2-1> Playing 'if-u-know-ext-dial' (language 'en')
  == CDR updated on Zap/2-1
    -- Executing [9000@DID_trunk_1:1] AGI("Zap/2-1", "mytest.agi") in new stack
    -- Launched AGI Script /var/lib/asterisk/agi-bin/mytest.agi
AGI Tx >> agi_request: mytest.agi
AGI Tx >> agi_channel: Zap/2-1
AGI Tx >> agi_language: en
AGI Tx >> agi_type: Zap
AGI Tx >> agi_uniqueid: 1194462658.54
AGI Tx >> agi_callerid: unknown
AGI Tx >> agi_calleridname: unknown
AGI Tx >> agi_callingpres: 0
AGI Tx >> agi_callingani2: 0
AGI Tx >> agi_callington: 0
AGI Tx >> agi_callingtns: 0
AGI Tx >> agi_dnid: unknown
AGI Tx >> agi_rdnis: unknown
AGI Tx >> agi_context: DID_trunk_1
AGI Tx >> agi_extension: 9000
AGI Tx >> agi_priority: 1
AGI Tx >> agi_enhanced: 0.0
AGI Tx >> agi_accountcode:
AGI Tx >>
AGI Rx << ANSWER
AGI Tx >> 200 result=0
AGI Rx << EXEC SAY DIGITS 12345
    -- AGI Script Executing Application: (SAY) Options: (DIGITS)
[Nov  7 20:11:06] WARNING[6938]: res_agi.c:1108 handle_exec: Could not find application (SAY)
AGI Tx >> 200 result=-2
AGI Rx << VERBOSE "-----------ANSWER-------------" 1
  mytest.agi: -----------ANSWER-------------
AGI Tx >> 200 result=1
AGI Rx << EXEC WAIT 1
    -- AGI Script Executing Application: (WAIT) Options: (1)
AGI Tx >> 200 result=0
AGI Rx << HANGUP
AGI Tx >> 200 result=1
  == Spawn extension (DID_trunk_1, 9000, 1) exited non-zero on 'Zap/2-1'
    -- Hungup 'Zap/2-1'

Which seems to imply that the script is running, and that it just can not find the ‘say’ application…

yes, it works!
try

for the complete list of function look at:
http://www.voip-info.org/wiki/view/Asterisk+AGI

Thank you very very much…

I’ll give builing scripts that are actually usefull a try now…

Thanks again!