|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Worker; |
| 4 | + |
| 5 | +use Ivoz\Core\Infrastructure\Persistence\Redis\RedisMasterFactory; |
| 6 | +use Ivoz\Provider\Domain\Job\WebhookJobInterface; |
| 7 | +use Ivoz\Provider\Domain\Model\Webhook\Payload\WebhookEventPayload; |
| 8 | +use Psr\Log\LoggerInterface; |
| 9 | +use Symfony\Component\HttpFoundation\Response; |
| 10 | + |
| 11 | +class RealtimeWebhooks |
| 12 | +{ |
| 13 | + private const REDIS_REALTIME_DB = 1; |
| 14 | + private const CHANNEL_PATTERN = 'users:*'; |
| 15 | + |
| 16 | + private const EVENT_MAP = [ |
| 17 | + 'Trying' => 'start', |
| 18 | + 'Proceeding' => 'ring', |
| 19 | + 'Early' => 'ring', |
| 20 | + 'Confirmed' => 'answer', |
| 21 | + 'Terminated' => 'end', |
| 22 | + ]; |
| 23 | + |
| 24 | + /** @var array<string, array{Party: ?string, Direction: ?string, Owner: ?string}> */ |
| 25 | + private array $callCache = []; |
| 26 | + |
| 27 | + public function __construct( |
| 28 | + private RedisMasterFactory $redisMasterFactory, |
| 29 | + private WebhookJobInterface $webhookJob, |
| 30 | + private LoggerInterface $logger, |
| 31 | + ) { |
| 32 | + } |
| 33 | + |
| 34 | + public function dispatch(): Response |
| 35 | + { |
| 36 | + $redis = $this->redisMasterFactory->create(self::REDIS_REALTIME_DB); |
| 37 | + |
| 38 | + $redis->pSubscribe( |
| 39 | + [self::CHANNEL_PATTERN], |
| 40 | + function ($redis, $pattern, $channel, $message): void { |
| 41 | + try { |
| 42 | + $this->processMessage($channel, $message); |
| 43 | + } catch (\Throwable $e) { |
| 44 | + $this->logger->error( |
| 45 | + '[RT-WEBHOOK] Error processing message on ' . $channel . ': ' . $e->getMessage() |
| 46 | + ); |
| 47 | + } |
| 48 | + } |
| 49 | + ); |
| 50 | + |
| 51 | + return new Response('', 500); |
| 52 | + } |
| 53 | + |
| 54 | + private function processMessage(string $channel, string $message): void |
| 55 | + { |
| 56 | + if (!preg_match('/^users:b(\d+):c(\d+):/', $channel, $matches)) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + $brandId = (int) $matches[1]; |
| 61 | + $companyId = (int) $matches[2]; |
| 62 | + |
| 63 | + /** @var array<string, mixed>|null $data */ |
| 64 | + $data = json_decode($message, true); |
| 65 | + if (!is_array($data) || !isset($data['Event'])) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + $kamEvent = (string) $data['Event']; |
| 70 | + $callId = isset($data['Call-ID']) ? (string) $data['Call-ID'] : null; |
| 71 | + |
| 72 | + if ($kamEvent === 'UpdateCLID') { |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + $webhookEvent = self::EVENT_MAP[$kamEvent] ?? null; |
| 77 | + if ($webhookEvent === null) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + if ($kamEvent === 'Trying' && $callId !== null) { |
| 82 | + $this->callCache[$callId] = [ |
| 83 | + 'Party' => isset($data['Party']) ? (string) $data['Party'] : null, |
| 84 | + 'Direction' => isset($data['Direction']) ? (string) $data['Direction'] : null, |
| 85 | + 'Owner' => isset($data['Owner']) ? (string) $data['Owner'] : null, |
| 86 | + ]; |
| 87 | + } |
| 88 | + |
| 89 | + $cached = ($callId !== null && isset($this->callCache[$callId])) |
| 90 | + ? $this->callCache[$callId] |
| 91 | + : ['Party' => null, 'Direction' => null, 'Owner' => null]; |
| 92 | + |
| 93 | + $party = isset($data['Party']) ? (string) $data['Party'] : $cached['Party']; |
| 94 | + $direction = $cached['Direction']; |
| 95 | + |
| 96 | + $payload = new WebhookEventPayload( |
| 97 | + event: $webhookEvent, |
| 98 | + brandId: $brandId, |
| 99 | + companyId: $companyId, |
| 100 | + ddiId: null, |
| 101 | + ddiE164: null, |
| 102 | + callId: $callId, |
| 103 | + uniqueId: isset($data['ID']) ? (string) $data['ID'] : null, |
| 104 | + caller: ($direction === 'inbound') ? $party : null, |
| 105 | + callee: ($direction === 'outbound') ? $party : null, |
| 106 | + dialStatus: null, |
| 107 | + timestamp: isset($data['Time']) |
| 108 | + ? date('Y-m-d\TH:i:s.v\Z', (int) $data['Time']) |
| 109 | + : null, |
| 110 | + ); |
| 111 | + |
| 112 | + $this->webhookJob |
| 113 | + ->setData($payload) |
| 114 | + ->send(); |
| 115 | + |
| 116 | + if ($kamEvent === 'Terminated' && $callId !== null) { |
| 117 | + unset($this->callCache[$callId]); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments