Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dial status at all times? #30

Open
analyserdmz opened this issue May 12, 2023 · 0 comments
Open

Dial status at all times? #30

analyserdmz opened this issue May 12, 2023 · 0 comments

Comments

@analyserdmz
Copy link

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();
?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant