Saturday, November 5, 2011

Asterisk AGI php example source code

Example for Asterisk IVR

 

 

;*****************************START context1***********************************

 

[context1]

;exten => 1234,1,Answer()

exten => 1234,1,Playback(/var/lib/asterisk/sounds/custom/folder-name/filename)

exten => 1234,n,Set(i=1)

exten => 1234,n,Set(x=1)

exten => 1234,n,Set(y=1)

exten => 1234,n,Set(z=1)

exten => 1234,n,GoTo(context2,1234,1)

 

 

;*****************************END context1***********************************

 

 

;****************START context2 (For authenticate the data provided by user)**********************

[context2]

exten => 1234,1(start),GoToIf($[${i}<3]?minloop:sorry,000,sorry)

exten => 1234,n(minloop),Read(userinput,/var/lib/asterisk/sounds/custom/folder-name/B,4)

exten => 1234,n,Set(value-captured=${userinput})

exten => 1234,n,GoToIf($[${ISNULL(${value-captured})}=1]?novaluecontext2,111,1)

exten => 1234,n,Set(UNIQUE=${UNIQUEID})

exten => 1234,n,Gosub(subthankucontext2,222,confirmation)

exten => 1234,n,GoToif($[${entered}=1]?goodcontext2,444,1)

exten => 1234,n,GoToif($[${entered}!=1]?wrongvaluecontext2,111,1)

exten => 1234,n,GoToIf($[${ISNULL(${entered})}=1]?novaluecontext2,111,1)

exten => 1234,n,Set(i=$[${i}+1])

exten => 1234,n,Goto(end-message,333,end)

exten => 1234,n,hangup()

 

 

[novaluecontext2]

exten => 111,1,Set(i=$[${i}+1])

exten => 111,n,Playback(/var/lib/asterisk/sounds/custom/folder-name/filename)

exten => 111,n,Goto(context2,1234,start)

 

 

[subthankucontext2]

exten => 222,1(confirmation),Playback(/var/lib/asterisk/sounds/custom/folder-name/filename)

exten => 222,n,SayDigits(${userinput})

exten => 222,n,Read(entered,/var/lib/asterisk/sounds/custom/folder-name/C2,1)

exten => 222,n,Return()

 

 

[goodcontext2]

exten => 444,1,Set(i=$[${i}+1])

exten => 444,n,AGI(data-verify.php,${value-captured},${UNIQUE})   //working with PHP-AGI below is the example of data-verify.php (will return the value in booleon)

exten => 444,n,Set(correct-data=${data-exists})

exten => 444,n,GoToif($[${correct-data}=0]?context2,1234,start)

exten => 444,n,Goto(next-context,1234,start)

 

[wrongvaluecontext2]

exten => 111,1,Set(i=$[${i}+1])

exten => 111,n,GoToif($[${entered}=2]?context2,1234,start)

exten => 111,n,GoToif($[${entered}!=2]?badvaluecontext2,666,1)

 

 

[badvaluecontext2]

exten => 666,1,Playback(/var/lib/asterisk/sounds/custom/folder-name/filename)

exten => 666,n,Goto(context2,1234,1)

 

 

;***************************END context2***********************************

 

 

;**************************STARTCOMMON******************************************

[sorry]

exten => 000,1(sorry),Playback(/var/lib/asterisk/sounds/custom/folder-name/filename)

exten => 000,n,Goto(end-message,333,1)

 

[end-message]

exten => 333,1(end),Playback(/var/lib/asterisk/sounds/custom/folder-name/filename)

exten => 333,n,hangup()

 

 

;**************************END COMMON******************************************

 

 

The info passed by Asterisk is:

  • agi_request - The filename of your script
  • agi_channel - The originating channel (your phone)
  • agi_language - The language code (e.g. "en")
  • agi_type - The originating channel type (e.g. "SIP" or "ZAP")
  • agi_uniqueid - A unique ID for the call
  • agi_callerid - The caller ID number (or "unknown")
  • agi_calleridname - The caller ID number (or "unknown")
  • agi_callingpres - The presentation for the callerid in a ZAP channel
  • (only for PRI Channels)
  • agi_dnid - The dialed number id
  • agi_rdnis - The referring DNIS number
  • agi_context - Origin context in extensions.conf
  • agi_extension - The called number
  • agi_priority - The priority it was executed as in the dial plan
  • agi_enhanced - The flag value is 1.0 if started as an EAGI script
  • agi_accountcode - Account code of the origin channel

 

 

example of Data-verify.php (File Permissions

Remember to make your files executable with chmod 755 /var/lib/asterisk/agi-bin/*.php .)

_________________

 

#!/usr/bin/php -q

<?php

require 'phpagi.php';

error_reporting(E_ALL);

$agi = new AGI();

ob_implicit_flush(true);

set_time_limit(6);

 

function connect_db() {

$db_connection = mysql_connect ('localhost', 'user', 'password') or die (mysql_error());

$db_select = mysql_select_db('database-name') or die (mysql_error());

}

 

// main program

$value-captured = $argv[1];

connect_db();

$result = mysql_query("SELECT  *  FROM  tablename WHERE value='$value-captured ");

$num_rows = mysql_num_rows($result);

$value_exists = $num_rows > 0 ? 1 : 0;

$agi->set_variable("valueexists", $value_exists);

 

exit;

?>

0 comments:

Post a Comment