Skip to content

Instantly share code, notes, and snippets.

@makgithub
Created August 4, 2017 13:01
Show Gist options
  • Save makgithub/28c395aae315cacc83bb7aa22bf0f2ed to your computer and use it in GitHub Desktop.
Save makgithub/28c395aae315cacc83bb7aa22bf0f2ed to your computer and use it in GitHub Desktop.
Asterisk Call Originate Action Using Pami
{
"require": {
"marcelog/pami": "dev-master",
"educoder/pest": "1.0.0",
"react/zmq": "0.2.*|0.3.*"
}
}
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";
}
@makgithub
Copy link
Author

Asterisk Call Originate Action Using Pami

@carlosb-altera
Copy link

thank you for the example!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment