server启动之后,填入callback

This commit is contained in:
liuwei
2025-04-23 11:42:58 +08:00
parent b476d30632
commit 1ab34cde0a
2 changed files with 25 additions and 21 deletions

View File

@@ -25,13 +25,21 @@ class Client:
base_url=self.base_url,
token=self.gewechat_token
)
# 登录, 自动创建二维码,扫码后自动登录
app_id, error_msg = self.client.login(app_id=self.app_id)
if error_msg:
logger.error("登录失败")
return
# 休眠等待server启动防止回调设置失败
# 如果启动时配置文件中的app_id为空那么将app_id写入配置文件
if not self.app_id and app_id:
config["Gewechat"]["app_id"] = app_id
with open(self.config_path, "w", encoding="utf-8") as f:
toml.dump(config, f)
logger.info(f"已将新的APP_ID: {app_id} 写入配置文件")
self.app_id = app_id
def client_set_callback(self):
"""在server启动后调用此方法设置回调"""
max_retries = 5
retry_interval = 5 # 秒
for attempt in range(1, max_retries + 1):
@@ -45,15 +53,5 @@ class Client:
else:
logger.error("set_callback 多次重试后仍失败请检查server状态。")
# 如果启动时配置文件中的app_id为空那么将app_id写入配置文件
if not self.app_id and app_id:
# 更新 config.toml 文件中的 app_id
config["Gewechat"]["app_id"] = app_id
with open(self.config_path, "w", encoding="utf-8") as f:
toml.dump(config, f)
logger.info(f"已将新的APP_ID: {app_id} 写入配置文件")
# 同时更新当前实例的 app_id
self.app_id = app_id
# 项目全局唯一 client 实例
gewe_client = Client().client