69 lines
2.1 KiB
PHP
69 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Telegram\Handlers;
|
|
|
|
use App\Models\Channel;
|
|
use App\Telegram\TelegramBotContext;
|
|
use SergiX44\Nutgram\Nutgram;
|
|
use SergiX44\Nutgram\Telegram\Types\Keyboard\CopyTextButton;
|
|
use SergiX44\Nutgram\Telegram\Types\Keyboard\InlineKeyboardButton;
|
|
use SergiX44\Nutgram\Telegram\Types\Keyboard\InlineKeyboardMarkup;
|
|
|
|
class ChannelCommand
|
|
{
|
|
public function __construct(
|
|
private readonly TelegramBotContext $context,
|
|
) {}
|
|
|
|
public function __invoke(Nutgram $bot): void
|
|
{
|
|
$query = Channel::query()->with('user:id,username')->orderByDesc('id');
|
|
$agent = $this->context->agent();
|
|
if ($agent !== null) {
|
|
$query->where('user_id', $agent->id);
|
|
}
|
|
|
|
$channels = $query->limit(30)->get();
|
|
|
|
if ($channels->isEmpty()) {
|
|
$bot->sendMessage('暂无渠道');
|
|
|
|
return;
|
|
}
|
|
|
|
$lines = ['📎 渠道列表(点按钮复制链接)', ''];
|
|
$markup = InlineKeyboardMarkup::make();
|
|
|
|
foreach ($channels as $i => $channel) {
|
|
$remark = trim((string) ($channel->remark ?? ''));
|
|
$remarkLabel = $remark !== '' ? $remark : '—';
|
|
$owner = $channel->agentLabel();
|
|
$status = $channel->isEnabled() ? '启用' : '停用';
|
|
$lines[] = ($i + 1).'. '.$channel->channel_id;
|
|
$lines[] = ' 备注: '.$remarkLabel.' | 归属: '.$owner.' | '.$status;
|
|
|
|
$links = $channel->supportLinks();
|
|
$link = $links[0] ?? $channel->landingPath();
|
|
if (strlen($link) > 256) {
|
|
$link = substr($link, 0, 256);
|
|
}
|
|
|
|
$btnText = ($remark !== '' ? mb_substr($remark, 0, 18) : substr($channel->channel_id, 0, 8)).' · 复制链接';
|
|
$markup->addRow(InlineKeyboardButton::make(
|
|
text: $btnText,
|
|
copy_text: CopyTextButton::make($link),
|
|
));
|
|
}
|
|
|
|
if ($channels->count() >= 30) {
|
|
$lines[] = '';
|
|
$lines[] = '(仅显示最近 30 条)';
|
|
}
|
|
|
|
$bot->sendMessage(
|
|
implode("\n", $lines),
|
|
reply_markup: $markup,
|
|
);
|
|
}
|
|
}
|