mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-10 21:10:59 +00:00
refactor(gewechat): remove redundant set_webhook_url and login logic
Remove the unnecessary set_webhook_url method and its caller in botmgr, instead inject webhook_prefix via adapter config for self-assembly. Remove login logic from run_async since login is handled elsewhere. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -272,12 +272,9 @@ class PlatformManager:
|
|||||||
# 如果 adapter 支持 set_bot_uuid 方法,设置 bot_uuid(用于统一 webhook)
|
# 如果 adapter 支持 set_bot_uuid 方法,设置 bot_uuid(用于统一 webhook)
|
||||||
if hasattr(adapter_inst, 'set_bot_uuid'):
|
if hasattr(adapter_inst, 'set_bot_uuid'):
|
||||||
adapter_inst.set_bot_uuid(bot_entity.uuid)
|
adapter_inst.set_bot_uuid(bot_entity.uuid)
|
||||||
|
adapter_inst.config['_webhook_prefix'] = self.ap.instance_config.data['api'].get(
|
||||||
# 如果 adapter 支持 set_webhook_url 方法,构建并传递完整的 webhook URL
|
'webhook_prefix', 'http://127.0.0.1:5300'
|
||||||
if hasattr(adapter_inst, 'set_webhook_url'):
|
)
|
||||||
webhook_prefix = self.ap.instance_config.data['api'].get('webhook_prefix', 'http://127.0.0.1:5300')
|
|
||||||
webhook_full_url = f'{webhook_prefix}/bots/{bot_entity.uuid}'
|
|
||||||
adapter_inst.set_webhook_url(webhook_full_url)
|
|
||||||
|
|
||||||
runtime_bot = RuntimeBot(ap=self.ap, bot_entity=bot_entity, adapter=adapter_inst, logger=logger)
|
runtime_bot = RuntimeBot(ap=self.ap, bot_entity=bot_entity, adapter=adapter_inst, logger=logger)
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import gewechat_client
|
|||||||
import typing
|
import typing
|
||||||
import asyncio
|
import asyncio
|
||||||
import traceback
|
import traceback
|
||||||
import time
|
|
||||||
import re
|
import re
|
||||||
import copy
|
import copy
|
||||||
import threading
|
import threading
|
||||||
@@ -404,7 +404,6 @@ class GewechatEventConverter(abstract_platform_adapter.AbstractEventConverter):
|
|||||||
class GeWeChatAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
class GeWeChatAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
||||||
bot: gewechat_client.GewechatClient = None
|
bot: gewechat_client.GewechatClient = None
|
||||||
bot_uuid: str = None
|
bot_uuid: str = None
|
||||||
webhook_url: str = None
|
|
||||||
message_converter: GewechatMessageConverter = None
|
message_converter: GewechatMessageConverter = None
|
||||||
event_converter: GewechatEventConverter = None
|
event_converter: GewechatEventConverter = None
|
||||||
|
|
||||||
@@ -425,9 +424,6 @@ class GeWeChatAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
|||||||
def set_bot_uuid(self, bot_uuid: str):
|
def set_bot_uuid(self, bot_uuid: str):
|
||||||
self.bot_uuid = bot_uuid
|
self.bot_uuid = bot_uuid
|
||||||
|
|
||||||
def set_webhook_url(self, webhook_url: str):
|
|
||||||
self.webhook_url = webhook_url
|
|
||||||
|
|
||||||
async def handle_unified_webhook(self, bot_uuid: str, path: str, request):
|
async def handle_unified_webhook(self, bot_uuid: str, path: str, request):
|
||||||
data = await request.json
|
data = await request.json
|
||||||
await self.logger.debug(f'Gewechat callback event: {data}')
|
await self.logger.debug(f'Gewechat callback event: {data}')
|
||||||
@@ -575,9 +571,8 @@ class GeWeChatAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def _build_callback_url(self) -> str:
|
def _build_callback_url(self) -> str:
|
||||||
if self.webhook_url:
|
webhook_prefix = self.config.get('_webhook_prefix', 'http://127.0.0.1:5300').rstrip('/')
|
||||||
return self.webhook_url
|
return f'{webhook_prefix}/bots/{self.bot_uuid}'
|
||||||
raise Exception('webhook_url 未设置,请检查 LangBot 的 api.webhook_prefix 配置')
|
|
||||||
|
|
||||||
async def run_async(self):
|
async def run_async(self):
|
||||||
if not self.config['token']:
|
if not self.config['token']:
|
||||||
@@ -592,20 +587,10 @@ class GeWeChatAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
|||||||
|
|
||||||
self.bot = gewechat_client.GewechatClient(f'{self.config["gewechat_url"]}/v2/api', self.config['token'])
|
self.bot = gewechat_client.GewechatClient(f'{self.config["gewechat_url"]}/v2/api', self.config['token'])
|
||||||
|
|
||||||
def gewechat_login_process():
|
def gewechat_init_process():
|
||||||
app_id, error_msg = self.bot.login(self.config['app_id'])
|
|
||||||
if error_msg:
|
|
||||||
raise Exception(f'Gewechat 登录失败: {error_msg}')
|
|
||||||
|
|
||||||
self.config['app_id'] = app_id
|
|
||||||
|
|
||||||
print(f'Gewechat 登录成功,app_id: {app_id}')
|
|
||||||
|
|
||||||
profile = self.bot.get_profile(self.config['app_id'])
|
profile = self.bot.get_profile(self.config['app_id'])
|
||||||
self.bot_account_id = profile['data']['nickName']
|
self.bot_account_id = profile['data']['nickName']
|
||||||
|
|
||||||
time.sleep(2)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
callback_url = self._build_callback_url()
|
callback_url = self._build_callback_url()
|
||||||
self.bot.set_callback(self.config['token'], callback_url)
|
self.bot.set_callback(self.config['token'], callback_url)
|
||||||
@@ -613,7 +598,7 @@ class GeWeChatAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise Exception(f'设置 Gewechat 回调失败,token失效:{e}')
|
raise Exception(f'设置 Gewechat 回调失败,token失效:{e}')
|
||||||
|
|
||||||
threading.Thread(target=gewechat_login_process).start()
|
threading.Thread(target=gewechat_init_process).start()
|
||||||
|
|
||||||
# 统一 webhook 模式下,不启动独立的 HTTP 服务
|
# 统一 webhook 模式下,不启动独立的 HTTP 服务
|
||||||
# 保持适配器运行
|
# 保持适配器运行
|
||||||
|
|||||||
Reference in New Issue
Block a user