seenKey($config['chat_id'])) ? Presence::Present : Presence::Unknown; } public function deliver(Event $event, User $user, array $config, Delivery $delivery): DeliveryOutcome { $text = $event->payload['title']; if (! empty($event->payload['subtitle'])) { $text .= "\n".$event->payload['subtitle']; } try { $response = $this->call('sendMessage', [ 'chat_id' => $config['chat_id'], 'text' => $text, 'reply_markup' => ['inline_keyboard' => [[ ['text' => 'Помню', 'callback_data' => 'ack:'.$delivery->action_token], ['text' => $event->payload['done_label'] ?? 'Сделано', 'callback_data' => 'done:'.$delivery->action_token], ]]], ]); } catch (Throwable $e) { return DeliveryOutcome::failed($e->getMessage()); } if (! $response->successful() || $response->json('ok') !== true) { return DeliveryOutcome::failed('Telegram: '.($response->json('description') ?? "HTTP {$response->status()}")); } return DeliveryOutcome::ok(['message_id' => $response->json('result.message_id')]); } public function onDone(Event $event, Delivery $delivery): void { $messageId = $delivery->meta['message_id'] ?? null; $chatId = $delivery->channel?->config['chat_id'] ?? null; if ($messageId === null || $chatId === null) { return; } try { $this->call('editMessageReplyMarkup', ['chat_id' => $chatId, 'message_id' => $messageId, 'reply_markup' => ['inline_keyboard' => []]]); } catch (Throwable) { // best effort } } public function touch(int|string $chatId): void { Cache::put($this->seenKey($chatId), true, $this->seenTtl); } public function sendText(int|string $chatId, string $text): void { try { $this->call('sendMessage', ['chat_id' => $chatId, 'text' => $text]); } catch (Throwable) { // best effort } } public function answerCallback(string $callbackId, string $text): void { try { $this->call('answerCallbackQuery', ['callback_query_id' => $callbackId, 'text' => $text]); } catch (Throwable) { // best effort } } private function call(string $method, array $params): Response { return Http::timeout(5)->asJson()->post("https://api.telegram.org/bot{$this->botToken}/{$method}", $params); } private function seenKey(int|string $chatId): string { return "tg:seen:{$chatId}"; } }