Friday, August 4, 2017

Asterisk Call Originate Action Using Pami

asterisk

What is Asterisk?

Asterisk is an open source framework for building communications applications. Asterisk turns an ordinary computer into a communications server. Asterisk powers IP PBX systems, VoIP gateways, conference servers and other custom solutions. It is used by small businesses, large businesses, call centers, carriers and government agencies, worldwide. Asterisk is free and open source.

About PAMI

PAMI means PHP Asterisk Manager Interface. It is an OOP client for the Asterisk Manager Protocol, implemented in PHP.

About PAMI Features

  • TCP and TLS connections.
  • Event Driven (the framework will dispatch events to your listener).
  • Lightweight, easy of use, and useful.
  • Supports synchronous and asynchronous events (Actions with Responses, and 
  • Responses with Events associated).
  • Supports SMS via VGMS boards.

{
"require": {
"marcelog/pami": "dev-master",
"educoder/pest": "1.0.0",
"react/zmq": "0.2.*|0.3.*"
}
}
view raw composer.json hosted with ❤ by GitHub
require_once 'vendor/autoload.php';
require_once 'configlocal.php';
use PAMI\Client\Impl\ClientImpl;
use PAMI\Listener\IEventListener;
use PAMI\Message\Event\EventMessage;
use PAMI\Message\Action\OriginateAction;
use PAMI\Message\Action\StatusAction;
class PamiEventListener implements IEventListener
{
public function __construct($cli)
{
$this->cli = $cli;
}
/**
* To Handle the All Pami Events
* @param EventMessage $event
*/
public function handle(EventMessage $event)
{
echo $strevt = $event->getKeys()['event'];
$this->var_error_log($event->getKeys());
if ($strevt == 'DialBegin') {
echo "DialBegin event --- \n";
}
if ($strevt == 'DialEnd') {
echo "Dial end event --- \n";
}
if ($strevt == 'Hangup') {
echo "Hangup event --- \n";
}
}
public function var_error_log($object = null)
{
$datetime = date("Y-m-d h:i:s a");
$contents = PHP_EOL . $datetime . " :";
ob_start(); // start buffer capture
var_dump($object); // dump the values
$contents .= ob_get_contents(); // put the buffer into a variable
ob_end_clean(); // end capture
//create the log file if not exist
$log_file_path = "log.txt";
if (file_exists($log_file_path) == false) {
fopen($log_file_path, "w");
}
error_log($contents, 3, $log_file_path);
}
}
////////////////////////////////////////////////////////////////////////////////
// Code STARTS.
////////////////////////////////////////////////////////////////////////////////
error_reporting(E_ALL);
ini_set('display_errors', 1);
try {
$options = array(
'host' => 122.111.111.222,
'scheme' => 'tcp://',
'port' => 5038,
'username' => 'testuser',
'secret' => '#######',
'connect_timeout' => 10,
'read_timeout' => 10000000
);
$phonenumber=1000;
$sipNumber=20000;
$a = new ClientImpl($options);
$eventListener = new PamiEventListener($a);
$a->registerEventListener($eventListener);
$a->open();
$time = time();
while (true) {
//(time() - $time) < 60) // Wait for events.
usleep(1000); // 1ms delay
//check the Avilable Agents status
$actionid = md5(uniqid());
$response = $a->send(new StatusAction());
$originateMsg = new OriginateAction('SIP/' . $phonenumber . "@voipgw");
$originateMsg->setContext('dialer');
$originateMsg->setPriority('1');
$originateMsg->setExtension($sipNumber);
$originateMsg->setCallerId($sipNumber);
$originateMsg->setAsync(false);
$originateMsg->setActionID($actionid);
$orgresp = $a->send($originateMsg);
$orgStatus = $orgresp->getKeys()['response'];
// Since we declare(ticks=1) at the top, the following line is not necessary
$a->process();
}
$a->close(); // send logoff and close the connection.
} catch (Exception $e) {
echo $e->getMessage() . "\n";
}
view raw index.php hosted with ❤ by GitHub