feat: bot
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Telegram\Handlers;
|
||||
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use SergiX44\Nutgram\Nutgram;
|
||||
use SergiX44\Nutgram\Telegram\Properties\ChatMemberStatus;
|
||||
use SergiX44\Nutgram\Telegram\Properties\ChatType;
|
||||
use SergiX44\Nutgram\Telegram\Properties\ParseMode;
|
||||
use SergiX44\Nutgram\Telegram\Types\Keyboard\CopyTextButton;
|
||||
use SergiX44\Nutgram\Telegram\Types\Keyboard\InlineKeyboardButton;
|
||||
use SergiX44\Nutgram\Telegram\Types\Keyboard\InlineKeyboardMarkup;
|
||||
|
||||
class JoinedChatHandler
|
||||
{
|
||||
private const IN_GROUP = ['member', 'administrator', 'creator', 'restricted'];
|
||||
|
||||
private const OUT_GROUP = ['left', 'kicked'];
|
||||
|
||||
public function __invoke(Nutgram $bot): void
|
||||
{
|
||||
$update = $bot->chatMember();
|
||||
if ($update === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$type = $update->chat->type ?? null;
|
||||
$typeValue = $type instanceof ChatType ? $type->value : (string) $type;
|
||||
if (! in_array($typeValue, [ChatType::GROUP->value, ChatType::SUPERGROUP->value, 'group', 'supergroup'], true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$old = $this->statusValue($update->old_chat_member->status ?? null);
|
||||
$new = $this->statusValue($update->new_chat_member->status ?? null);
|
||||
if (! in_array($old, self::OUT_GROUP, true) || ! in_array($new, self::IN_GROUP, true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$chatId = (string) $update->chat->id;
|
||||
$title = trim((string) ($update->chat->title ?? ''));
|
||||
$lines = ['✅ Bot 已加入本群'];
|
||||
if ($title !== '') {
|
||||
$lines[] = '🏷 群名: '.$this->e($title);
|
||||
}
|
||||
$lines[] = '🆔 Chat ID: <code>'.$this->e($chatId).'</code>';
|
||||
$lines[] = '';
|
||||
$lines[] = '把上面的 Chat ID 填到后台:';
|
||||
$lines[] = '• 官方通知 → 系统设置';
|
||||
$lines[] = '• 代理单独推送 → 代理用户';
|
||||
|
||||
try {
|
||||
$bot->sendMessage(
|
||||
implode("\n", $lines),
|
||||
parse_mode: ParseMode::HTML,
|
||||
reply_markup: InlineKeyboardMarkup::make()->addRow(
|
||||
InlineKeyboardButton::make(
|
||||
text: '复制 Chat ID',
|
||||
copy_text: CopyTextButton::make($chatId),
|
||||
)
|
||||
),
|
||||
);
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('telegram join announce failed: '.$e->getMessage(), [
|
||||
'chat_id' => $chatId,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
private function statusValue(mixed $status): string
|
||||
{
|
||||
if ($status instanceof ChatMemberStatus) {
|
||||
return $status->value;
|
||||
}
|
||||
|
||||
return strtolower(trim((string) $status));
|
||||
}
|
||||
|
||||
private function e(string $value): string
|
||||
{
|
||||
return htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user