2025-04-16 15:02:01 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
import typing
|
|
|
|
|
import asyncio
|
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
|
|
import datetime
|
2025-06-16 13:18:59 +08:00
|
|
|
import pydantic
|
2025-04-16 15:02:01 +08:00
|
|
|
|
|
|
|
|
from libs.wecom_customer_service_api.api import WecomCSClient
|
2025-06-16 13:18:59 +08:00
|
|
|
import langbot_plugin.api.definition.abstract.platform.adapter as abstract_platform_adapter
|
2025-04-16 15:02:01 +08:00
|
|
|
from libs.wecom_customer_service_api.wecomcsevent import WecomCSEvent
|
2025-06-16 13:18:59 +08:00
|
|
|
import langbot_plugin.api.entities.builtin.platform.entities as platform_entities
|
|
|
|
|
import langbot_plugin.api.entities.builtin.platform.message as platform_message
|
|
|
|
|
import langbot_plugin.api.entities.builtin.platform.events as platform_events
|
2025-04-16 15:02:01 +08:00
|
|
|
from ...command.errors import ParamNotEnoughError
|
2025-06-16 13:18:59 +08:00
|
|
|
import langbot_plugin.api.definition.abstract.platform.event_logger as abstract_platform_logger
|
2025-04-16 15:02:01 +08:00
|
|
|
|
|
|
|
|
|
2025-06-16 13:18:59 +08:00
|
|
|
class WecomMessageConverter(abstract_platform_adapter.AbstractMessageConverter):
|
2025-04-16 15:02:01 +08:00
|
|
|
@staticmethod
|
2025-05-10 18:04:58 +08:00
|
|
|
async def yiri2target(message_chain: platform_message.MessageChain, bot: WecomCSClient):
|
2025-04-16 15:02:01 +08:00
|
|
|
content_list = []
|
|
|
|
|
|
|
|
|
|
for msg in message_chain:
|
|
|
|
|
if type(msg) is platform_message.Plain:
|
2025-05-10 18:04:58 +08:00
|
|
|
content_list.append(
|
|
|
|
|
{
|
|
|
|
|
'type': 'text',
|
|
|
|
|
'content': msg.text,
|
|
|
|
|
}
|
|
|
|
|
)
|
2025-04-16 15:02:01 +08:00
|
|
|
elif type(msg) is platform_message.Image:
|
2025-05-10 18:04:58 +08:00
|
|
|
content_list.append(
|
|
|
|
|
{
|
|
|
|
|
'type': 'image',
|
|
|
|
|
'media_id': await bot.get_media_id(msg),
|
|
|
|
|
}
|
|
|
|
|
)
|
2025-04-16 15:02:01 +08:00
|
|
|
elif type(msg) is platform_message.Forward:
|
|
|
|
|
for node in msg.node_list:
|
|
|
|
|
content_list.extend((await WecomMessageConverter.yiri2target(node.message_chain, bot)))
|
|
|
|
|
else:
|
2025-05-10 18:04:58 +08:00
|
|
|
content_list.append(
|
|
|
|
|
{
|
|
|
|
|
'type': 'text',
|
|
|
|
|
'content': str(msg),
|
|
|
|
|
}
|
|
|
|
|
)
|
2025-04-16 15:02:01 +08:00
|
|
|
|
|
|
|
|
return content_list
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
async def target2yiri(message: str, message_id: int = -1):
|
|
|
|
|
yiri_msg_list = []
|
2025-05-10 18:04:58 +08:00
|
|
|
yiri_msg_list.append(platform_message.Source(id=message_id, time=datetime.datetime.now()))
|
2025-04-16 15:02:01 +08:00
|
|
|
|
|
|
|
|
yiri_msg_list.append(platform_message.Plain(text=message))
|
|
|
|
|
chain = platform_message.MessageChain(yiri_msg_list)
|
|
|
|
|
|
|
|
|
|
return chain
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
async def target2yiri_image(picurl: str, message_id: int = -1):
|
|
|
|
|
yiri_msg_list = []
|
2025-05-10 18:04:58 +08:00
|
|
|
yiri_msg_list.append(platform_message.Source(id=message_id, time=datetime.datetime.now()))
|
2025-04-16 15:02:01 +08:00
|
|
|
yiri_msg_list.append(platform_message.Image(base64=picurl))
|
|
|
|
|
chain = platform_message.MessageChain(yiri_msg_list)
|
2025-05-10 18:04:58 +08:00
|
|
|
|
2025-04-16 15:02:01 +08:00
|
|
|
return chain
|
|
|
|
|
|
|
|
|
|
|
2025-06-16 13:18:59 +08:00
|
|
|
class WecomEventConverter(abstract_platform_adapter.AbstractEventConverter):
|
2025-04-16 15:02:01 +08:00
|
|
|
@staticmethod
|
2025-05-10 18:04:58 +08:00
|
|
|
async def yiri2target(event: platform_events.Event, bot_account_id: int, bot: WecomCSClient) -> WecomCSEvent:
|
2025-04-16 15:02:01 +08:00
|
|
|
# only for extracting user information
|
|
|
|
|
|
|
|
|
|
if type(event) is platform_events.GroupMessage:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
if type(event) is platform_events.FriendMessage:
|
|
|
|
|
return event.source_platform_object
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
async def target2yiri(event: WecomCSEvent):
|
|
|
|
|
"""
|
|
|
|
|
将 WecomEvent 转换为平台的 FriendMessage 对象。
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
event (WecomEvent): 企业微信客服事件。
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
platform_events.FriendMessage: 转换后的 FriendMessage 对象。
|
|
|
|
|
"""
|
|
|
|
|
# 转换消息链
|
2025-05-10 18:04:58 +08:00
|
|
|
if event.type == 'text':
|
|
|
|
|
yiri_chain = await WecomMessageConverter.target2yiri(event.message, event.message_id)
|
2025-04-16 15:02:01 +08:00
|
|
|
friend = platform_entities.Friend(
|
2025-05-10 18:04:58 +08:00
|
|
|
id=f'u{event.user_id}',
|
2025-04-16 15:02:01 +08:00
|
|
|
nickname=str(event.user_id),
|
2025-05-10 18:04:58 +08:00
|
|
|
remark='',
|
2025-04-16 15:02:01 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return platform_events.FriendMessage(
|
|
|
|
|
sender=friend, message_chain=yiri_chain, time=event.timestamp, source_platform_object=event
|
|
|
|
|
)
|
2025-05-10 18:04:58 +08:00
|
|
|
elif event.type == 'image':
|
2025-04-16 15:02:01 +08:00
|
|
|
friend = platform_entities.Friend(
|
2025-05-10 18:04:58 +08:00
|
|
|
id=f'u{event.user_id}',
|
2025-04-16 15:02:01 +08:00
|
|
|
nickname=str(event.user_id),
|
2025-05-10 18:04:58 +08:00
|
|
|
remark='',
|
2025-04-16 15:02:01 +08:00
|
|
|
)
|
|
|
|
|
|
2025-05-10 18:04:58 +08:00
|
|
|
yiri_chain = await WecomMessageConverter.target2yiri_image(picurl=event.picurl, message_id=event.message_id)
|
2025-04-16 15:02:01 +08:00
|
|
|
|
|
|
|
|
return platform_events.FriendMessage(
|
|
|
|
|
sender=friend, message_chain=yiri_chain, time=event.timestamp, source_platform_object=event
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2025-06-16 13:18:59 +08:00
|
|
|
class WecomCSAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter):
|
|
|
|
|
bot: WecomCSClient = pydantic.Field(exclude=True)
|
2025-04-16 15:02:01 +08:00
|
|
|
message_converter: WecomMessageConverter = WecomMessageConverter()
|
|
|
|
|
event_converter: WecomEventConverter = WecomEventConverter()
|
|
|
|
|
|
2025-06-16 13:18:59 +08:00
|
|
|
def __init__(self, config: dict, logger: abstract_platform_logger.AbstractEventLogger):
|
2025-04-16 15:02:01 +08:00
|
|
|
required_keys = [
|
2025-05-10 18:04:58 +08:00
|
|
|
'corpid',
|
|
|
|
|
'secret',
|
|
|
|
|
'token',
|
|
|
|
|
'EncodingAESKey',
|
2025-04-16 15:02:01 +08:00
|
|
|
]
|
|
|
|
|
missing_keys = [key for key in required_keys if key not in config]
|
|
|
|
|
if missing_keys:
|
2025-05-10 18:04:58 +08:00
|
|
|
raise ParamNotEnoughError('企业微信客服缺少相关配置项,请查看文档或联系管理员')
|
2025-04-16 15:02:01 +08:00
|
|
|
|
2025-06-16 13:18:59 +08:00
|
|
|
bot = WecomCSClient(
|
2025-05-10 18:04:58 +08:00
|
|
|
corpid=config['corpid'],
|
|
|
|
|
secret=config['secret'],
|
|
|
|
|
token=config['token'],
|
|
|
|
|
EncodingAESKey=config['EncodingAESKey'],
|
2025-06-16 13:18:59 +08:00
|
|
|
logger=logger,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
super().__init__(
|
|
|
|
|
config=config,
|
|
|
|
|
logger=logger,
|
|
|
|
|
bot=bot,
|
|
|
|
|
bot_account_id='',
|
|
|
|
|
listeners={},
|
2025-04-16 15:02:01 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
async def reply_message(
|
|
|
|
|
self,
|
|
|
|
|
message_source: platform_events.MessageEvent,
|
|
|
|
|
message: platform_message.MessageChain,
|
|
|
|
|
quote_origin: bool = False,
|
|
|
|
|
):
|
2025-05-10 18:04:58 +08:00
|
|
|
Wecom_event = await WecomEventConverter.yiri2target(message_source, self.bot_account_id, self.bot)
|
2025-04-16 15:02:01 +08:00
|
|
|
content_list = await WecomMessageConverter.yiri2target(message, self.bot)
|
2025-05-10 18:04:58 +08:00
|
|
|
|
2025-04-16 15:02:01 +08:00
|
|
|
for content in content_list:
|
2025-05-10 18:04:58 +08:00
|
|
|
if content['type'] == 'text':
|
|
|
|
|
await self.bot.send_text_msg(
|
|
|
|
|
open_kfid=Wecom_event.receiver_id,
|
|
|
|
|
external_userid=Wecom_event.user_id,
|
|
|
|
|
msgid=Wecom_event.message_id,
|
|
|
|
|
content=content['content'],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
async def send_message(self, target_type: str, target_id: str, message: platform_message.MessageChain):
|
2025-04-16 15:02:01 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def register_listener(
|
|
|
|
|
self,
|
|
|
|
|
event_type: typing.Type[platform_events.Event],
|
2025-06-16 13:18:59 +08:00
|
|
|
callback: typing.Callable[
|
|
|
|
|
[platform_events.Event, abstract_platform_adapter.AbstractMessagePlatformAdapter], None
|
|
|
|
|
],
|
2025-04-16 15:02:01 +08:00
|
|
|
):
|
|
|
|
|
async def on_message(event: WecomCSEvent):
|
|
|
|
|
self.bot_account_id = event.receiver_id
|
|
|
|
|
try:
|
2025-05-10 18:04:58 +08:00
|
|
|
return await callback(await self.event_converter.target2yiri(event), self)
|
2025-06-15 12:51:51 +08:00
|
|
|
except Exception:
|
|
|
|
|
await self.logger.error(f'Error in wecomcs callback: {traceback.format_exc()}')
|
2025-04-16 15:02:01 +08:00
|
|
|
|
|
|
|
|
if event_type == platform_events.FriendMessage:
|
2025-05-10 18:04:58 +08:00
|
|
|
self.bot.on_message('text')(on_message)
|
|
|
|
|
self.bot.on_message('image')(on_message)
|
2025-04-16 15:02:01 +08:00
|
|
|
elif event_type == platform_events.GroupMessage:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
async def run_async(self):
|
|
|
|
|
async def shutdown_trigger_placeholder():
|
|
|
|
|
while True:
|
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
|
|
|
|
|
|
await self.bot.run_task(
|
2025-05-10 18:04:58 +08:00
|
|
|
host='0.0.0.0',
|
|
|
|
|
port=self.config['port'],
|
2025-04-16 15:02:01 +08:00
|
|
|
shutdown_trigger=shutdown_trigger_placeholder,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
async def kill(self) -> bool:
|
|
|
|
|
return False
|
|
|
|
|
|
2025-06-16 13:18:59 +08:00
|
|
|
async def is_muted(self, group_id: int) -> bool:
|
|
|
|
|
return False
|
|
|
|
|
|
2025-04-16 15:02:01 +08:00
|
|
|
async def unregister_listener(
|
|
|
|
|
self,
|
|
|
|
|
event_type: type,
|
2025-06-16 13:18:59 +08:00
|
|
|
callback: typing.Callable[
|
|
|
|
|
[platform_events.Event, abstract_platform_adapter.AbstractMessagePlatformAdapter], None
|
|
|
|
|
],
|
2025-04-16 15:02:01 +08:00
|
|
|
):
|
2025-05-10 18:04:58 +08:00
|
|
|
return super().unregister_listener(event_type, callback)
|