File: //proc/1124/cwd/frontend/controllers/NewBotController.php
<?php
namespace frontend\controllers;
use Yii;
use yii\web\Controller;
use frontend\services\TelegramService;
class BotController extends Controller
{
public $enableCsrfValidation = false; // для вебхуков CSRF не нужен
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'; // защита от случайного захода
}
try {
// Принимаем и обрабатываем сообщение
$this->telegramService->handleUpdate($id);
} catch (\Throwable $e) {
// Логируем ошибку
file_put_contents(Yii::getAlias('@web_path') . '/tg_log.txt', date('Y-m-d H:i:s') . " - " ."Telegram webhook error: " . $e->getMessage() . "\n" . $e->getTraceAsString(), FILE_APPEND);
}
return 'ok';
}
}