feat: add supports for wecom

This commit is contained in:
wangcham
2025-01-12 05:09:53 -05:00
parent fd30022065
commit 60d4f3d77c
19 changed files with 939 additions and 58 deletions

View File

@@ -44,10 +44,10 @@ class Query(pydantic.BaseModel):
launcher_type: LauncherTypes
"""会话类型platform处理阶段设置"""
launcher_id: int
launcher_id: typing.Union[int, str]
"""会话IDplatform处理阶段设置"""
sender_id: int
sender_id: typing.Union[int, str]
"""发送者IDplatform处理阶段设置"""
message_event: platform_events.MessageEvent
@@ -113,9 +113,9 @@ class Session(pydantic.BaseModel):
"""会话,一个 Session 对应一个 {launcher_type.value}_{launcher_id}"""
launcher_type: LauncherTypes
launcher_id: int
launcher_id: typing.Union[int, str]
sender_id: typing.Optional[int] = 0
sender_id: typing.Optional[typing.Union[int, str]] = 0
use_prompt_name: typing.Optional[str] = 'default'

View File

@@ -68,7 +68,7 @@ class Controller:
except Exception as e:
# traceback.print_exc()
self.ap.logger.error(f"控制器循环出错: {e}")
self.ap.logger.debug(f"Traceback: {traceback.format_exc()}")
self.ap.logger.error(f"Traceback: {traceback.format_exc()}")
async def _check_output(self, query: entities.Query, result: pipeline_entities.StageProcessResult):
"""检查输出
@@ -163,29 +163,30 @@ class Controller:
async def process_query(self, query: entities.Query):
"""处理请求
"""
# ======== 触发 MessageReceived 事件 ========
event_type = events.PersonMessageReceived if query.launcher_type == entities.LauncherTypes.PERSON else events.GroupMessageReceived
event_ctx = await self.ap.plugin_mgr.emit_event(
event=event_type(
launcher_type=query.launcher_type.value,
launcher_id=query.launcher_id,
sender_id=query.sender_id,
message_chain=query.message_chain,
query=query
)
)
if event_ctx.is_prevented_default():
return
self.ap.logger.debug(f"Processing query {query}")
try:
# ======== 触发 MessageReceived 事件 ========
event_type = events.PersonMessageReceived if query.launcher_type == entities.LauncherTypes.PERSON else events.GroupMessageReceived
event_ctx = await self.ap.plugin_mgr.emit_event(
event=event_type(
launcher_type=query.launcher_type.value,
launcher_id=query.launcher_id,
sender_id=query.sender_id,
message_chain=query.message_chain,
query=query
)
)
if event_ctx.is_prevented_default():
return
self.ap.logger.debug(f"Processing query {query}")
await self._execute_from_stage(0, query)
except Exception as e:
self.ap.logger.error(f"处理请求时出错 query_id={query.query_id} stage={query.current_stage.inst_name} : {e}")
inst_name = query.current_stage.inst_name if query.current_stage else 'unknown'
self.ap.logger.error(f"处理请求时出错 query_id={query.query_id} stage={inst_name} : {e}")
self.ap.logger.debug(f"Traceback: {traceback.format_exc()}")
finally:
self.ap.logger.debug(f"Query {query} processed")

View File

@@ -1,7 +1,7 @@
from __future__ import annotations
import asyncio
import typing
from ..core import entities
from ..platform import adapter as msadapter
@@ -29,8 +29,8 @@ class QueryPool:
async def add_query(
self,
launcher_type: entities.LauncherTypes,
launcher_id: int,
sender_id: int,
launcher_id: typing.Union[int, str],
sender_id: typing.Union[int, str],
message_event: platform_events.MessageEvent,
message_chain: platform_message.MessageChain,
adapter: msadapter.MessageSourceAdapter

View File

@@ -31,7 +31,7 @@ class ReteLimitAlgo(metaclass=abc.ABCMeta):
pass
@abc.abstractmethod
async def require_access(self, launcher_type: str, launcher_id: int) -> bool:
async def require_access(self, launcher_type: str, launcher_id: typing.Union[int, str]) -> bool:
"""进入处理流程
这个方法对等待是友好的,意味着算法可以实现在这里等待一段时间以控制速率。
@@ -46,7 +46,7 @@ class ReteLimitAlgo(metaclass=abc.ABCMeta):
raise NotImplementedError
@abc.abstractmethod
async def release_access(self, launcher_type: str, launcher_id: int):
async def release_access(self, launcher_type: str, launcher_id: typing.Union[int, str]):
"""退出处理流程
Args:

View File

@@ -1,6 +1,7 @@
from __future__ import annotations
import asyncio
import time
import typing
from .. import algo
# 固定窗口算法
@@ -29,7 +30,7 @@ class FixedWindowAlgo(algo.ReteLimitAlgo):
self.containers_lock = asyncio.Lock()
self.containers = {}
async def require_access(self, launcher_type: str, launcher_id: int) -> bool:
async def require_access(self, launcher_type: str, launcher_id: typing.Union[int, str]) -> bool:
# 加锁,找容器
container: SessionContainer = None
@@ -83,5 +84,5 @@ class FixedWindowAlgo(algo.ReteLimitAlgo):
# 返回True
return True
async def release_access(self, launcher_type: str, launcher_id: int):
async def release_access(self, launcher_type: str, launcher_id: typing.Union[int, str]):
pass

View File

@@ -52,7 +52,7 @@ class MessageSourceAdapter(metaclass=abc.ABCMeta):
self.config = config
self.ap = ap
async def send_message(
async def send_message(
self,
target_type: str,
target_id: str,

View File

@@ -37,7 +37,7 @@ class PlatformManager:
async def initialize(self):
from .sources import nakuru, aiocqhttp, qqbotpy
from .sources import nakuru, aiocqhttp, qqbotpy,wecom
async def on_friend_message(event: platform_events.FriendMessage, adapter: msadapter.MessageSourceAdapter):

View File

@@ -0,0 +1,193 @@
from __future__ import annotations
import typing
import asyncio
import traceback
import time
import datetime
import aiocqhttp
import aiohttp
from libs.wecom_api.api import WecomClient
from pkg.platform.adapter import MessageSourceAdapter
from pkg.platform.types import events as platform_events, message as platform_message
from libs.wecom_api.wecomevent import WecomEvent
from pkg.core import app
from .. import adapter
from ...pipeline.longtext.strategies import forward
from ...core import app
from ..types import message as platform_message
from ..types import events as platform_events
from ..types import entities as platform_entities
from ...command.errors import ParamNotEnoughError
class WecomMessageConverter(adapter.MessageConverter):
@staticmethod
async def yiri2target(message_chain:platform_message.MessageChain):
content=''
for msg in message_chain:
if type(msg) is platform_message.Plain:
content+=msg.text
return content
@staticmethod
async def target2yiri(message:str,message_id:int = -1):
yiri_msg_list = []
yiri_msg_list.append(
platform_message.Source(id = message_id,time = datetime.datetime.now())
)
yiri_msg_list.append(platform_message.Plain(text=message))
chain = platform_message.MessageChain(yiri_msg_list)
return chain
class WecomEventConverter:
@staticmethod
async def yiri2target(event:platform_events.Event,bot_account_id:int) -> WecomEvent:
content = await WecomMessageConverter.yiri2target(event.message_chain)
if type(event) is platform_events.GroupMessage:
pass
if type(event) is platform_events.FriendMessage:
payload = {
"MsgType": "text",
"Content": content,
"FromUserName": event.sender.id,
"ToUserName": bot_account_id,
"CreateTime": int(datetime.datetime.now().timestamp()),
"AgentID": event.sender.nickname
}
wecom_event = WecomEvent.from_payload(payload=payload)
if not wecom_event:
raise ValueError("无法从 message_data 构造 WecomEvent 对象")
return wecom_event
@staticmethod
async def target2yiri(event: WecomEvent):
"""
将 WecomEvent 转换为平台的 FriendMessage 对象。
Args:
event (WecomEvent): 企业微信事件。
Returns:
platform_events.FriendMessage: 转换后的 FriendMessage 对象。
"""
# 转换消息链
yiri_chain = await WecomMessageConverter.target2yiri(
event.message, event.message_id
)
# 判断消息类型并进行转换
# if event.message_type == "private": 默认消息都是从好友发出
friend = platform_entities.Friend(
id=event.user_id,
nickname=str(event.agent_id),
remark="",
)
return platform_events.FriendMessage(
sender=friend,
message_chain=yiri_chain,
time=event.timestamp
)
@adapter.adapter_class("wecom")
class WecomeAdapter(adapter.MessageSourceAdapter):
bot:WecomClient
ap:app.Application
bot_account_id:str
message_converter:WecomMessageConverter = WecomMessageConverter()
event_converter:WecomEventConverter = WecomEventConverter()
config:dict
ap:app.Application
def __init__(self, config: dict, ap:app.Application):
self.config = config
#这里需要对config里的内容换成企业微信的config。是config:corpid,token......
self.ap = ap
required_keys = ["corpid","secret","token","EncodingAESKey","contacts_secret"]
missing_keys = [key for key in required_keys if key not in config]
if missing_keys:
raise ParamNotEnoughError("企业微信缺少相关配置项,请查看文档或联系管理员")
self.bot = WecomClient(
corpid=config['corpid'],
secret=config['secret'],
token=config['token'],
EncodingAESKey=config['EncodingAESKey'],
contacts_secret=config['contacts_secret']
)
async def reply_message(self,message_source:platform_events.MessageEvent,message:platform_message.MessageChain,
quote_origin:bool=False,
):
Wecom_event = await WecomEventConverter.yiri2target(message_source,self.bot_account_id)
Wecom_msg = await WecomMessageConverter.yiri2target(message)
# message_converter传回一个消息str
user_id = Wecom_event.user_id
agent_id = Wecom_event.agent_id
return await self.bot.send_private_msg(user_id=user_id,agent_id=agent_id,content=Wecom_msg)
async def send_message(self, target_type: str, target_id: str, message: platform_message.MessageChain):
pass
def register_listener(
self,
event_type:typing.Type[platform_events.Event],
callback:typing.Callable[[platform_events.Event,adapter.MessageSourceAdapter],None],
):
async def on_message(event:WecomEvent):
self.bot_account_id = event.receiver_id
try:
return await callback(await self.event_converter.target2yiri(event),self)
except:
traceback.print_exc()
if event_type == platform_events.FriendMessage:
self.bot.on_message("text")(on_message)
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(host=self.config['host'],port=self.config['port'],shutdown_trigger=shutdown_trigger_placeholder)
async def kill(self) -> bool:
return False
async def unregister_listener(self, event_type: type, callback: typing.Callable[[platform_events.Event, MessageSourceAdapter], None]):
return super().unregister_listener(event_type, callback)

View File

@@ -25,7 +25,7 @@ class Entity(pydantic.BaseModel):
class Friend(Entity):
"""好友。"""
id: int
id: typing.Union[int, str]
"""QQ 号。"""
nickname: typing.Optional[str]
"""昵称。"""
@@ -52,7 +52,7 @@ class Permission(str, Enum):
class Group(Entity):
"""群。"""
id: int
id: typing.Union[int, str]
"""群号。"""
name: str
"""群名称。"""
@@ -67,7 +67,7 @@ class Group(Entity):
class GroupMember(Entity):
"""群成员。"""
id: int
id: typing.Union[int, str]
"""QQ 号。"""
member_name: str
"""群成员名称。"""
@@ -92,7 +92,7 @@ class GroupMember(Entity):
class Client(Entity):
"""来自其他客户端的用户。"""
id: int
id: typing.Union[int, str]
"""识别 id。"""
platform: str
"""来源平台。"""
@@ -105,7 +105,7 @@ class Client(Entity):
class Subject(pydantic.BaseModel):
"""另一种实体类型表示。"""
id: int
id: typing.Union[int, str]
"""QQ 号或群号。"""
kind: typing.Literal['Friend', 'Group', 'Stranger']
"""类型。"""

View File

@@ -485,11 +485,11 @@ class Quote(MessageComponent):
"""消息组件类型。"""
id: typing.Optional[int] = None
"""被引用回复的原消息的 message_id。"""
group_id: typing.Optional[int] = None
group_id: typing.Optional[typing.Union[int, str]] = None
"""被引用回复的原消息所接收的群号当为好友消息时为0。"""
sender_id: typing.Optional[int] = None
sender_id: typing.Optional[typing.Union[int, str]] = None
"""被引用回复的原消息的发送者的QQ号。"""
target_id: typing.Optional[int] = None
target_id: typing.Optional[typing.Union[int, str]] = None
"""被引用回复的原消息的接收者者的QQ号或群号"""
origin: MessageChain
"""被引用回复的原消息的消息链对象。"""
@@ -749,7 +749,7 @@ class Voice(MessageComponent):
class ForwardMessageNode(pydantic.BaseModel):
"""合并转发中的一条消息。"""
sender_id: typing.Optional[int] = None
sender_id: typing.Optional[typing.Union[int, str]] = None
"""发送人QQ号。"""
sender_name: typing.Optional[str] = None
"""显示名称。"""

View File

@@ -25,10 +25,10 @@ class PersonMessageReceived(BaseEventModel):
launcher_type: str
"""发起对象类型(group/person)"""
launcher_id: int
launcher_id: typing.Union[int, str]
"""发起对象ID(群号/QQ号)"""
sender_id: int
sender_id: typing.Union[int, str]
"""发送者ID(QQ号)"""
message_chain: platform_message.MessageChain
@@ -39,9 +39,9 @@ class GroupMessageReceived(BaseEventModel):
launcher_type: str
launcher_id: int
launcher_id: typing.Union[int, str]
sender_id: int
sender_id: typing.Union[int, str]
message_chain: platform_message.MessageChain
@@ -51,9 +51,9 @@ class PersonNormalMessageReceived(BaseEventModel):
launcher_type: str
launcher_id: int
launcher_id: typing.Union[int, str]
sender_id: int
sender_id: typing.Union[int, str]
text_message: str
@@ -69,9 +69,9 @@ class PersonCommandSent(BaseEventModel):
launcher_type: str
launcher_id: int
launcher_id: typing.Union[int, str]
sender_id: int
sender_id: typing.Union[int, str]
command: str
@@ -93,9 +93,9 @@ class GroupNormalMessageReceived(BaseEventModel):
launcher_type: str
launcher_id: int
launcher_id: typing.Union[int, str]
sender_id: int
sender_id: typing.Union[int, str]
text_message: str
@@ -111,9 +111,9 @@ class GroupCommandSent(BaseEventModel):
launcher_type: str
launcher_id: int
launcher_id: typing.Union[int, str]
sender_id: int
sender_id: typing.Union[int, str]
command: str
@@ -135,9 +135,9 @@ class NormalMessageResponded(BaseEventModel):
launcher_type: str
launcher_id: int
launcher_id: typing.Union[int, str]
sender_id: int
sender_id: typing.Union[int, str]
session: core_entities.Session
"""会话对象"""