feat: web, webhook and telegram channel drivers

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BCrwHHnGCB5XH968Nxokqw
This commit is contained in:
nikita.hohlov
2026-09-04 07:18:37 -03:00
parent 1b03fe0796
commit 1b0a559e3c
11 changed files with 487 additions and 0 deletions

View File

@@ -0,0 +1,100 @@
<?php
namespace App\Channels;
use App\Enums\Presence;
use App\Models\Delivery;
use App\Models\Event;
use App\Models\User;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Throwable;
final class TelegramChannel implements ChannelDriver
{
public function __construct(
private readonly string $botToken,
private readonly int $seenTtl,
) {}
public function presence(User $user, array $config): Presence
{
// Бот не видит онлайн-статус: present только если недавно жал кнопку или писал.
return Cache::has($this->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}";
}
}