File: //proc/1124/cwd/frontend/controllers/BotController.php
<?php
namespace frontend\controllers;
use Yii;
use yii\web\Controller;
use frontend\services\TelegramService;
class BotController extends Controller
{
public $enableCsrfValidation = false;
protected TelegramService $telegramService;
public function __construct($id, $module, $config = [])
{
parent::__construct($id, $module, $config);
$this->telegramService = new TelegramService();
}
/**
* Вебхук для Telegram
* @param int|null $id ID бота
*/
public function actionIndex($id = null)
{
if (!$id) {
return 'ok';
}
$rawBody = file_get_contents('php://input');
if ($rawBody === false) {
$rawBody = '';
}
$response = Yii::$app->response;
$response->statusCode = 200;
$response->content = 'ok';
$response->send();
if (function_exists('fastcgi_finish_request')) {
fastcgi_finish_request();
} else {
ignore_user_abort(true);
ob_start();
echo 'ok';
$size = ob_get_length();
header("Content-Length: $size");
header("Connection: close");
ob_end_flush();
flush();
}
try {
$this->telegramService->handleUpdate((int) $id, $rawBody !== '' ? $rawBody : null);
} catch (\Throwable $e) {
file_put_contents(
Yii::getAlias('@webroot') . '/tg_log.txt',
date('Y-m-d H:i:s') . " - Telegram webhook error: " . $e->getMessage() . "\n" . $e->getTraceAsString() . "\n",
FILE_APPEND
);
}
exit(0);
}
}