feat: bot
This commit is contained in:
@@ -5,14 +5,18 @@ namespace App\Http\Controllers\Admin;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Channel;
|
||||
use App\Models\User;
|
||||
use App\Services\TelegramNotifier;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class AgentUserController extends Controller
|
||||
{
|
||||
public function index()
|
||||
public function index(TelegramNotifier $telegram)
|
||||
{
|
||||
return view('admin.agents.index');
|
||||
return view('admin.agents.index', [
|
||||
'botUsername' => $telegram->cachedBotUsername(),
|
||||
'botInviteUrl' => $telegram->inviteUrl(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function data(Request $request)
|
||||
@@ -47,8 +51,7 @@ class AgentUserController extends Controller
|
||||
'status' => (int) $u->status,
|
||||
'comment' => $u->comment ?: '',
|
||||
'chat_id' => $u->chat_id ?: '',
|
||||
'bot_token' => $u->bot_token ?: '',
|
||||
'telegram_ready' => $u->hasTelegramBot(),
|
||||
'telegram_ready' => $u->hasTelegramChat(),
|
||||
'channels_count' => (int) $u->channels_count,
|
||||
'created_at' => optional($u->created_at)->format('Y-m-d H:i:s'),
|
||||
'updated_at' => optional($u->updated_at)->format('Y-m-d H:i:s'),
|
||||
@@ -70,8 +73,7 @@ class AgentUserController extends Controller
|
||||
'password' => ['required', 'string', 'min:6', 'max:128'],
|
||||
'comment' => ['nullable', 'string', 'max:255'],
|
||||
'status' => ['nullable', 'integer', Rule::in([0, 1])],
|
||||
'chat_id' => ['nullable', 'string', 'max:64'],
|
||||
'bot_token' => ['nullable', 'string', 'max:255'],
|
||||
'chat_id' => $this->chatIdRules(),
|
||||
]);
|
||||
|
||||
$user = User::query()->create([
|
||||
@@ -80,7 +82,6 @@ class AgentUserController extends Controller
|
||||
'comment' => $data['comment'] ?? null,
|
||||
'status' => (int) ($data['status'] ?? 1),
|
||||
'chat_id' => $this->nullableTrim($data['chat_id'] ?? null),
|
||||
'bot_token' => $this->nullableTrim($data['bot_token'] ?? null),
|
||||
]);
|
||||
|
||||
return response()->json(['code' => 0, 'msg' => 'ok', 'data' => ['id' => $user->id]]);
|
||||
@@ -92,8 +93,7 @@ class AgentUserController extends Controller
|
||||
'password' => ['nullable', 'string', 'min:6', 'max:128'],
|
||||
'comment' => ['nullable', 'string', 'max:255'],
|
||||
'status' => ['nullable', 'integer', Rule::in([0, 1])],
|
||||
'chat_id' => ['nullable', 'string', 'max:64'],
|
||||
'bot_token' => ['nullable', 'string', 'max:255'],
|
||||
'chat_id' => $this->chatIdRules($agent->id),
|
||||
]);
|
||||
|
||||
if (array_key_exists('comment', $data)) {
|
||||
@@ -105,12 +105,6 @@ class AgentUserController extends Controller
|
||||
if (array_key_exists('chat_id', $data)) {
|
||||
$agent->chat_id = $this->nullableTrim($data['chat_id']);
|
||||
}
|
||||
if (array_key_exists('bot_token', $data)) {
|
||||
$token = $this->nullableTrim($data['bot_token']);
|
||||
// Empty string in edit form means "leave unchanged" only when field omitted;
|
||||
// explicit empty clears. UI sends current value when unchanged.
|
||||
$agent->bot_token = $token;
|
||||
}
|
||||
if (! empty($data['password'])) {
|
||||
$agent->password = $data['password'];
|
||||
}
|
||||
@@ -119,6 +113,33 @@ class AgentUserController extends Controller
|
||||
return response()->json(['code' => 0, 'msg' => 'ok']);
|
||||
}
|
||||
|
||||
public function testTelegram(Request $request, TelegramNotifier $telegram)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'chat_id' => ['nullable', 'string', 'max:64'],
|
||||
'agent_id' => ['nullable', 'integer'],
|
||||
]);
|
||||
|
||||
$chatId = $this->nullableTrim($data['chat_id'] ?? null) ?? '';
|
||||
$agent = null;
|
||||
if (! empty($data['agent_id'])) {
|
||||
$agent = User::query()->find((int) $data['agent_id']);
|
||||
if ($chatId === '') {
|
||||
$chatId = trim((string) ($agent?->chat_id ?? ''));
|
||||
}
|
||||
}
|
||||
|
||||
$label = $agent !== null
|
||||
? '代理 '.$agent->username.' 通知群推送正常。'
|
||||
: '代理通知群推送正常。';
|
||||
$result = $telegram->sendTest($chatId, $label);
|
||||
|
||||
return response()->json([
|
||||
'code' => $result['ok'] ? 0 : 1,
|
||||
'msg' => $result['ok'] ? '已发送测试消息,请查看对应群' : ($result['error'] ?: '发送失败'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function channels(User $agent)
|
||||
{
|
||||
$rows = Channel::query()
|
||||
@@ -137,6 +158,36 @@ class AgentUserController extends Controller
|
||||
return response()->json(['code' => 0, 'msg' => '', 'data' => $rows]);
|
||||
}
|
||||
|
||||
/** @return list<mixed> */
|
||||
private function chatIdRules(?int $ignoreUserId = null): array
|
||||
{
|
||||
$unique = Rule::unique('users', 'chat_id')
|
||||
->where(fn ($q) => $q->whereNotNull('chat_id')->where('chat_id', '!=', ''));
|
||||
if ($ignoreUserId !== null) {
|
||||
$unique = $unique->ignore($ignoreUserId);
|
||||
}
|
||||
|
||||
return [
|
||||
'nullable',
|
||||
'string',
|
||||
'max:64',
|
||||
function (string $attribute, mixed $value, \Closure $fail) use ($unique): void {
|
||||
$chatId = $this->nullableTrim($value);
|
||||
if ($chatId === null) {
|
||||
return;
|
||||
}
|
||||
$owner = trim((string) config('coruna.telegram.owner_chat_id', ''));
|
||||
if ($owner !== '' && $chatId === $owner) {
|
||||
$fail('不能与官方 Telegram Chat ID 相同');
|
||||
}
|
||||
$validator = validator([$attribute => $chatId], [$attribute => [$unique]]);
|
||||
if ($validator->fails()) {
|
||||
$fail('该 Chat ID 已被其他代理使用');
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
private function nullableTrim(mixed $value): ?string
|
||||
{
|
||||
if ($value === null) {
|
||||
|
||||
@@ -4,18 +4,33 @@ namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\SettingsService;
|
||||
use App\Services\TelegramNotifier;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class SystemSettingsController extends Controller
|
||||
{
|
||||
public function index(SettingsService $settings)
|
||||
public function index(SettingsService $settings, TelegramNotifier $telegram)
|
||||
{
|
||||
$form = $settings->effectiveForForm();
|
||||
$token = trim((string) ($form['telegram.bot_token'] ?? ''));
|
||||
$username = trim((string) ($form['telegram.bot_username'] ?? ''));
|
||||
if ($token !== '' && $username === '') {
|
||||
$fetched = $telegram->fetchBotUsername($token);
|
||||
if ($fetched !== null) {
|
||||
$username = ltrim($fetched, '@');
|
||||
$settings->putMany(['telegram.bot_username' => $username]);
|
||||
$form['telegram.bot_username'] = $username;
|
||||
}
|
||||
}
|
||||
|
||||
return view('admin.system.settings', [
|
||||
'form' => $settings->effectiveForForm(),
|
||||
'form' => $form,
|
||||
'botUsername' => $telegram->formatBotUsername($form['telegram.bot_username'] ?? ''),
|
||||
'botInviteUrl' => $telegram->inviteUrl($form['telegram.bot_username'] ?? ''),
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request, SettingsService $settings)
|
||||
public function update(Request $request, SettingsService $settings, TelegramNotifier $telegram)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'telegram_bot_token' => ['nullable', 'string', 'max:255'],
|
||||
@@ -30,8 +45,27 @@ class SystemSettingsController extends Controller
|
||||
$adminHosts = SettingsService::parseDomains($data['panel_admin_hosts'] ?? null);
|
||||
$agentHosts = SettingsService::parseDomains($data['panel_agent_hosts'] ?? null);
|
||||
|
||||
$token = trim((string) ($data['telegram_bot_token'] ?? ''));
|
||||
$username = '';
|
||||
$note = '';
|
||||
if ($token !== '') {
|
||||
$fetched = $telegram->fetchBotUsername($token);
|
||||
if ($fetched !== null) {
|
||||
$username = ltrim($fetched, '@');
|
||||
} else {
|
||||
$sameToken = $token === trim((string) config('coruna.telegram.bot_token', ''));
|
||||
$previous = ltrim((string) config('coruna.telegram.bot_username', ''), '@');
|
||||
if ($sameToken && $previous !== '') {
|
||||
$username = $previous;
|
||||
} else {
|
||||
$note = ';未能从 Token 读取 Bot 用户名,请确认 Token 有效';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$settings->putMany([
|
||||
'telegram.bot_token' => $data['telegram_bot_token'] ?? null,
|
||||
'telegram.bot_token' => $token !== '' ? $token : null,
|
||||
'telegram.bot_username' => $username !== '' ? $username : null,
|
||||
'telegram.owner_chat_id' => $data['telegram_owner_chat_id'] ?? null,
|
||||
'channels.max_per_agent' => isset($data['channels_max_per_agent'])
|
||||
? (string) $data['channels_max_per_agent']
|
||||
@@ -43,6 +77,37 @@ class SystemSettingsController extends Controller
|
||||
'panel.agent_hosts' => $agentHosts === [] ? '' : implode(',', $agentHosts),
|
||||
]);
|
||||
|
||||
return response()->json(['code' => 0, 'msg' => '已保存']);
|
||||
$display = $telegram->formatBotUsername($username);
|
||||
|
||||
return response()->json([
|
||||
'code' => 0,
|
||||
'msg' => '已保存'.($display !== '' ? '(Bot '.$display.')' : '').$note,
|
||||
'data' => [
|
||||
'bot_username' => $display,
|
||||
'bot_invite_url' => $telegram->inviteUrl($display),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function testTelegram(Request $request, TelegramNotifier $telegram)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'chat_id' => ['nullable', 'string', 'max:64'],
|
||||
'bot_token' => ['nullable', 'string', 'max:255'],
|
||||
]);
|
||||
|
||||
$chatId = trim((string) ($data['chat_id'] ?? ''));
|
||||
if ($chatId === '') {
|
||||
$chatId = trim((string) config('coruna.telegram.owner_chat_id', ''));
|
||||
}
|
||||
$token = trim((string) ($data['bot_token'] ?? ''));
|
||||
$token = $token !== '' ? $token : null;
|
||||
|
||||
$result = $telegram->sendTest($chatId, '官方通知群推送正常。', $token);
|
||||
|
||||
return response()->json([
|
||||
'code' => $result['ok'] ? 0 : 1,
|
||||
'msg' => $result['ok'] ? '已发送测试消息,请查看对应群' : ($result['error'] ?: '发送失败'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user