Skip to content

Dial status at all times? #30

@analyserdmz

Description

@analyserdmz

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions