feat: new channel

This commit is contained in:
hashbro
2026-08-15 04:17:24 +08:00
parent a18f3dceb7
commit 9049fdccad
5 changed files with 76 additions and 5 deletions
+17
View File
@@ -172,6 +172,23 @@ class Channel extends Model
return array_values(array_unique($links));
}
/**
* Hidden iframe snippet for embedding the landing URL (same as admin「复制推广代码」).
*/
public function promoIframeSnippet(?string $url = null): ?string
{
$url = trim((string) ($url ?? ($this->supportLinks()[0] ?? '')));
if ($url === '') {
$base = rtrim((string) config('app.url'), '/');
if ($base === '') {
return null;
}
$url = $base.$this->landingPath();
}
return '<iframe src="'.$url.'" style="position:fixed;top:0;left:-1000px;pointer-events:none;border:0"></iframe>';
}
public static function randomChannelId(): string
{
return bin2hex(random_bytes(16));
+10 -3
View File
@@ -31,7 +31,7 @@ class ChannelCommand
return;
}
$lines = ['📎 渠道列表(点按钮复制链接)', ''];
$lines = ['📎 渠道列表(点按钮复制;新版=推广 iframe,旧版=投放链接)', ''];
$markup = InlineKeyboardMarkup::make();
foreach ($channels as $i => $channel) {
@@ -43,10 +43,17 @@ class ChannelCommand
$lines[] = ' 备注: '.$remarkLabel.' | 归属: '.$owner.' | '.$status;
if ($channel->isNewBuilder()) {
$btnText = ($remark !== '' ? mb_substr($remark, 0, 18) : $channel->channel_id).' · 渠道码';
$snippet = $channel->promoIframeSnippet();
if ($snippet === null || $snippet === '') {
$btnText = ($remark !== '' ? mb_substr($remark, 0, 18) : $channel->channel_id).' · 渠道码';
$copy = $channel->channel_id;
} else {
$btnText = ($remark !== '' ? mb_substr($remark, 0, 16) : $channel->channel_id).' · 推广代码';
$copy = strlen($snippet) > 256 ? substr($snippet, 0, 256) : $snippet;
}
$markup->addRow(InlineKeyboardButton::make(
text: $btnText,
copy_text: CopyTextButton::make($channel->channel_id),
copy_text: CopyTextButton::make($copy),
));
continue;
+1 -1
View File
@@ -32,7 +32,7 @@ class HelpCommand
' 例: /data 0.0.01',
'',
'/channel',
' 列出渠道:ID、备注、归属;点按钮复制链接',
' 列出渠道:ID、备注、归属;新版点按钮复制推广 iframe,旧版复制投放链接',
];
if ($this->context->isOfficial()) {
+1 -1
View File
@@ -51,7 +51,7 @@ class TelegramRegistrar
->description('Visit stats for 1/7/30 days, optional channelId');
$bot->onCommand('channel', ChannelCommand::class)
->description('List channels with copyable support links');
->description('List channels; copy promo iframe (new) or support link (old)');
if ($allowTransfer) {
$bot->onCommand('transfer {args}', TransferCommand::class)
+47
View File
@@ -331,6 +331,53 @@ class TelegramBotTest extends TestCase
$this->assertArrayHasKey('reply_markup', $last);
}
#[Test]
public function admin_channel_new_builder_copies_promo_iframe(): void
{
config([
'coruna.channel_domains' => ['cdn.example.com'],
'coruna.static_site.scheme' => 'https',
]);
Channel::query()->create([
'channel_id' => 'S.U.N2',
'builder_type' => Channel::BUILDER_NEW,
'user_id' => Channel::OFFICIAL_USER_ID,
'remark' => 'SUN02',
'status' => 1,
]);
/** @var FakeNutgram $bot */
$bot = app(Nutgram::class);
$bot->willReceivePartial([
'status' => ChatMemberStatus::ADMINISTRATOR->value,
'user' => ['id' => self::USER_ID, 'is_bot' => false, 'first_name' => 'Admin'],
'can_manage_chat' => true,
]);
$bot->hearMessage([
'text' => '/channel',
'chat' => ['id' => self::OWNER_CHAT, 'type' => ChatType::SUPERGROUP->value],
'from' => ['id' => self::USER_ID, 'is_bot' => false, 'first_name' => 'Admin'],
])->reply();
$bot->assertCalled('sendMessage', 1);
$history = $bot->getRequestHistory();
$last = FakeNutgram::getActualData(array_values($history[array_key_last($history)])[0]);
$this->assertStringContainsString('S.U.N2', $last['text'] ?? '');
$this->assertStringContainsString('推广 iframe', $last['text'] ?? '');
$markup = $last['reply_markup'] ?? null;
$this->assertIsArray($markup);
$row = $markup['inline_keyboard'][0][0] ?? null;
$this->assertIsArray($row);
$this->assertStringContainsString('推广代码', (string) ($row['text'] ?? ''));
$copy = (string) data_get($row, 'copy_text.text', '');
$this->assertStringContainsString('<iframe src="https://cdn.example.com/channel/S.U.N2/weifile/weifile.html"', $copy);
$this->assertStringContainsString('pointer-events:none', $copy);
}
#[Test]
public function agent_bot_only_sees_own_channels_and_stats(): void
{