Add authenticated Telegram ticket replies and notification tracking

This commit is contained in:
hellcat committed 2026-10-11 11:58:19 +08:00
1 parent 9f8f297a5d
commit 6c9422d055
21 files changed
+439 -82

No files matched your search

+15 -67
View File
@@ -71,41 +71,8 @@ class Plugin extends AbstractPlugin
return;
}
$message = $ticket->messages()->latest()->first();
$user = User::find($ticket->user_id);
if (!$user)
return;
$user->load('plan');
$transfer_enable = $this->transferToGBString($user->transfer_enable);
$remaining_traffic = $this->transferToGBString($user->transfer_enable - $user->u - $user->d);
$u = $this->transferToGBString($user->u);
$d = $this->transferToGBString($user->d);
$expired_at = $user->expired_at ? date('Y-m-d H:i:s', $user->expired_at) : '长期有效';
$money = $user->balance / 100;
$affmoney = $user->commission_balance / 100;
$plan = $user->plan;
$ip = request()?->ip() ?? '';
$region = $ip ? (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) ? (new \Ip2Region())->simple($ip) : 'NULL') : '';
$TGmessage = "📮 *工单提醒* #{$ticket->id}\n";
$TGmessage .= "━━━━━━━━━━━━━━━━━━━━\n";
$TGmessage .= "📧 邮箱: `{$user->email}`\n";
$TGmessage .= "📍 位置: `{$region}`\n";
if ($plan) {
$TGmessage .= "📦 套餐: `" . Helper::escapeMarkdown($plan->name) . "`\n";
$TGmessage .= "📊 流量: `{$remaining_traffic}G / {$transfer_enable}G` (剩余/总计)\n";
$TGmessage .= "⬆️⬇️ 已用: `{$u}G / {$d}G`\n";
$TGmessage .= "⏰ 到期: `{$expired_at}`\n";
} else {
$TGmessage .= "📦 套餐: `未订购任何套餐`\n";
}
$TGmessage .= "💰 余额: `{$money}元`\n";
$TGmessage .= "💸 佣金: `{$affmoney}元`\n";
$TGmessage .= "━━━━━━━━━━━━━━━━━━━━\n";
$TGmessage .= "📝 *主题*: `" . Helper::escapeMarkdown($ticket->subject) . "`\n";
$TGmessage .= "💬 *内容*: `" . Helper::escapeMarkdown($message->message) . "`";
$this->telegramService->sendMessageWithAdmin($TGmessage, true);
try { app(\App\Services\TelegramTickets\TicketBridge::class)->enqueue($ticket); }
catch (\Throwable $e) { Log::warning('Telegram ticket notification could not be queued', ['ticket_id' => $ticket->id]); }
}
protected function registerDefaultCommands(): void
@@ -114,6 +81,7 @@ class Plugin extends AbstractPlugin
$this->registerTelegramCommand($command, [$this, $config['handler']]);
}
$this->registerTelegramCommand('/staffbind', [$this, 'handleStaffBind']);
$this->registerReplyHandler('/(📮.*?工单提醒.*?#?|工单ID: ?)(\\d+)/', [$this, 'handleTicketReply']);
}
@@ -160,6 +128,12 @@ class Plugin extends AbstractPlugin
return $user;
}
public function handleStaffBind(object $msg): void
{
$result = app(\App\Services\TelegramTickets\TicketBridge::class)->bind($msg->raw_message ?? [], (string) ($msg->args[0] ?? ''));
$this->telegramService->sendMessage($msg->chat_id, $result);
}
public function handleStartCommand(object $msg): void
{
$welcomeTitle = $this->getConfig('start_welcome_title', '🎉 欢迎使用 XBoard Telegram Bot!');
@@ -196,6 +170,7 @@ class Plugin extends AbstractPlugin
default => false
};
} catch (\Exception $e) {
if (($msg->message_type ?? '') === 'reply_message') { throw new \App\Exceptions\ApiException('工单回复暂时未完成,请重试', 500); }
Log::error('Telegram 命令处理意外错误', [
'command' => $msg->command ?? 'unknown',
'chat_id' => $msg->chat_id ?? 'unknown',
@@ -228,12 +203,9 @@ class Plugin extends AbstractPlugin
return false;
}
foreach ($this->commands['replies'] as $regex => $handler) {
if (preg_match($regex, $msg->reply_text, $matches)) {
call_user_func($handler, $msg, $matches);
return true;
}
}
$bridge = app(\App\Services\TelegramTickets\TicketBridge::class);
$result = $bridge->reply($msg->raw_message ?? [], (int) ($msg->update_id ?? 0));
if ($result !== null) { $this->sendMessage($msg, $result); return true; }
return false;
}
@@ -386,32 +358,8 @@ class Plugin extends AbstractPlugin
public function handleTicketReply(object $msg, array $matches): void
{
$user = $this->getBoundUser($msg);
if (!$user) {
return;
}
if (!isset($matches[2]) || !is_numeric($matches[2])) {
Log::warning('Telegram 工单回复正则未匹配到工单ID', ['matches' => $matches, 'msg' => $msg]);
$this->sendMessage($msg, '未能识别工单ID,请直接回复工单提醒消息。');
return;
}
$ticketId = (int) $matches[2];
$ticket = Ticket::where('id', $ticketId)->first();
if (!$ticket) {
$this->sendMessage($msg, '工单不存在');
return;
}
$ticketService = new TicketService();
$ticketService->replyByAdmin(
$ticketId,
$msg->text,
$user->id
);
$this->sendMessage($msg, "工单 #{$ticketId} 回复成功");
// Legacy text-derived ticket IDs are no longer accepted.
$this->sendMessage($msg, '请直接回复机器人发送的新工单提醒消息。');
}
/**