mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-07-24 21:36:06 +00:00
Merge pull request #1664 from langbot-app/fix/wecom
fix: wecom function
This commit is contained in:
@@ -340,3 +340,4 @@ class WecomClient:
|
|||||||
async def get_media_id(self, image: platform_message.Image):
|
async def get_media_id(self, image: platform_message.Image):
|
||||||
media_id = await self.upload_to_work(image=image)
|
media_id = await self.upload_to_work(image=image)
|
||||||
return media_id
|
return media_id
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ from __future__ import annotations
|
|||||||
import typing
|
import typing
|
||||||
import asyncio
|
import asyncio
|
||||||
import traceback
|
import traceback
|
||||||
|
import pydantic
|
||||||
import datetime
|
import datetime
|
||||||
import langbot_plugin.api.definition.abstract.platform.adapter as abstract_platform_adapter
|
import langbot_plugin.api.definition.abstract.platform.adapter as abstract_platform_adapter
|
||||||
from libs.official_account_api.oaevent import OAEvent
|
from libs.official_account_api.oaevent import OAEvent
|
||||||
@@ -56,47 +56,51 @@ class OAEventConverter(abstract_platform_adapter.AbstractEventConverter):
|
|||||||
|
|
||||||
|
|
||||||
class OfficialAccountAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
class OfficialAccountAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
||||||
bot: OAClient | OAClientForLongerResponse
|
|
||||||
bot_account_id: str
|
|
||||||
message_converter: OAMessageConverter = OAMessageConverter()
|
message_converter: OAMessageConverter = OAMessageConverter()
|
||||||
event_converter: OAEventConverter = OAEventConverter()
|
event_converter: OAEventConverter = OAEventConverter()
|
||||||
config: dict
|
bot: typing.Union[OAClient, OAClientForLongerResponse] = pydantic.Field(exclude=True)
|
||||||
|
|
||||||
def __init__(self, config: dict, logger: EventLogger):
|
def __init__(self, config: dict, logger: EventLogger):
|
||||||
self.config = config
|
required_keys = ['token', 'EncodingAESKey', 'AppSecret', 'AppID', 'Mode']
|
||||||
self.logger = logger
|
missing_keys = [k for k in required_keys if k not in config]
|
||||||
|
|
||||||
required_keys = [
|
|
||||||
'token',
|
|
||||||
'EncodingAESKey',
|
|
||||||
'AppSecret',
|
|
||||||
'AppID',
|
|
||||||
'Mode',
|
|
||||||
]
|
|
||||||
missing_keys = [key for key in required_keys if key not in config]
|
|
||||||
if missing_keys:
|
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'],
|
token=config['token'],
|
||||||
EncodingAESKey=config['EncodingAESKey'],
|
EncodingAESKey=config['EncodingAESKey'],
|
||||||
Appsecret=config['AppSecret'],
|
Appsecret=config['AppSecret'],
|
||||||
AppID=config['AppID'],
|
AppID=config['AppID'],
|
||||||
logger=self.logger,
|
logger=logger,
|
||||||
)
|
)
|
||||||
elif self.config['Mode'] == 'passive':
|
elif config['Mode'] == 'passive':
|
||||||
self.bot = OAClientForLongerResponse(
|
bot = OAClientForLongerResponse(
|
||||||
token=config['token'],
|
token=config['token'],
|
||||||
EncodingAESKey=config['EncodingAESKey'],
|
EncodingAESKey=config['EncodingAESKey'],
|
||||||
Appsecret=config['AppSecret'],
|
Appsecret=config['AppSecret'],
|
||||||
AppID=config['AppID'],
|
AppID=config['AppID'],
|
||||||
LoadingMessage=config['LoadingMessage'],
|
LoadingMessage=config.get('LoadingMessage', ''),
|
||||||
logger=self.logger,
|
logger=logger,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
raise KeyError('请设置微信公众号通信模式')
|
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(
|
async def reply_message(
|
||||||
self,
|
self,
|
||||||
message_source: platform_events.FriendMessage,
|
message_source: platform_events.FriendMessage,
|
||||||
@@ -154,3 +158,6 @@ class OfficialAccountAdapter(abstract_platform_adapter.AbstractMessagePlatformAd
|
|||||||
],
|
],
|
||||||
):
|
):
|
||||||
return super().unregister_listener(event_type, callback)
|
return super().unregister_listener(event_type, callback)
|
||||||
|
|
||||||
|
async def is_muted(self, group_id: str, ) -> bool:
|
||||||
|
pass
|
||||||
|
|||||||
@@ -134,9 +134,7 @@ class WecomAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
|||||||
config: dict
|
config: dict
|
||||||
|
|
||||||
def __init__(self, config: dict, logger: EventLogger):
|
def __init__(self, config: dict, logger: EventLogger):
|
||||||
self.config = config
|
# 校验必填项
|
||||||
self.logger = logger
|
|
||||||
|
|
||||||
required_keys = [
|
required_keys = [
|
||||||
'corpid',
|
'corpid',
|
||||||
'secret',
|
'secret',
|
||||||
@@ -146,17 +144,27 @@ class WecomAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
|||||||
]
|
]
|
||||||
missing_keys = [key for key in required_keys if key not in config]
|
missing_keys = [key for key in required_keys if key not in config]
|
||||||
if missing_keys:
|
if missing_keys:
|
||||||
raise command_errors.ParamNotEnoughError('企业微信缺少相关配置项,请查看文档或联系管理员')
|
raise Exception(f'Wecom 缺少配置项: {missing_keys}')
|
||||||
|
|
||||||
self.bot = WecomClient(
|
# 创建运行时 bot 对象
|
||||||
|
bot = WecomClient(
|
||||||
corpid=config['corpid'],
|
corpid=config['corpid'],
|
||||||
secret=config['secret'],
|
secret=config['secret'],
|
||||||
token=config['token'],
|
token=config['token'],
|
||||||
EncodingAESKey=config['EncodingAESKey'],
|
EncodingAESKey=config['EncodingAESKey'],
|
||||||
contacts_secret=config['contacts_secret'],
|
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(
|
async def reply_message(
|
||||||
self,
|
self,
|
||||||
message_source: platform_events.MessageEvent,
|
message_source: platform_events.MessageEvent,
|
||||||
@@ -231,3 +239,6 @@ class WecomAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
|||||||
],
|
],
|
||||||
):
|
):
|
||||||
return super().unregister_listener(event_type, callback)
|
return super().unregister_listener(event_type, callback)
|
||||||
|
|
||||||
|
async def is_muted(self, group_id: int) -> bool:
|
||||||
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user