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'], 'Ок, напомню позже'); } } }