From d49db3467d6096e084f7f8fe95cb81edf055d75b Mon Sep 17 00:00:00 2001 From: fomichev Date: Wed, 30 Oct 2024 12:26:32 +0300 Subject: [PATCH] =?utf8?q?=D0=97=D0=B0=D0=BC=D0=B5=D0=BD=D0=B0=20=D0=BC?= =?utf8?q?=D0=B5=D1=82=D0=BE=D0=B4=D0=B0=20=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD?= =?utf8?q?=D0=B8=D1=8F=20=D0=B0=D0=B4=D1=80=D0=B5=D1=81=D0=B0=20=D0=B2?= =?utf8?q?=D0=B5=D0=B1=20=D1=85=D1=83=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- erp24/api2/controllers/TelegramController.php | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/erp24/api2/controllers/TelegramController.php b/erp24/api2/controllers/TelegramController.php index 70ce9705..53056d74 100644 --- a/erp24/api2/controllers/TelegramController.php +++ b/erp24/api2/controllers/TelegramController.php @@ -40,14 +40,42 @@ class TelegramController extends Controller return parent::beforeAction($action); } + public function actionSetWebhook($on = '1') + { + $client = new Client(); + $url = 'https://api.telegram.org/bot' . Yii::$app->params['TELEGRAM_BOT_TOKEN'] . '/setWebhook'; + $webhookUrl = $on == '1' ? Yii::$app->params['TELEGRAM_WEBHOOK_URL'] : ''; + + try { + $response = $client->post($url, [ + 'json' => [ + 'url' => $webhookUrl + ] + ]); + + // Проверяем, успешен ли ответ + $responseBody = json_decode($response->getBody()->getContents(), true); + if ($responseBody && isset($responseBody['ok']) && $responseBody['ok']) { + Yii::info("Webhook успешно установлен: {$webhookUrl}", 'telegram'); + return "OK"; + } else { + Yii::error("Ошибка установки вебхука: " . json_encode($responseBody), 'telegram'); + return "NOT OK"; + } + } catch (\Exception $e) { + Yii::error("Ошибка установки вебхука: " . $e->getMessage(), 'telegram'); + return "NOT OK"; + } + } + /*public function actionSetWebhook($on = '1') { return self::apiRequest('setWebhook', [ 'url' => ( $on == '1' ? Yii::$app->params['TELEGRAM_WEBHOOK_URL'] : '' ) ]) ? "OK" : "NOT OK"; - } + }*/ // public function actionInfo() { // self::apiRequestJson("sendMessage", ['chat_id' => "5489795686", "parse_mode" => "html", "text" => 'Hello', -- 2.39.5