<?php
require __DIR__ . '/vendor/autoload.php';

use Twilio\Jwt\AccessToken;
use Twilio\Jwt\Grants\VoiceGrant;

$accountSid = 'ACxxxxxxxxxxxxxxxxxxxx';
$apiKey = 'SKxxxxxxxxxxxxxxxxxxxx';
$apiSecret = 'xxxxxxxxxxxxxxxxxxxx';
$twimlAppSid = 'APxxxxxxxxxxxxxxxxxxxx';

$identity = $_GET['identity'] ?? 'agent_100';

$voiceGrant = new VoiceGrant();
$voiceGrant->setOutgoingApplicationSid($twimlAppSid);
$voiceGrant->setIncomingAllow(true);

$token = new AccessToken(
    $accountSid,
    $apiKey,
    $apiSecret,
    3600,
    $identity
);
$token->addGrant($voiceGrant);

header('Content-Type: application/json');
echo json_encode(['token' => $token->toJWT()]);
