This commit is contained in:
2026-01-30 09:05:59 +08:00
parent b7e3cd840b
commit ad03fab8e9

View File

@@ -3692,14 +3692,18 @@ class ProvisionerBot:
new_display = f"{new_token[:8]}...{new_token[-4:]}" if len(new_token) > 12 else new_token
await update.message.reply_text(
# 发送更新成功消息
progress_msg = await update.message.reply_text(
f"<b>✅ Token 更新成功</b>\n\n"
f"旧 Token: <code>{old_display}</code>\n"
f"新 Token: <code>{new_display}</code>\n\n"
f"配置已自动重载生效",
f"⏳ 正在检测邮件 API 连接...",
parse_mode="HTML"
)
# 测试 API 连接
await self._test_mail_api_connection(update.effective_chat.id, progress_msg.message_id, new_token, config)
except ImportError:
await update.message.reply_text(
"❌ 缺少 tomli_w 依赖\n"
@@ -3708,6 +3712,110 @@ class ProvisionerBot:
except Exception as e:
await update.message.reply_text(f"❌ 更新 Token 失败: {e}")
async def _test_mail_api_connection(self, chat_id: int, message_id: int, token: str, config: dict):
"""测试邮件 API 连接"""
import requests
mail_api_base = config.get("autogptplus", {}).get("mail_api_base", "")
if not mail_api_base:
await self.app.bot.edit_message_text(
chat_id=chat_id,
message_id=message_id,
text="<b>❌ 邮件 API 连接失败</b>\n\n"
"错误: mail_api_base 未配置\n\n"
"请检查配置后重试:\n"
"• mail_api_base: 未配置\n"
"• mail_api_token: 已配置",
parse_mode="HTML"
)
return
try:
# 测试 API 连接
url = f"{mail_api_base}/api/public/emailList"
headers = {
"Authorization": token,
"Content-Type": "application/json"
}
payload = {
"toEmail": "test@test.com",
"timeSort": "desc",
"size": 1
}
start_time = time.time()
response = requests.post(url, headers=headers, json=payload, timeout=10)
elapsed = time.time() - start_time
data = response.json()
token_display = f"{token[:8]}...{token[-4:]}" if len(token) > 12 else token
if response.status_code == 200 and data.get("code") == 200:
await self.app.bot.edit_message_text(
chat_id=chat_id,
message_id=message_id,
text=f"<b>✅ Token 更新成功</b>\n\n"
f"新 Token: <code>{token_display}</code>\n\n"
f"<b>✅ 邮件 API 连接成功</b>\n"
f"• 服务器: {mail_api_base}\n"
f"• 响应时间: {elapsed*1000:.0f}ms\n"
f"• 状态: 正常",
parse_mode="HTML"
)
else:
error_msg = data.get("message", "未知错误")
await self.app.bot.edit_message_text(
chat_id=chat_id,
message_id=message_id,
text=f"<b>✅ Token 更新成功</b>\n\n"
f"新 Token: <code>{token_display}</code>\n\n"
f"<b>❌ 邮件 API 连接失败</b>\n"
f"错误: API 响应异常: {error_msg}\n\n"
f"请检查配置后重试:\n"
f"• mail_api_base: {mail_api_base}\n"
f"• mail_api_token: 已配置",
parse_mode="HTML"
)
except requests.exceptions.ConnectionError:
await self.app.bot.edit_message_text(
chat_id=chat_id,
message_id=message_id,
text=f"<b>✅ Token 更新成功</b>\n\n"
f"<b>❌ 邮件 API 连接失败</b>\n"
f"错误: 无法连接到服务器\n\n"
f"请检查配置后重试:\n"
f"• mail_api_base: {mail_api_base}\n"
f"• mail_api_token: 已配置",
parse_mode="HTML"
)
except requests.exceptions.Timeout:
await self.app.bot.edit_message_text(
chat_id=chat_id,
message_id=message_id,
text=f"<b>✅ Token 更新成功</b>\n\n"
f"<b>❌ 邮件 API 连接失败</b>\n"
f"错误: 连接超时\n\n"
f"请检查配置后重试:\n"
f"• mail_api_base: {mail_api_base}\n"
f"• mail_api_token: 已配置",
parse_mode="HTML"
)
except Exception as e:
await self.app.bot.edit_message_text(
chat_id=chat_id,
message_id=message_id,
text=f"<b>✅ Token 更新成功</b>\n\n"
f"<b>❌ 邮件 API 连接失败</b>\n"
f"错误: {str(e)}\n\n"
f"请检查配置后重试:\n"
f"• mail_api_base: {mail_api_base}\n"
f"• mail_api_token: 已配置",
parse_mode="HTML"
)
@admin_only
async def cmd_autogptplus(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
"""AutoGPTPlus 配置管理 - 交互式菜单"""