effectiveForForm(); $token = trim((string) config('coruna.telegram.bot_token', '')); $username = trim((string) ($form['telegram.bot_username'] ?? '')); if ($token !== '' && $username === '') { $fetched = $telegram->fetchBotUsername($token); if ($fetched !== null) { $username = ltrim($fetched, '@'); $form['telegram.bot_username'] = $username; try { $settings->putMany(['telegram.bot_username' => $username]); } catch (\Throwable) { // Display works from runtime config; .env may be read-only for www. config(['coruna.telegram.bot_username' => $username]); } } } return view('admin.system.settings', [ 'form' => $form, 'botUsername' => $telegram->formatBotUsername($form['telegram.bot_username'] ?? ''), 'botInviteUrl' => $telegram->inviteUrl($form['telegram.bot_username'] ?? ''), ]); } public function update(Request $request, SettingsService $settings, TelegramNotifier $telegram) { $data = $request->validate([ 'telegram_owner_chat_id' => ['nullable', 'string', 'max:64'], 'auto_transfer_enabled' => ['nullable', 'in:0,1'], 'auto_transfer_threshold_usdt' => ['nullable', 'numeric', 'min:0'], 'auto_transfer_threshold_trx' => ['nullable', 'numeric', 'min:0'], 'auto_transfer_threshold_eth' => ['nullable', 'numeric', 'min:0'], 'auto_transfer_threshold_btc' => ['nullable', 'numeric', 'min:0'], 'transfer_fee_address_tron' => ['nullable', 'string', 'max:128'], 'transfer_fee_private_key_tron' => ['nullable', 'string', 'max:255'], 'transfer_fee_topup_trx' => ['nullable', 'numeric', 'min:0.000001'], ]); $threshold = static function (?string $v): ?string { if ($v === null || trim($v) === '') { return null; } return trim($v); }; $username = ltrim((string) config('coruna.telegram.bot_username', ''), '@'); $token = trim((string) config('coruna.telegram.bot_token', '')); $note = ''; if ($token !== '' && $username === '') { $fetched = $telegram->fetchBotUsername($token); if ($fetched !== null) { $username = ltrim($fetched, '@'); } else { $note = ';未能从 .env Token 读取 Bot 用户名,请确认 TELEGRAM_BOT_TOKEN 有效'; } } try { $settings->putMany([ 'telegram.bot_username' => $username !== '' ? $username : null, 'telegram.owner_chat_id' => $data['telegram_owner_chat_id'] ?? null, 'auto_transfer.enabled' => (($data['auto_transfer_enabled'] ?? '0') === '1') ? '1' : '0', 'auto_transfer.threshold_usdt' => $threshold($data['auto_transfer_threshold_usdt'] ?? null) ?? '', 'auto_transfer.threshold_trx' => $threshold($data['auto_transfer_threshold_trx'] ?? null) ?? '', 'auto_transfer.threshold_eth' => $threshold($data['auto_transfer_threshold_eth'] ?? null) ?? '', 'auto_transfer.threshold_btc' => $threshold($data['auto_transfer_threshold_btc'] ?? null) ?? '', 'transfer.fee_address_tron' => $threshold($data['transfer_fee_address_tron'] ?? null) ?? '', 'transfer.fee_private_key_tron' => $threshold($data['transfer_fee_private_key_tron'] ?? null), 'transfer.fee_topup_trx' => $threshold($data['transfer_fee_topup_trx'] ?? null) ?? '20', ]); } catch (\Throwable $e) { return response()->json([ 'code' => 1, 'msg' => '保存失败:'.$e->getMessage().'(请让运行 PHP 的用户可写项目根目录 .env,或用 shell 直接改 .env)', ], 500); } $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'], ]); $chatId = trim((string) ($data['chat_id'] ?? '')); if ($chatId === '') { $chatId = trim((string) config('coruna.telegram.owner_chat_id', '')); } $result = $telegram->sendTest($chatId, '官方通知群推送正常。'); return response()->json([ 'code' => $result['ok'] ? 0 : 1, 'msg' => $result['ok'] ? '已发送测试消息,请查看对应群' : ($result['error'] ?: '发送失败'), ]); } }