Last active
January 29, 2025 20:30
-
-
Save shahriarhasib/714557992f610d4037364d871ba5f7eb to your computer and use it in GitHub Desktop.
Asterisk Gateway Interface (AGI) with PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Resources: | |
## Asterisk 20.6.0, PHP 8.3.6 (cli) | |
## https://phpagi.sourceforge.net/ | |
## https://github.com/welltime/phpagi | |
## Direct Download Link: https://altushost-swe.dl.sourceforge.net/project/phpagi/phpagi/2.20/phpagi-2.20.tgz?viasf=1 | |
##Download and Install PHPAGI | |
#Install PHP and Required Extensions | |
sudo apt install -y php php-cli php-pear php-xml | |
sudo apt install git | |
#Clone the Repository: | |
#Clone the PHPAGI repository from the correct URL: | |
sudo git clone https://github.com/shahriarhasib/phpagi.git /usr/share/asterisk/agi-bin/ | |
mysql -u root -p | |
CREATE DATABASE aseteriskdb; | |
CREATE USER 'asteriskuser'@'localhost' IDENTIFIED BY '7AEmMN76dADwn2#UqyY3'; | |
GRANT ALL PRIVILEGES ON aseteriskdb.* TO 'asteriskuser'@'localhost'; | |
GRANT ALL PRIVILEGES ON aseteriskdb.* TO 'phpmyadmin'@'localhost'; | |
FLUSH PRIVILEGES; | |
USE aseteriskdb; | |
CREATE TABLE asteriskdb.peers ( | |
peer VARCHAR(255) NOT NULL, | |
type VARCHAR(255), | |
host VARCHAR(255), | |
secret VARCHAR(255), | |
context VARCHAR(255), | |
callerid VARCHAR(255), | |
mobile VARCHAR(255), | |
tcpenable VARCHAR(255), | |
transport VARCHAR(255), | |
status VARCHAR(255), | |
PRIMARY KEY (peer) | |
); | |
#-- To view the data: | |
SELECT * FROM asteriskdb.peers; | |
#Insert some sample data: | |
INSERT INTO asteriskdb.peers (peer, type, host, secret, context, callerid, mobile, tcpenable, transport) | |
VALUES | |
('501', 'friend', 'dynamic', 'kndohj98dj', 'DOMESTIC', 'User 1', '01755533331', NULL, NULL), -- NULL for tcpenable and transport | |
('502', 'friend', 'dynamic', 'kndohj98dj', 'INTERCOM', 'User 2', '09609914135', NULL, NULL), -- NULL for tcpenable and transport | |
('IPTS-PROVIDER', 'peer', 'iptsp.bol-online.com', NULL, 'FROM-IPTSP', NULL, NULL, NULL, NULL), -- NULL for other fields | |
('HUDAS-SERVER', 'friend', 'dynamic', 'qZ9a4M2zrwhakvcdVNLS', 'DOMESTIC', NULL, NULL, 'yes', 'tcp,udp'), | |
('KOLLOL-SERVER', 'friend', 'dynamic', 'qZ9a4M2zrwhakvcdVNLS', 'DOMESTIC', NULL, NULL, 'yes', 'tcp,udp'), | |
('TANZIZ-SERVER', 'friend', 'dynamic', 'pKuLoKkHh3uSTbFn7mBu', NULL, NULL, NULL, NULL, NULL); -- NULL for other fields | |
cd /usr/share/asterisk/agi-bin/ | |
#Create agi script with php to search mobile number against the daialed peer number: | |
vi /var/lib/asterisk/agi-bin/searchmobile.php | |
#Permite Access/ Execution on the php files | |
chmod 755 /var/lib/asterisk/agi-bin/*.php |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[general] | |
[globals] | |
[INTERCOM] | |
exten => _5XX,1,NoOP(${CALLERID(All)} is dialing ${EXTEN} using dialplan ${CONTEXT}) | |
same => n,DIAL(SIP/${EXTEN}) | |
same => n,NoOP(Dial status: ${DIALSTATUS}) | |
; Considering the AGI file at default location and | |
; planning to call the AGI file if DIALSTATUS=CHANUNAVAIL (ref: https://www.voip-info.org/asterisk-variable-dialstatus/) | |
; Otherwise add absolute patha can be added | |
same => n,ExecIf($[${DIALSTATUS}=CHANUNAVAIL]?AGI(searchmobile.php)) | |
;same => n,NoOp(Found: Username: ${foundusername} and Mobile: ${foundmobile}) | |
same => n,NoOp(Found: Username: ${AGIvar4usr} and Mobile: ${AGIvar4mob}) | |
same => n,Hangup() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php -q | |
<?php | |
require('phpagi.php'); | |
//File Location can be anywhere, default /usr/share/asterisk/agi-bin | |
$agi = new AGI(); | |
# Get the dialed extension from the AGI arguments | |
#$dialedExtension = $argv[1]; | |
$dialedExtension = $agi->request['agi_extension']; | |
$callerID = $agi->request['agi_callerid']; | |
# Database connection parameters | |
$dbHost = 'localhost'; | |
$dbUser = 'asteriskuser'; | |
$dbPass = '7AEmMN76dADwn2#UqyY3'; | |
$dbName = 'asteriskdb'; | |
$technology = 'SIP/IPTS-PROVIDER'; | |
# Connect to the database | |
$db = new mysqli($dbHost, $dbUser, $dbPass, $dbName); | |
if ($db->connect_error) { | |
$agi->verbose("Failed to connect to the database: " . $db->connect_error); | |
exit(1); | |
} | |
# Query the 'extensions' table to get the mobile number | |
$query1 = "SELECT callerid FROM peers WHERE peer = '$callerID'"; | |
$result1 = $db->query($query1); | |
if ($result1->num_rows > 0) { | |
$row = $result1->fetch_assoc(); | |
$PHPvar4caller = $row['callerid']; | |
$agi->set_variable('AGIvar4caller', $PHPvar4caller); | |
} | |
$query2 = "SELECT callerid, mobile FROM peers WHERE peer = '$dialedExtension'"; | |
$result2 = $db->query($query2); | |
if ($result2->num_rows > 0) { | |
$row = $result2->fetch_assoc(); | |
$PHPvar4usr = $row['callerid']; | |
$PHPvar4mob = $row['mobile']; | |
# Set the 'mobile' custom variable for use in the dialplan | |
$agi->set_variable('AGIvar4usr', $PHPvar4usr); | |
$agi->set_variable('AGIvar4mob', $PHPvar4mob); | |
$agi->set_variable('AGIvar4caller', $PHPvar4caller); | |
$agi->verbose("$PHPvar4caller called $PHPvar4usr"); | |
$agi->verbose("Mobile number for peer $dialedExtension is $PHPvar4mob"); | |
$agi->verbose("Dialing $technology/$PHPvar4mob from $callerID"); | |
// We can strip the variable on need by increasing 0 | |
$actualdialcode = substr($PHPvar4mob, 0); | |
$agi->exec('Set', "CALLERID(num)=09609012009"); | |
$agi->exec('Dial', "$technology/$actualdialcode"); | |
} | |
else { | |
$agi->verbose("Extension $dialedExtension not found in the database"); | |
} | |
#$dialStatus = $agi->channel_status() | |
#$dialStatus = $agi->get_variable('DIALSTATUS'); | |
$dialStatusArray = $agi->get_variable('DIALSTATUS'); | |
// Convert the dial status array to a string | |
#$dialStatus = implode('', $dialStatusArray); | |
$dialStatus = end($dialStatusArray); | |
$query3 = "UPDATE peers SET status = '$dialStatus' WHERE peer = '$dialedExtension'"; | |
$result3 = $db->query($query3); | |
if ($result3) { | |
$agi->verbose("Dial status for extension $dialedExtension updated to $dialStatus"); | |
} else { | |
$agi->verbose("Failed to update dial status in the database: " . $db->error); | |
} | |
$db->close(); | |
?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment