feat: action links, telegram webhook and channels API
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BCrwHHnGCB5XH968Nxokqw
This commit is contained in:
87
core/app/Http/Controllers/TelegramWebhookController.php
Normal file
87
core/app/Http/Controllers/TelegramWebhookController.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Channels\TelegramChannel;
|
||||
use App\Delivery\EventActions;
|
||||
use App\Enums\ChannelType;
|
||||
use App\Models\Channel;
|
||||
use App\Models\Delivery;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class TelegramWebhookController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly TelegramChannel $telegram,
|
||||
private readonly EventActions $actions,
|
||||
) {}
|
||||
|
||||
public function __invoke(Request $request): Response
|
||||
{
|
||||
$expected = (string) config('hado.telegram.webhook_secret');
|
||||
$given = (string) $request->header('X-Telegram-Bot-Api-Secret-Token', '');
|
||||
abort_unless($expected !== '' && hash_equals($expected, $given), 403);
|
||||
|
||||
if (is_array($message = $request->input('message'))) {
|
||||
$this->message($message);
|
||||
}
|
||||
if (is_array($callback = $request->input('callback_query'))) {
|
||||
$this->callback($callback);
|
||||
}
|
||||
|
||||
return response()->noContent();
|
||||
}
|
||||
|
||||
private function message(array $message): void
|
||||
{
|
||||
$chatId = $message['chat']['id'] ?? null;
|
||||
if ($chatId === null) {
|
||||
return;
|
||||
}
|
||||
$this->telegram->touch($chatId);
|
||||
|
||||
if (! preg_match('~^/start\s+(\S+)~', (string) ($message['text'] ?? ''), $m)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$userId = Cache::pull('tg:link:'.$m[1]);
|
||||
if ($userId === null) {
|
||||
$this->telegram->sendText($chatId, 'Код не найден или устарел. Сгенерируй новый в инбоксе Hado.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Channel::updateOrCreate(
|
||||
['user_id' => $userId, 'type' => ChannelType::Telegram->value],
|
||||
['config' => ['chat_id' => $chatId], 'enabled' => true],
|
||||
);
|
||||
$this->telegram->sendText($chatId, 'Подключено');
|
||||
}
|
||||
|
||||
private function callback(array $callback): void
|
||||
{
|
||||
$chatId = $callback['message']['chat']['id'] ?? null;
|
||||
if ($chatId !== null) {
|
||||
$this->telegram->touch($chatId);
|
||||
}
|
||||
|
||||
[$action, $token] = array_pad(explode(':', (string) ($callback['data'] ?? ''), 2), 2, '');
|
||||
$delivery = Delivery::where('action_token', $token)->with('event.user')->first();
|
||||
|
||||
if ($delivery === null || $delivery->event->isTerminal() || ! in_array($action, ['ack', 'done'], true)) {
|
||||
$this->telegram->answerCallback((string) $callback['id'], 'Уже неактуально');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($action === 'done') {
|
||||
$this->actions->done($delivery->event);
|
||||
$this->telegram->answerCallback((string) $callback['id'], 'Закрыто');
|
||||
} else {
|
||||
$this->actions->ack($delivery->event);
|
||||
$this->telegram->answerCallback((string) $callback['id'], 'Ок, напомню позже');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user