diff --git a/libs/wecom_api/api.py b/libs/wecom_api/api.py index 352a550c..d9cb7726 100644 --- a/libs/wecom_api/api.py +++ b/libs/wecom_api/api.py @@ -340,3 +340,4 @@ class WecomClient: async def get_media_id(self, image: platform_message.Image): media_id = await self.upload_to_work(image=image) return media_id + diff --git a/pkg/platform/sources/officialaccount.py b/pkg/platform/sources/officialaccount.py index 01a2c868..1f70f3eb 100644 --- a/pkg/platform/sources/officialaccount.py +++ b/pkg/platform/sources/officialaccount.py @@ -2,7 +2,7 @@ from __future__ import annotations import typing import asyncio import traceback - +import pydantic import datetime import langbot_plugin.api.definition.abstract.platform.adapter as abstract_platform_adapter from libs.official_account_api.oaevent import OAEvent @@ -56,47 +56,51 @@ class OAEventConverter(abstract_platform_adapter.AbstractEventConverter): class OfficialAccountAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter): - bot: OAClient | OAClientForLongerResponse - bot_account_id: str message_converter: OAMessageConverter = OAMessageConverter() event_converter: OAEventConverter = OAEventConverter() - config: dict + bot: typing.Union[OAClient, OAClientForLongerResponse] = pydantic.Field(exclude=True) def __init__(self, config: dict, logger: EventLogger): - self.config = config - self.logger = logger - - required_keys = [ - 'token', - 'EncodingAESKey', - 'AppSecret', - 'AppID', - 'Mode', - ] - missing_keys = [key for key in required_keys if key not in config] + required_keys = ['token', 'EncodingAESKey', 'AppSecret', 'AppID', 'Mode'] + missing_keys = [k for k in required_keys if k not in config] if missing_keys: - raise command_errors.ParamNotEnoughError('微信公众号缺少相关配置项,请查看文档或联系管理员') + raise Exception(f'OfficialAccount 缺少配置项: {missing_keys}') - if self.config['Mode'] == 'drop': - self.bot = OAClient( + + if config['Mode'] == 'drop': + bot = OAClient( token=config['token'], EncodingAESKey=config['EncodingAESKey'], Appsecret=config['AppSecret'], AppID=config['AppID'], - logger=self.logger, + logger=logger, ) - elif self.config['Mode'] == 'passive': - self.bot = OAClientForLongerResponse( + elif config['Mode'] == 'passive': + bot = OAClientForLongerResponse( token=config['token'], EncodingAESKey=config['EncodingAESKey'], Appsecret=config['AppSecret'], AppID=config['AppID'], - LoadingMessage=config['LoadingMessage'], - logger=self.logger, + LoadingMessage=config.get('LoadingMessage', ''), + logger=logger, ) else: raise KeyError('请设置微信公众号通信模式') + bot_account_id = config.get('AppID', '') + + + super().__init__( + bot=bot, + bot_account_id=bot_account_id, + config=config, + logger=logger, + ) + + + + + async def reply_message( self, message_source: platform_events.FriendMessage, @@ -154,3 +158,6 @@ class OfficialAccountAdapter(abstract_platform_adapter.AbstractMessagePlatformAd ], ): return super().unregister_listener(event_type, callback) + + async def is_muted(self, group_id: str, ) -> bool: + pass diff --git a/pkg/platform/sources/wecom.py b/pkg/platform/sources/wecom.py index 392db801..3f5c0676 100644 --- a/pkg/platform/sources/wecom.py +++ b/pkg/platform/sources/wecom.py @@ -134,9 +134,7 @@ class WecomAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter): config: dict def __init__(self, config: dict, logger: EventLogger): - self.config = config - self.logger = logger - + # 校验必填项 required_keys = [ 'corpid', 'secret', @@ -146,17 +144,27 @@ class WecomAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter): ] missing_keys = [key for key in required_keys if key not in config] if missing_keys: - raise command_errors.ParamNotEnoughError('企业微信缺少相关配置项,请查看文档或联系管理员') + raise Exception(f'Wecom 缺少配置项: {missing_keys}') - self.bot = WecomClient( + # 创建运行时 bot 对象 + bot = WecomClient( corpid=config['corpid'], secret=config['secret'], token=config['token'], EncodingAESKey=config['EncodingAESKey'], contacts_secret=config['contacts_secret'], - logger=self.logger, + logger=logger, ) + + super().__init__( + config=config, + logger=logger, + bot=bot, + bot_account_id="", + ) + + async def reply_message( self, message_source: platform_events.MessageEvent, @@ -231,3 +239,6 @@ class WecomAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter): ], ): return super().unregister_listener(event_type, callback) + + async def is_muted(self, group_id: int) -> bool: + pass