use GuzzleHttp\Client;
use Yii;
+use yii_app\records\UsersTelegramMessage;
- class TelegramService {
+ class TelegramService
+ {
const TELEGRAM_API_URL = "8063257458:AAGnMf4cxwJWlYLF1wS_arn4PrOaLs9ERQQ";
return $client->request('GET', $url);
}
- public static function isDevelopmentEnvironment($urlString = null): bool
- {
- $currentUrl = empty($urlString) ? Yii::$app->request->getHostInfo() : $urlString;
- return !str_contains($currentUrl, self::TARGET_PROD_URL);
- }
+ public static function isDevelopmentEnvironment($urlString = null) : bool
+ {
+ try {
+ $ip = Yii::$app->request->getHostInfo();
+ } catch (\Exception $exception) {
+ $ip = 'console';
+ }
+
+ $currentUrl = empty($urlString) ? $ip : $urlString;
+ return !str_contains($currentUrl, self::TARGET_PROD_URL);
+ }
- public static function sendErrorToTelegramMessage($message,$disableNotification, $isDev)
- {
- $botToken = self::TELEGRAM_API_URL;
- $chatIdDefault = self::CHAT_CHANNEL_ID;
- $chatIdErp = self::CHAT_CHANNEL_ERP_ID;
- $apiURL = "https://api.telegram.org/bot{$botToken}/sendMessage";
+ public static function sendErrorToTelegramMessage($message, $disableNotification, $isDev)
+ {
+ $botToken = self::TELEGRAM_API_URL;
+ $chatIdDefault = self::CHAT_CHANNEL_ID;
+ $chatIdErp = self::CHAT_CHANNEL_ERP_ID;
+ $apiURL = "https://api.telegram.org/bot{$botToken}/sendMessage";
- if ($isDev) {
- $chatId = $chatIdDefault;
- } else {
- $chatId = $chatIdErp;
+ if ($isDev) {
+ $chatId = $chatIdDefault;
+ } else {
+ $chatId = $chatIdErp;
+ }
+
+ $client = new Client();
+ try {
+ $client->post($apiURL, [
+ 'json' => [
+ 'chat_id' => $chatId,
+ 'text' => $message,
+ 'parse_mode' => 'MarkdownV2',
+ 'disable_notification' => $disableNotification,
+ ],
+ ]);
+ } catch (\Exception $e) {
+ Yii::error("Ошибка отправки сообщения в Telegram: " . $e->getMessage(), 'telegram');
+ }
}
- $client = new Client();
- try {
- $client->post($apiURL, [
- 'json' => [
- 'chat_id' => $chatId,
- 'text' => $message,
- 'parse_mode' => 'MarkdownV2',
- 'disable_notification' => $disableNotification,
- ],
- ]);
- } catch (\Exception $e) {
- Yii::error("Ошибка отправки сообщения в Telegram: " . $e->getMessage(), 'telegram');
+ public static function sendMessageToTelegramClient($chatId, $message)
+ {
+ $botToken = self::TELEGRAM_API_URL; // Токен вашего Telegram-бота
+ $apiURL = "https://api.telegram.org/bot{$botToken}/sendMessage";
+
+ $client = new Client();
+ try {
+ $response = $client->post($apiURL, [
+ 'json' => [
+ 'chat_id' => $chatId,
+ 'text' => $message,
+ 'parse_mode' => 'MarkdownV2',
+ ],
+ ]);
+
+ if ($response->getStatusCode() !== 200) {
+ Yii::error("Ошибка при отправке сообщения в Telegram. Код ответа: "
+ . $response->getStatusCode(), 'telegram');
+ return $response->getStatusCode();
+ }
+
+ return $response->getReasonPhrase();
+ } catch (\Exception $e) {
+ Yii::error("Ошибка отправки сообщения в Telegram: " . $e->getMessage(), 'telegram');
+ return $e->getMessage();
+ }
}
- }
// Метод для экранирования символов MarkdownV2 для файла