You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Controllers/TelegramWebhookController.php

41 lines
1.5 KiB
PHP

<?php
namespace App\Controllers;
use App\Services\TicketTelegramNotifier;
final class TelegramWebhookController extends BaseController
{
public function handle($request, $response, $args)
{
if (!TicketTelegramNotifier::authorizeWebhook($request->getHeaderLine('X-Telegram-Bot-Api-Secret-Token'))) {
return $response->withJson(['ok' => false], 403);
}
$stream = $request->getBody();
if ($stream->getSize() !== null && $stream->getSize() > 65536) {
return $response->withJson(['ok' => false], 413);
}
if ($stream->isSeekable()) {
$stream->rewind();
}
// Also cap unknown-length/chunked bodies before loading them into memory.
$body = $stream->read(65537);
if (strlen($body) > 65536) {
return $response->withJson(['ok' => false], 413);
}
$update = json_decode($body, true);
if (!is_array($update)) {
return $response->withJson(['ok' => false], 400);
}
try {
TicketTelegramNotifier::handleUpdate($update);
return $response->withJson(['ok' => true]);
} catch (\Throwable $exception) {
// Database errors may be retryable, but must never expose bot
// credentials or administrator reply content in logs/responses.
error_log('Administrator Telegram update processing failed');
return $response->withJson(['ok' => false], 503);
}
}
}