4
This commit is contained in:
110
telegram_bot.py
110
telegram_bot.py
@@ -2716,6 +2716,54 @@ class ProvisionerBot:
|
||||
except Exception as e:
|
||||
await update.message.reply_text(f"❌ 修改配置失败: {e}")
|
||||
|
||||
async def _test_mail_api_connection(self, mail_api_base: str, mail_api_token: str, domain: str) -> tuple[bool, str]:
|
||||
"""测试邮件 API 连接
|
||||
|
||||
Args:
|
||||
mail_api_base: 邮件 API 地址
|
||||
mail_api_token: 邮件 API Token
|
||||
domain: 测试用的邮箱域名
|
||||
|
||||
Returns:
|
||||
tuple: (success, message)
|
||||
"""
|
||||
import requests
|
||||
import random
|
||||
import string
|
||||
|
||||
try:
|
||||
# 生成测试邮箱
|
||||
random_str = ''.join(random.choices(string.ascii_lowercase + string.digits, k=8))
|
||||
test_email = f"test-{random_str}@{domain.lstrip('@')}"
|
||||
|
||||
# 测试 API 连接
|
||||
url = f"{mail_api_base}/api/public/emailList"
|
||||
headers = {
|
||||
"Authorization": mail_api_token,
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
payload = {
|
||||
"toEmail": test_email,
|
||||
"timeSort": "desc",
|
||||
"size": 1
|
||||
}
|
||||
|
||||
response = requests.post(url, headers=headers, json=payload, timeout=10)
|
||||
data = response.json()
|
||||
|
||||
if data.get("code") == 200:
|
||||
return True, "邮件 API 连接正常"
|
||||
else:
|
||||
error_msg = data.get("message", "未知错误")
|
||||
return False, f"API 响应异常: {error_msg}"
|
||||
|
||||
except requests.exceptions.Timeout:
|
||||
return False, "连接超时,无法连接到邮件 API 服务器"
|
||||
except requests.exceptions.ConnectionError:
|
||||
return False, "连接失败,请检查 mail_api_base 配置"
|
||||
except Exception as e:
|
||||
return False, f"测试失败: {e}"
|
||||
|
||||
@admin_only
|
||||
async def cmd_team_register(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
"""开始 GPT Team 自动订阅注册"""
|
||||
@@ -2757,6 +2805,25 @@ class ProvisionerBot:
|
||||
parse_mode="HTML"
|
||||
)
|
||||
return
|
||||
|
||||
# 测试邮件 API 连接
|
||||
await update.message.reply_text("⏳ 正在检测邮件 API 连接...")
|
||||
|
||||
import random
|
||||
test_domain = random.choice(domains)
|
||||
success, message = await self._test_mail_api_connection(MAIL_API_BASE, MAIL_API_TOKEN, test_domain)
|
||||
|
||||
if not success:
|
||||
await update.message.reply_text(
|
||||
f"<b>❌ 邮件 API 连接失败</b>\n\n"
|
||||
f"错误: {message}\n\n"
|
||||
f"请检查配置后重试:\n"
|
||||
f"• mail_api_base: {MAIL_API_BASE}\n"
|
||||
f"• mail_api_token: {'已配置' if MAIL_API_TOKEN else '未配置'}",
|
||||
parse_mode="HTML"
|
||||
)
|
||||
return
|
||||
|
||||
except ImportError:
|
||||
await update.message.reply_text("❌ auto_gpt_team 模块未找到")
|
||||
return
|
||||
@@ -2943,6 +3010,8 @@ class ProvisionerBot:
|
||||
try:
|
||||
import run
|
||||
if run._shutdown_requested:
|
||||
with step_lock:
|
||||
current_step[0] = "用户请求停止..."
|
||||
break
|
||||
except:
|
||||
pass
|
||||
@@ -2953,6 +3022,13 @@ class ProvisionerBot:
|
||||
import functools
|
||||
|
||||
def run_with_callback():
|
||||
# 在执行前再次检查停止请求
|
||||
try:
|
||||
import run as run_module
|
||||
if run_module._shutdown_requested:
|
||||
return {"success": False, "error": "用户停止", "stopped": True}
|
||||
except:
|
||||
pass
|
||||
return run_single_registration_auto(
|
||||
progress_callback=None,
|
||||
step_callback=step_callback
|
||||
@@ -2971,6 +3047,8 @@ class ProvisionerBot:
|
||||
if result.get("stopped"):
|
||||
# 被 /stop 命令中断,不计入失败
|
||||
log.info("注册被用户停止")
|
||||
with step_lock:
|
||||
current_step[0] = "已停止"
|
||||
break
|
||||
elif result.get("success"):
|
||||
success_count += 1
|
||||
@@ -2985,12 +3063,35 @@ class ProvisionerBot:
|
||||
else:
|
||||
fail_count += 1
|
||||
log.warning(f"注册失败: {result.get('error', '未知错误')}")
|
||||
except asyncio.CancelledError:
|
||||
log.info("注册任务被取消")
|
||||
with step_lock:
|
||||
current_step[0] = "已取消"
|
||||
break
|
||||
except Exception as e:
|
||||
fail_count += 1
|
||||
log.error(f"注册异常: {e}")
|
||||
|
||||
# 清理浏览器进程
|
||||
cleanup_chrome_processes()
|
||||
|
||||
# 每次注册后检查停止请求
|
||||
try:
|
||||
import run
|
||||
if run._shutdown_requested:
|
||||
with step_lock:
|
||||
current_step[0] = "用户请求停止..."
|
||||
break
|
||||
except:
|
||||
pass
|
||||
|
||||
# 检查是否被停止
|
||||
stopped = False
|
||||
try:
|
||||
import run
|
||||
stopped = run._shutdown_requested
|
||||
except:
|
||||
pass
|
||||
|
||||
# 停止进度更新任务
|
||||
progress_task.cancel()
|
||||
@@ -3001,8 +3102,15 @@ class ProvisionerBot:
|
||||
|
||||
# 完成进度
|
||||
progress_bar = '▰' * 20
|
||||
completed = success_count + fail_count
|
||||
|
||||
if stopped:
|
||||
status_text = f"<b>🛑 注册已停止</b> {completed}/{count}"
|
||||
else:
|
||||
status_text = f"<b>🎉 注册完成!</b> {success_count}/{count}"
|
||||
|
||||
await progress_msg.edit_text(
|
||||
f"<b>🎉 注册完成!</b> {success_count}/{count}\n"
|
||||
f"{status_text}\n"
|
||||
f"{progress_bar}\n\n"
|
||||
f"✅ 成功: {success_count}\n"
|
||||
f"❌ 失败: {fail_count}",
|
||||
|
||||
Reference in New Issue
Block a user