/
itp_practice
/
itp_backend
Обзор
Документация
Войти
/
itp_practice
/
itp_backend
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
master
app/Services/TelegramService.php
125 строк
4 KB
vivanenko
add auth
21 май 2025, 16:49
21 май 2025, 16:49
408bda1
Код
Авторство
О чём код?
<?php namespace App\Services; use App\Contracts\Telegram\TelegramBotService; use Illuminate\Support\Facades\Http; use RuntimeException; class TelegramService implements TelegramBotService { private string $botToken; private string $botUsername; private string $apiBaseUrl; public function __construct() { $this->botToken = config('services.telegram.bot_token'); $this->botUsername = config('services.telegram.bot_username'); $this->apiBaseUrl = "https://api.telegram.org/bot{$this->botToken}"; } public function validateTelegramAuthorization(array $data): bool { $checkHash = $data['hash']; unset($data['hash']); $dataCheckArr = []; foreach ($data as $key => $value) { $dataCheckArr[] = $key . '=' . $value; } sort($dataCheckArr); $dataCheckString = implode("\n", $dataCheckArr); $secretKey = hash('sha256', $this->botToken, true); $hash = hash_hmac('sha256', $dataCheckString, $secretKey); if (strcmp($hash, $checkHash) !== 0) { return false; } if ((time() - $data['auth_date']) > 86400) { return false; } return true; } public function sendMessage(string $chatId, string $message, ?array $replyMarkup = null): void { $data = [ 'chat_id' => $chatId, 'text' => $message, 'parse_mode' => 'HTML', ]; if ($replyMarkup) { $data['reply_markup'] = json_encode($replyMarkup); } $response = Http::post("{$this->apiBaseUrl}/sendMessage", $data); if (!$response->successful()) { throw new RuntimeException('Failed to send Telegram message: ' . $response->body()); } } public function sendInlineKeyboard(string $chatId, string $message, array $buttons): void { $keyboard = [ 'inline_keyboard' => $buttons ]; $this->sendMessage($chatId, $message, $keyboard); } public function answerCallbackQuery(string $callbackQueryId, ?string $text = null, bool $showAlert = false): void { $data = [ 'callback_query_id' => $callbackQueryId, 'show_alert' => $showAlert, ]; if ($text) { $data['text'] = $text; } $response = Http::post("{$this->apiBaseUrl}/answerCallbackQuery", $data); if (!$response->successful()) { throw new RuntimeException('Failed to answer callback query: ' . $response->body()); } } public function deleteMessage(string $chatId, int $messageId): void { $response = Http::post("{$this->apiBaseUrl}/deleteMessage", [ 'chat_id' => $chatId, 'message_id' => $messageId, ]); if (!$response->successful()) { throw new RuntimeException('Failed to delete message: ' . $response->body()); } } public function editMessageText(string $chatId, int $messageId, string $text, ?array $replyMarkup = null): void { $data = [ 'chat_id' => $chatId, 'message_id' => $messageId, 'text' => $text, 'parse_mode' => 'HTML', ]; if ($replyMarkup) { $data['reply_markup'] = json_encode($replyMarkup); } $response = Http::post("{$this->apiBaseUrl}/editMessageText", $data); if (!$response->successful()) { throw new RuntimeException('Failed to edit message: ' . $response->body()); } } }