Files
telegram-customer-bot/manage_bot.sh
2025-11-01 21:58:31 +08:00

164 lines
4.8 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Telegram Bot 管理脚本
# 统一管理 integrated_bot_ai.py (使用 claude-agent-sdk)
set -euo pipefail
BOT_DIR="/home/atai/telegram-bot"
BOT_SCRIPT="integrated_bot_ai.py"
SCREEN_NAME="agent_bot"
LOG_FILE="bot_agent_sdk.log"
SYSTEMD_UNIT="telegram-bot.service"
SYSTEMD_UNIT_PATH="/etc/systemd/system/${SYSTEMD_UNIT}"
# 环境变量(非 systemd 模式时使用)
export ANTHROPIC_BASE_URL="http://202.79.167.23:3000/api"
export ANTHROPIC_AUTH_TOKEN="cr_9792f20a98f055e204248a41f280780ca2fb8f08f35e60c785e5245653937e06"
export ALL_PROXY="socks5://127.0.0.1:1080"
cd "$BOT_DIR" || exit 1
if [[ -f "$SYSTEMD_UNIT_PATH" ]]; then
USE_SYSTEMD=1
else
USE_SYSTEMD=0
fi
systemd_active() {
sudo systemctl is-active --quiet "$SYSTEMD_UNIT"
}
start_bot_legacy() {
if screen -list | grep -q "$SCREEN_NAME"; then
echo "⚠️ Bot 已经在运行中"
screen -ls | grep "$SCREEN_NAME"
else
screen -dmS "$SCREEN_NAME" bash -lc "cd $BOT_DIR && exec ./run_bot_loop.sh"
sleep 2
if screen -list | grep -q "$SCREEN_NAME"; then
echo "✅ Bot 已启动"
screen -ls | grep "$SCREEN_NAME"
echo ""
echo "📝 查看日志: tail -f $BOT_DIR/$LOG_FILE"
else
echo "❌ 启动失败,请检查日志"
fi
fi
}
stop_bot_legacy() {
if screen -list | grep -q "$SCREEN_NAME"; then
screen -S "$SCREEN_NAME" -X quit
sleep 1
echo "✅ Bot 已停止"
else
echo "⚠️ Bot 没有运行"
fi
}
case "${1:-status}" in
start)
echo "🚀 启动 Telegram Bot..."
if [[ $USE_SYSTEMD -eq 1 ]]; then
if systemd_active; then
echo "⚠️ systemd 服务已在运行"
else
sudo systemctl start "$SYSTEMD_UNIT"
sleep 2
if systemd_active; then
echo "✅ systemd 已启动机器人"
else
echo "❌ 启动失败,请查看: sudo journalctl -u $SYSTEMD_UNIT"
fi
fi
else
start_bot_legacy
fi
;;
stop)
echo "🛑 停止 Telegram Bot..."
if [[ $USE_SYSTEMD -eq 1 ]]; then
if systemd_active; then
sudo systemctl stop "$SYSTEMD_UNIT"
echo "✅ systemd 已停止机器人"
else
echo "⚠️ systemd 服务未运行"
fi
else
stop_bot_legacy
fi
;;
restart)
echo "🔄 重启 Telegram Bot..."
if [[ $USE_SYSTEMD -eq 1 ]]; then
sudo systemctl restart "$SYSTEMD_UNIT"
sleep 2
if systemd_active; then
echo "✅ systemd 重启完成"
else
echo "❌ 重启失败,请检查 systemd 日志"
fi
else
stop_bot_legacy
sleep 2
start_bot_legacy
fi
;;
status)
echo "📊 Bot 状态检查..."
echo ""
if [[ $USE_SYSTEMD -eq 1 ]]; then
sudo systemctl status "$SYSTEMD_UNIT" --no-pager
echo ""
fi
if screen -list | grep -q "$SCREEN_NAME"; then
echo "✅ Screen 会话运行中"
screen -ls | grep "$SCREEN_NAME"
else
echo "❌ 未检测到 Screen 会话"
fi
echo ""
echo "最近日志:"
tail -20 "$BOT_DIR/$LOG_FILE" || echo "暂无日志"
;;
logs)
echo "📝 实时日志 (Ctrl+C 退出)..."
tail -f "$BOT_DIR/$LOG_FILE"
;;
attach)
echo "🔗 进入 Bot Screen 会话 (Ctrl+A, D 退出)..."
screen -r "$SCREEN_NAME"
;;
info)
echo " Bot 信息"
echo "============================================"
echo "脚本: $BOT_SCRIPT"
echo "位置: $BOT_DIR"
echo "日志: $LOG_FILE"
echo "Screen: $SCREEN_NAME"
if [[ $USE_SYSTEMD -eq 1 ]]; then
echo "服务: $SYSTEMD_UNIT (systemd)"
else
echo "服务: Screen 模式"
fi
echo "使用: claude-agent-sdk (Python)"
echo "模型: claude-sonnet-4-5-20250929"
echo "============================================"
;;
*)
echo "Telegram Bot 管理脚本"
echo ""
echo "用法: $0 {start|stop|restart|status|logs|attach|info}"
echo ""
echo "命令说明:"
echo " start - 启动 Bot"
echo " stop - 停止 Bot"
echo " restart - 重启 Bot"
echo " status - 查看运行状态"
echo " logs - 实时查看日志"
echo " attach - 进入 Screen 会话"
echo " info - 显示 Bot 信息"
exit 1
;;
esac