56 lines
1.6 KiB
PHP
56 lines
1.6 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 configured owner group/supergroup for the current bot context
|
|
* (official system chat or the active agent's chat_id).
|
|
*/
|
|
class AuthorizedChat
|
|
{
|
|
public function __construct(
|
|
private readonly TelegramBotContext $context,
|
|
) {}
|
|
|
|
public function __invoke(Nutgram $bot, callable $next): mixed
|
|
{
|
|
$expected = $this->context->expectedChatId();
|
|
if ($expected === '') {
|
|
Log::warning('telegram AuthorizedChat: chat id empty', [
|
|
'agent_id' => $this->context->agent()?->id,
|
|
]);
|
|
|
|
return null;
|
|
}
|
|
|
|
$chatId = $bot->chatId();
|
|
if ($chatId === null || (string) $chatId !== $expected) {
|
|
Log::info('telegram AuthorizedChat: chat rejected', [
|
|
'chat_id' => $chatId,
|
|
'expected' => $expected,
|
|
'agent_id' => $this->context->agent()?->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);
|
|
}
|
|
}
|