chan-sccp / PAMI

Fork of PHP Asterisk Manager Interface ( AMI ) supports synchronous command ( action )/ responses and asynchronous events using the pattern observer-listener. Supports commands with responses with multiple events. Very suitable for development of operator consoles and / or asterisk / channels / peers monitoring through SOA, etc
http://marcelog.github.com/PAMI
Apache License 2.0
27 stars 21 forks source link

Dial status at all times? #30

Open analyserdmz opened 1 year ago

analyserdmz commented 1 year ago

Hello, thank you for keeping PAMI alive!

I am trying to get every status of the current call. Meaning, to print each and every status that the call is in (Ringing, Answered, Busy, etc). The code I have so far (and it kind of works nicely) is this:

<?php
require_once '/root/PHPUnmask/vendor/autoload.php';

use PAMI\Client\Impl\ClientImpl;
use PAMI\Message\Action\OriginateAction;
use PAMI\Message\Event\DialEvent;
use PAMI\Message\Event\HangupEvent;

$options = array(
    'host' => 'localhost',
    'scheme' => 'tcp://',
    'port' => 5038,
    'username' => 'USERNAME_HERE',
    'secret' => 'PASSWORD_HERE',
    'connect_timeout' => 10,
    'read_timeout' => 10
);

$client = new ClientImpl($options);

$callInProgress = true;
$callAnswered = false;

$client->registerEventListener(
    function ($event) use (&$callInProgress, &$callAnswered) {
        if ($event instanceof DialEvent) {
            echo 'Dial Status: ' . $event->getDialStatus() . PHP_EOL;

            if ($event->getDialStatus() === 'ANSWER') {
                echo "Call answered.\n";
                $callAnswered = true;
            } elseif ($event->getDialStatus() === 'BUSY') {
                echo "Call busy.\n";
                $callInProgress = false;
            }
        } elseif ($event instanceof HangupEvent) {
            echo 'Call has ended.' . PHP_EOL;
            $callInProgress = false;
        }
    }
);

$client->open();

$originateMsg = new OriginateAction('PJSIP/NUMBER_TO_CALL_HERE@MY_OUTBOUND_ROUTE_HERE');
$originateMsg->setContext('from-pbx');
$originateMsg->setExtension('beep');
$originateMsg->setPriority('1');

$response = $client->send($originateMsg);

echo "Script started." . PHP_EOL;

if ($response->isSuccess()) {
    echo "Call successfully initiated." . PHP_EOL;
} else {
    echo "Call initiation failed." . PHP_EOL;
    $callInProgress = false;
}

// Wait for the call to be answered
while ($callInProgress && !$callAnswered) {
    $client->process();
}

// Wait for the call to end
while ($callInProgress) {
    $client->process();
}

echo "Script ended." . PHP_EOL;

$client->close();
?>