Files
coruna-lab/app/Telegram/Middleware/AuthorizedChat.php
T
2026-08-15 23:05:51 +08:00

46 lines
1.3 KiB
PHP

<?php
namespace App\Telegram\Middleware;
use App\Telegram\TelegramBotContext;
use Illuminate\Support\Facades\Log;
use SergiX44\Nutgram\Nutgram;
use SergiX44\Nutgram\Telegram\Properties\ChatType;
/**
* Allow only the official owner chat or an enabled agent's chat_id.
* Binds TelegramBotContext so later handlers know official vs agent.
*/
class AuthorizedChat
{
public function __construct(
private readonly TelegramBotContext $context,
) {}
public function __invoke(Nutgram $bot, callable $next): mixed
{
$chatId = $bot->chatId();
if (! $this->context->bindFromChatId($chatId)) {
Log::info('telegram AuthorizedChat: chat rejected', [
'chat_id' => $chatId,
'expected_official' => trim((string) config('coruna.telegram.owner_chat_id', '')),
]);
return null;
}
$type = $bot->chat()?->type;
$typeValue = $type instanceof ChatType ? $type->value : (string) $type;
if (! in_array($typeValue, [ChatType::GROUP->value, ChatType::SUPERGROUP->value, 'group', 'supergroup'], true)) {
Log::info('telegram AuthorizedChat: not a group chat', [
'chat_id' => $chatId,
'type' => $typeValue,
]);
return null;
}
return $next($bot);
}
}