v3: 新增 claw 管理命令(--menu),装完可随时改模型/Telegram/启停/更新,无需重装

This commit is contained in:
2026-08-10 05:18:19 +00:00
parent c2cff584e7
commit c314493e1e
2 changed files with 1109 additions and 0 deletions
+181
View File
@@ -503,6 +503,12 @@ wizard() {
plain ""
# --- Telegram ---
wizard_telegram
wizard_provider
}
# --- 可独立调用:Telegram 配置 ---
wizard_telegram() {
plain "${C_BOLD}[1/3] Telegram 机器人${C_RESET}"
plain " 获取方式:在 Telegram 里搜索 ${C_BOLD}@BotFather${C_RESET} → 发送 /newbot → 按提示创建"
plain " 创建成功后它会给你一串形如 123456:AAxxxxxxxx 的 Token"
@@ -534,6 +540,10 @@ wizard() {
# --- Provider ---
plain ""
}
# --- 可独立调用:API 模型配置 ---
wizard_provider() {
plain "${C_BOLD}[3/3] AI 模型服务(API 配置)${C_RESET}"
plain " 1) OpenAI 官方"
plain " 2) Anthropic 官方(Claude"
@@ -950,9 +960,178 @@ except Exception:
# ---------------------------------------------------------------------------
# main
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# 8. 管理菜单(装完后用 claw 命令随时改配置)
# ---------------------------------------------------------------------------
CLAW_BIN="/usr/local/bin/claw"
install_claw_command() {
local self_url="https://mjjtop.com/claw"
local target="$CLAW_BIN"
# 非 root 时装到用户目录
if [ "$(id -u)" != "0" ]; then
target="$HOME/.local/bin/claw"
mkdir -p "$HOME/.local/bin" 2>/dev/null || true
fi
cat > "$target" <<CLAWEOF
#!/usr/bin/env bash
# OpenClaw 管理命令(由一键脚本安装)
exec bash <(curl -sL ${self_url}) --menu "\$@"
CLAWEOF
chmod +x "$target" 2>/dev/null || true
if [ -x "$target" ]; then
ok "管理命令已安装:${target}"
hint "以后随时输入 ${C_BOLD}claw${C_RESET} 即可打开管理菜单(改模型/改 Telegram/启停/更新)"
case ":$PATH:" in
*":$(dirname "$target"):"*) : ;;
*) warn "$(dirname "$target") 不在 PATH 中,请手动执行:export PATH=\"$(dirname "$target"):\$PATH\"" ;;
esac
else
warn "管理命令安装失败,可稍后手动重跑本脚本。"
fi
}
svc_status_line() {
local inst="未安装" run="未运行" ver=""
if command -v openclaw >/dev/null 2>&1; then
ver="$(openclaw_local_version)"
inst="已安装${ver:+ v$ver}"
fi
is_running && run="运行中"
printf '%s | %s' "$inst" "$run"
}
show_current_config() {
[ -z "${OPENCLAW_CONFIG:-}" ] && detect_config_path
if [ ! -f "$OPENCLAW_CONFIG" ]; then
warn "尚未找到配置文件:${OPENCLAW_CONFIG:-未知}"
return 1
fi
python3 - "$OPENCLAW_CONFIG" <<'PYEOF'
import json,sys
try:
cfg=json.load(open(sys.argv[1]))
except Exception as e:
print(" 读取配置失败:",e); sys.exit(0)
def mask(v):
if not isinstance(v,str) or not v: return "(未设置)"
return v[:6]+"***"+v[-4:] if len(v)>12 else "***"
tg=cfg.get("channels",{}).get("telegram",{})
print(" Telegram", "已启用" if tg.get("enabled") else "未启用")
print(" botToken :", mask(tg.get("botToken","")))
af=tg.get("allowFrom") or []
print(" allowFrom:", ", ".join(str(x) for x in af) if af else "(未设置)")
provs=cfg.get("models",{}).get("providers",{})
if provs:
print(" 模型 Provider")
for name,p in provs.items():
if not isinstance(p,dict): continue
print(f" [{name}] baseURL={p.get('baseURL','(默认)')} key={mask(p.get('apiKey',''))}")
ms=p.get("models")
if isinstance(ms,list) and ms: print(f" models={', '.join(map(str,ms[:5]))}")
else:
print(" 模型 Provider(未配置)")
dm=cfg.get("agents",{}).get("defaults",{}).get("model")
print(" 默认模型:", dm or "(未设置)")
PYEOF
}
menu_mode() {
resolve_home
detect_config_path
while true; do
plain ""
plain "${C_BOLD}======================================${C_RESET}"
plain "${C_BOLD} OpenClaw 管理菜单${C_RESET}"
plain " 状态:$(svc_status_line)"
plain " 配置:${OPENCLAW_CONFIG:-未知}"
plain "${C_BOLD}======================================${C_RESET}"
plain " 1) 查看当前配置"
plain " 2) 配置 / 更换 API 模型"
plain " 3) 配置 Telegram 机器人"
plain " 4) 启动服务"
plain " 5) 停止服务"
plain " 6) 重启服务"
plain " 7) 查看运行状态"
plain " 8) 查看日志"
plain " 9) 更新 OpenClaw 到最新版"
plain " 0) 退出"
plain ""
ask " 请输入编号" "0"
case "$REPLY_VALUE" in
1) plain ""; show_current_config ;;
2)
CFG_PROVIDER=""; CFG_BASEURL=""; CFG_APIKEY=""; CFG_MODEL=""
wizard_provider
if [ "$CFG_PROVIDER" != "skip" ] && [ -n "$CFG_PROVIDER" ]; then
write_config && ok "模型配置已更新。" || warn "写入失败。"
ask_yes_no " 需要重启服务让配置生效吗?" "y" && do_restart
fi
;;
3)
CFG_TG_TOKEN=""; CFG_TG_USERID=""
wizard_telegram
if [ -n "$CFG_TG_TOKEN" ] || [ -n "$CFG_TG_USERID" ]; then
CFG_PROVIDER="skip"
write_config && ok "Telegram 配置已更新。" || warn "写入失败。"
ask_yes_no " 需要重启服务让配置生效吗?" "y" && do_restart
fi
;;
4) run_quiet openclaw gateway start && ok "已启动" || warn "启动失败,见日志" ;;
5) run_quiet openclaw gateway stop && ok "已停止" || warn "停止失败" ;;
6) do_restart ;;
7) plain ""; openclaw status 2>&1 | head -40 ;;
8) plain ""; openclaw logs 2>&1 | tail -40 || tail -40 "$LOG_FILE" 2>/dev/null ;;
9)
info "正在更新…"
if run_quiet npm install -g openclaw@latest --no-update-notifier; then
ok "已更新到 v$(openclaw_local_version)"
ask_yes_no " 重启服务生效?" "y" && do_restart
else
warn "更新失败,请检查网络。"
fi
;;
0) plain "已退出。"; return 0 ;;
*) warn " 请输入 0-9 之间的编号。" ;;
esac
done
}
do_restart() {
info "正在重启服务…"
run_quiet openclaw gateway stop || true
sleep 2
if run_quiet openclaw gateway start; then
sleep 2
is_running && ok "服务已重启并在运行" || warn "已发出启动命令,但未检测到进程"
else
warn "重启失败,请执行 openclaw status 查看"
fi
}
main() {
init_log "$@"
# --menu:管理模式(由 claw 命令调用),不重装,只改配置/启停
for a in "$@"; do
if [ "$a" = "--menu" ]; then
plain ""
plain "${C_BOLD}OpenClaw 管理工具 v${SCRIPT_VERSION}${C_RESET}"
if ! command -v openclaw >/dev/null 2>&1; then
die "未检测到 OpenClaw,请先执行安装:" "bash <(curl -sL https://mjjtop.com/claw)"
fi
menu_mode
exit 0
fi
done
plain ""
plain "${C_BOLD}OpenClaw 一键安装配置脚本 v${SCRIPT_VERSION}${C_RESET}"
plain "日志:${LOG_FILE}"
@@ -976,6 +1155,8 @@ main() {
onboard_and_start
local start_rc=$?
install_claw_command
summary
if [ "$start_rc" -eq 0 ]; then