Пытаюсь сделать телеграмм бота, установил вебхук, получил сообщение о том, что все хорошо, но телеграмм не передает данные. Получаю информацию о последнем запросе с помощью запроса api.telegram.org/botтокен/getWebhookInfo
{"ok":true,"result":{"url":"https://домен/site/webhook","has_custom_certificate":false,"pending_update_count":22,"last_error_date":1680683249,"last_error_message":"Wrong response from the webhook: 302 Found","max_connections":40,"ip_address":"ip"}}
В runtime/logs/app.log данная информация:
2023-04-05 11:17:03 [91.108.6.124][-][-][info][application] $_GET = []
$_POST = []
$_FILES = []
$_COOKIE = []
$_SERVER = [
'REDIRECT_REDIRECT_HTTPS' => 'on'
'REDIRECT_REDIRECT_downgrade-1_0' => ''
'REDIRECT_REDIRECT_suppress-error-charset' => ''
'REDIRECT_REDIRECT_STATUS' => '200'
'REDIRECT_HTTPS' => 'on'
'REDIRECT_downgrade-1_0' => ''
'REDIRECT_suppress-error-charset' => ''
'REDIRECT_STATUS' => '200'
'HTTPS' => 'on'
'downgrade-1_0' => ''
'suppress-error-charset' => ''
'HTTP_HOST' => 'домен'
'HTTP_X_REAL_IP' => ''
'HTTP_X_PROTOCOL' => 'HTTP/1.1'
'HTTP_X_SERVER_IP' => ''
'HTTP_X_FORWARDED_PROTOCOL' => 'https'
'HTTP_X_FORWARDED_PROTO' => 'https'
'HTTP_CONNECTION' => 'close'
'CONTENT_LENGTH' => '443'
'CONTENT_TYPE' => 'application/json'
'HTTP_ACCEPT_ENCODING' => 'gzip, deflate'
'PATH' => '/usr/bin:/bin:/usr/local/bin'
'SERVER_SIGNATURE' => ''
'SERVER_SOFTWARE' => 'Apache'
'SERVER_NAME' => ''
'SERVER_ADDR' => ''
'SERVER_PORT' => '80'
'REMOTE_ADDR' => ''
'DOCUMENT_ROOT' => '/home/users/o/oktyabr/domains/домен'
'REQUEST_SCHEME' => 'http'
'CONTEXT_PREFIX' => ''
'CONTEXT_DOCUMENT_ROOT' => '/home/users/o/oktyabr/domains/домен'
'SERVER_ADMIN' => 'info@jino.ru'
'SCRIPT_FILENAME' => '/home/users/o/oktyabr/domains/домен/frontend/web/index.php'
'REMOTE_PORT' => '39066'
'REDIRECT_URL' => '/frontend/web/site/webhook'
'GATEWAY_INTERFACE' => 'CGI/1.1'
'SERVER_PROTOCOL' => 'HTTP/1.0'
'REQUEST_METHOD' => 'POST'
'QUERY_STRING' => ''
'REQUEST_URI' => '/site/webhook'
'SCRIPT_NAME' => '/frontend/web/index.php'
'PHP_SELF' => '/frontend/web/index.php'
'REQUEST_TIME_FLOAT' => 1680682623.7914
'REQUEST_TIME' => 1680682623
]
Я так понимаю из-за того, что идет переадресация 'REDIRECT_URL' => '/frontend/web/site/webhook' получаю 302 ошибку. Как можно настроить иначе, чтобы не было переадресации?
Код проекта:
.htaccess
Options +FollowSymLinks
IndexIgnore /
RewriteEngine on
# Если запрос начинается с /admin, то заменяем на /backend/web/
RewriteCond %{REQUEST_URI} ^/admin
RewriteRule ^admin\/?(.*) /backend/web/$1
# Добавляем другой запрос /frontend/web/$1
RewriteCond %{REQUEST_URI} !^/(frontend/web|backend/web|admin)
RewriteRule (.*) /frontend/web/$1
# Если frontend запрос
RewriteCond %{REQUEST_URI} ^/frontend/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /frontend/web/index.php
# Если backend запрос
RewriteCond %{REQUEST_URI} ^/backend/web
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /backend/web/index.php
В контроллере SiteController
public function actionSetWebhook()
{
$botToken = Yii::$app->params['telegram'];
$webhookUrl = Yii::$app->urlManager->createAbsoluteUrl(['site/webhook']);
$telegramApiUrl = "https://api.telegram.org/bot{$botToken}";
$setWebhookUrl = "{$telegramApiUrl}/setWebhook?url={$webhookUrl}";
$curl = curl_init($setWebhookUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
// Проверяем ответ Telegram API
$jsonResponse = json_decode($response, true);
print_r($jsonResponse);
if ($jsonResponse['ok']) {
echo 'Webhook установлен';
} else {
echo 'Ошибка при установке webhook: ' . $jsonResponse['description'];
}
}
private function sendMessage($chatId, $message)
{
$botToken = Yii::$app->params['telegram'];
$telegramApiUrl = "https://api.telegram.org/bot{$botToken}";
$data = [
'chat_id' => $chatId,
'text' => $message,
];
$curl = curl_init($telegramApiUrl . '/sendMessage');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_exec($curl);
curl_close($curl);
}
public function actionWebhook()
{
$data = file_get_contents('php://input');
$data = json_decode($data, true);
file_put_contents( '/home/users/o/аккаунт/domains/домен/frontend/web/message2.txt', print_r($data, true));
}