本次提交包含多项优化与新增功能: 1. 清理多个文件中多余的空行与导入顺序 2. 修复voice.py中的多行字符串格式化问题 3. 新增微信公众号被动回复构建函数与配置项 4. 新增企业微信markdown消息发送支持 5. 新增消息去重TTL与最大条目配置 6. 新增markdown文本截断工具函数 7. 新增微信授权与OAuth相关工具方法 8. 重构消息去重逻辑,使用DedupPolicy替代本地字典实现 9. 新增子账号多租户支持功能 10. 新增消息动作处理适配器,支持send/reply等操作 11. 修复token持久化逻辑,新增状态存储支持
315 lines
16 KiB
Python
315 lines
16 KiB
Python
from __future__ import annotations
|
|
|
|
from collections.abc import Awaitable, Callable
|
|
from typing import TYPE_CHECKING, Any
|
|
|
|
if TYPE_CHECKING:
|
|
from yuxi.channels.message_actions import ActionStatus, MessageAction
|
|
|
|
WECHAT_MESSAGE_ACTIONS: dict[str, dict[str, Any]] = {
|
|
"send": {"status": "implemented", "impl": "adapter.send()"},
|
|
"broadcast": {"status": "unsupported", "reason": "WeChat API doesn't support broadcast"},
|
|
"reply": {"status": "implemented", "impl": "adapter.py (bridge reply_to_mode + wecom/mp reply quoting)"},
|
|
"sendWithEffect": {"status": "unsupported", "reason": "WeChat doesn't support message effects"},
|
|
"sendAttachment": {"status": "implemented", "impl": "adapter.send_media()"},
|
|
"sticker": {"status": "unsupported", "reason": "WeChat doesn't support stickers"},
|
|
"sticker-search": {"status": "unsupported", "reason": "WeChat doesn't support stickers"},
|
|
"sticker-upload": {"status": "unsupported", "reason": "WeChat doesn't support stickers"},
|
|
"edit": {"status": "unsupported", "reason": "WeChat API doesn't support message editing"},
|
|
"unsend": {"status": "unsupported", "reason": "WeChat API doesn't support unsend"},
|
|
"delete": {"status": "unsupported", "reason": "WeChat API doesn't support delete"},
|
|
"read": {"status": "implemented", "impl": "message_read.py"},
|
|
"pin": {"status": "unsupported", "reason": "WeChat doesn't support pin"},
|
|
"unpin": {"status": "unsupported", "reason": "WeChat doesn't support pin"},
|
|
"list-pins": {"status": "unsupported", "reason": "WeChat doesn't support pin"},
|
|
"permissions": {"status": "unsupported", "reason": "WeChat doesn't support permission API"},
|
|
"timeout": {"status": "unsupported", "reason": "WeChat doesn't support timeout/kick"},
|
|
"kick": {"status": "unsupported", "reason": "WeChat doesn't support kick API"},
|
|
"ban": {"status": "unsupported", "reason": "WeChat doesn't support ban API"},
|
|
"react": {"status": "unsupported", "reason": "WeChat doesn't support Reaction"},
|
|
"reactions": {"status": "unsupported", "reason": "WeChat doesn't support Reaction"},
|
|
"poll": {"status": "unsupported", "reason": "WeChat doesn't support Poll"},
|
|
"poll-vote": {"status": "unsupported", "reason": "WeChat doesn't support Poll"},
|
|
"emoji-list": {"status": "unsupported", "reason": "WeChat doesn't support Emoji API"},
|
|
"emoji-upload": {"status": "unsupported", "reason": "WeChat doesn't support Emoji API"},
|
|
"voice-status": {"status": "unsupported", "reason": "WeChat doesn't support voice status"},
|
|
"renameGroup": {"status": "implemented", "impl": "group_admin.py"},
|
|
"setGroupIcon": {"status": "unsupported", "reason": "WeCom API doesn't support group icon change"},
|
|
"addParticipant": {"status": "implemented", "impl": "group_admin.py"},
|
|
"removeParticipant": {"status": "implemented", "impl": "group_admin.py"},
|
|
"leaveGroup": {"status": "unsupported", "reason": "WeCom doesn't support leave group via API"},
|
|
"channel-info": {"status": "unsupported", "reason": "WeChat doesn't support channel concept"},
|
|
"channel-list": {"status": "unsupported", "reason": "WeChat doesn't support channel concept"},
|
|
"channel-create": {"status": "unsupported", "reason": "WeChat doesn't support channel concept"},
|
|
"channel-edit": {"status": "unsupported", "reason": "WeChat doesn't support channel concept"},
|
|
"channel-delete": {"status": "unsupported", "reason": "WeChat doesn't support channel concept"},
|
|
"channel-move": {"status": "unsupported", "reason": "WeChat doesn't support channel concept"},
|
|
"category-create": {"status": "unsupported", "reason": "WeChat doesn't support category concept"},
|
|
"category-edit": {"status": "unsupported", "reason": "WeChat doesn't support category concept"},
|
|
"category-delete": {"status": "unsupported", "reason": "WeChat doesn't support category concept"},
|
|
"topic-create": {"status": "unsupported", "reason": "WeChat doesn't support topic concept"},
|
|
"topic-edit": {"status": "unsupported", "reason": "WeChat doesn't support topic concept"},
|
|
"thread-create": {"status": "unsupported", "reason": "WeChat doesn't support thread concept"},
|
|
"thread-list": {"status": "unsupported", "reason": "WeChat doesn't support thread concept"},
|
|
"thread-reply": {"status": "unsupported", "reason": "WeChat doesn't support thread concept"},
|
|
"member-info": {"status": "unsupported", "reason": "WeChat doesn't support member info API"},
|
|
"role-info": {"status": "unsupported", "reason": "WeChat doesn't support role concept"},
|
|
"role-add": {"status": "unsupported", "reason": "WeChat doesn't support role concept"},
|
|
"role-remove": {"status": "unsupported", "reason": "WeChat doesn't support role concept"},
|
|
"event-list": {"status": "unsupported", "reason": "WeChat doesn't support event API"},
|
|
"event-create": {"status": "unsupported", "reason": "WeChat doesn't support event API"},
|
|
"download-file": {"status": "implemented", "impl": "adapter.download_media()"},
|
|
"upload-file": {"status": "implemented", "impl": "adapter.send_media()"},
|
|
"search": {"status": "unsupported", "reason": "WeChat doesn't support message search API"},
|
|
"set-profile": {"status": "unsupported", "reason": "WeChat doesn't support profile API"},
|
|
"set-presence": {"status": "unsupported", "reason": "WeChat doesn't support presence API"},
|
|
"template_card": {"status": "implemented", "impl": "template_card.py"},
|
|
}
|
|
|
|
|
|
class WeChatMessageActionAdapter:
|
|
def __init__(self, adapter: Any = None):
|
|
self._adapter = adapter
|
|
|
|
@staticmethod
|
|
def is_supported(action_name: str) -> bool:
|
|
info = WECHAT_MESSAGE_ACTIONS.get(action_name, {})
|
|
return info.get("status") == "implemented"
|
|
|
|
def get_handler(self, action_name: str) -> Callable[..., Awaitable[Any]] | None:
|
|
if self._adapter is None:
|
|
return None
|
|
return self._build_handler(action_name)
|
|
|
|
def _build_handler(self, action_name: str) -> Callable[..., Awaitable[Any]] | None:
|
|
adapter = self._adapter
|
|
|
|
async def _send(**kwargs: Any) -> Any:
|
|
chat_id = kwargs.get("chat_id", "")
|
|
content = kwargs.get("content", "")
|
|
if not chat_id or not content:
|
|
return {"success": False, "error": "missing chat_id or content"}
|
|
from yuxi.channels.models import ChannelIdentity, ChannelResponse
|
|
|
|
identity = ChannelIdentity(
|
|
channel_id=adapter.channel_id,
|
|
channel_type=adapter.channel_type,
|
|
channel_chat_id=chat_id,
|
|
channel_user_id=kwargs.get("channel_user_id", ""),
|
|
)
|
|
response = ChannelResponse(identity=identity, content=content)
|
|
result = await adapter.send(response)
|
|
return {"success": result.success, "error": result.error}
|
|
|
|
async def _reply(**kwargs: Any) -> Any:
|
|
chat_id = kwargs.get("chat_id", "")
|
|
content = kwargs.get("content", "")
|
|
reply_to_msg_id = kwargs.get("reply_to_msg_id", "")
|
|
if not chat_id or not content:
|
|
return {"success": False, "error": "missing chat_id or content"}
|
|
from yuxi.channels.models import ChannelIdentity, ChannelResponse
|
|
|
|
identity = ChannelIdentity(
|
|
channel_id=adapter.channel_id,
|
|
channel_type=adapter.channel_type,
|
|
channel_chat_id=chat_id,
|
|
channel_user_id=kwargs.get("channel_user_id", ""),
|
|
)
|
|
response = ChannelResponse(
|
|
identity=identity,
|
|
content=content,
|
|
reply_to_message_id=reply_to_msg_id,
|
|
metadata={
|
|
"reply_to_channel_user_id": kwargs.get("reply_to_channel_user_id", ""),
|
|
"sender_wxid": kwargs.get("sender_wxid"),
|
|
"chat_type": kwargs.get("chat_type", "direct"),
|
|
},
|
|
)
|
|
result = await adapter.send(response)
|
|
return {"success": result.success, "error": result.error}
|
|
|
|
async def _send_attachment(**kwargs: Any) -> Any:
|
|
chat_id = kwargs.get("chat_id", "")
|
|
media_type = kwargs.get("media_type", "file")
|
|
data = kwargs.get("data")
|
|
if not chat_id or data is None:
|
|
return {"success": False, "error": "missing chat_id or data"}
|
|
result = await adapter.send_media(chat_id, media_type, data)
|
|
return {"success": result.success, "error": result.error}
|
|
|
|
async def _download_file(**kwargs: Any) -> Any:
|
|
file_id = kwargs.get("file_id", "")
|
|
if not file_id:
|
|
return {"success": False, "error": "missing file_id"}
|
|
try:
|
|
data = await adapter.download_media(file_id)
|
|
return {"success": True, "data": data}
|
|
except Exception as e:
|
|
return {"success": False, "error": str(e)}
|
|
|
|
async def _upload_file(**kwargs: Any) -> Any:
|
|
chat_id = kwargs.get("chat_id", "")
|
|
data = kwargs.get("data")
|
|
media_type = kwargs.get("media_type", "file")
|
|
if not chat_id or data is None:
|
|
return {"success": False, "error": "missing chat_id or data"}
|
|
result = await adapter.send_media(chat_id, media_type, data)
|
|
return {"success": result.success, "error": result.error}
|
|
|
|
async def _read(**kwargs: Any) -> Any:
|
|
msg_id = kwargs.get("msg_id", "")
|
|
if not msg_id:
|
|
return {"success": False, "error": "missing msg_id"}
|
|
result = await adapter.read_message(msg_id)
|
|
return result
|
|
|
|
async def _rename_group(**kwargs: Any) -> Any:
|
|
chat_id = kwargs.get("chat_id", "")
|
|
name = kwargs.get("name", "")
|
|
if not chat_id or not name:
|
|
return {"success": False, "error": "missing chat_id or name"}
|
|
if adapter._mode != "wecom" or not adapter._wecom_client or not adapter._http_client:
|
|
return {"success": False, "error": "rename group only supported in WeCom mode"}
|
|
result = await adapter._group_admin.rename_group(adapter._wecom_client, adapter._http_client, chat_id, name)
|
|
return {"success": result.success, "error": result.error}
|
|
|
|
async def _add_participant(**kwargs: Any) -> Any:
|
|
chat_id = kwargs.get("chat_id", "")
|
|
user_id = kwargs.get("user_id", "")
|
|
if not chat_id or not user_id:
|
|
return {"success": False, "error": "missing chat_id or user_id"}
|
|
if adapter._mode != "wecom" or not adapter._wecom_client or not adapter._http_client:
|
|
return {"success": False, "error": "add participant only supported in WeCom mode"}
|
|
result = await adapter._group_admin.add_participant(
|
|
adapter._wecom_client, adapter._http_client, chat_id, user_id
|
|
)
|
|
return {"success": result.success, "error": result.error}
|
|
|
|
async def _remove_participant(**kwargs: Any) -> Any:
|
|
chat_id = kwargs.get("chat_id", "")
|
|
user_id = kwargs.get("user_id", "")
|
|
if not chat_id or not user_id:
|
|
return {"success": False, "error": "missing chat_id or user_id"}
|
|
if adapter._mode != "wecom" or not adapter._wecom_client or not adapter._http_client:
|
|
return {"success": False, "error": "remove participant only supported in WeCom mode"}
|
|
result = await adapter._group_admin.remove_participant(
|
|
adapter._wecom_client, adapter._http_client, chat_id, user_id
|
|
)
|
|
return {"success": result.success, "error": result.error}
|
|
|
|
handler_map: dict[str, Callable[..., Awaitable[Any]]] = {
|
|
"send": _send,
|
|
"reply": _reply,
|
|
"sendAttachment": _send_attachment,
|
|
"download-file": _download_file,
|
|
"upload-file": _upload_file,
|
|
"read": _read,
|
|
"renameGroup": _rename_group,
|
|
"addParticipant": _add_participant,
|
|
"removeParticipant": _remove_participant,
|
|
}
|
|
return handler_map.get(action_name)
|
|
|
|
@staticmethod
|
|
def list_supported_actions() -> list[str]:
|
|
return [k for k, v in WECHAT_MESSAGE_ACTIONS.items() if v.get("status") == "implemented"]
|
|
|
|
@staticmethod
|
|
def list_all_actions() -> dict[str, dict[str, Any]]:
|
|
return dict(WECHAT_MESSAGE_ACTIONS)
|
|
|
|
|
|
def _status_to_action_status(status: str) -> ActionStatus:
|
|
from yuxi.channels.message_actions import ActionStatus
|
|
|
|
status_map = {
|
|
"implemented": ActionStatus.SUPPORTED,
|
|
"planned": ActionStatus.PARTIAL,
|
|
"unsupported": ActionStatus.UNSUPPORTED,
|
|
}
|
|
return status_map.get(status, ActionStatus.UNSUPPORTED)
|
|
|
|
|
|
def _action_name_to_enum(name: str) -> MessageAction | None:
|
|
from yuxi.channels.message_actions import MessageAction
|
|
|
|
mapping: dict[str, str] = {
|
|
"send": "send",
|
|
"broadcast": "broadcast",
|
|
"reply": "reply",
|
|
"sendWithEffect": "send_with_effect",
|
|
"sendAttachment": "send_attachment",
|
|
"sticker": "sticker",
|
|
"sticker-search": "sticker_search",
|
|
"sticker-upload": "sticker_upload",
|
|
"edit": "edit",
|
|
"unsend": "unsend",
|
|
"delete": "delete",
|
|
"read": "read",
|
|
"pin": "pin",
|
|
"unpin": "unpin",
|
|
"list-pins": "list_pins",
|
|
"permissions": "permissions",
|
|
"timeout": "mute",
|
|
"kick": "kick",
|
|
"ban": "ban",
|
|
"react": "react",
|
|
"reactions": "list_reactions",
|
|
"poll": "create_poll",
|
|
"poll-vote": "close_poll",
|
|
"emoji-list": "emoji_list",
|
|
"emoji-upload": "emoji_upload",
|
|
"voice-status": "voice_status",
|
|
"renameGroup": "rename_group",
|
|
"setGroupIcon": "set_group_icon",
|
|
"addParticipant": "add_participant",
|
|
"removeParticipant": "remove_participant",
|
|
"leaveGroup": "leave_group",
|
|
"channel-info": "channel_info",
|
|
"channel-list": "channel_list",
|
|
"channel-create": "channel_create",
|
|
"channel-edit": "channel_edit",
|
|
"channel-delete": "channel_delete",
|
|
"channel-move": "channel_move",
|
|
"category-create": "category_create",
|
|
"category-edit": "category_edit",
|
|
"category-delete": "category_delete",
|
|
"topic-create": "topic_create",
|
|
"topic-edit": "topic_edit",
|
|
"thread-create": "thread_create",
|
|
"thread-list": "thread_list",
|
|
"thread-reply": "thread_reply",
|
|
"member-info": "member_info",
|
|
"role-info": "role_info",
|
|
"role-add": "role_add",
|
|
"role-remove": "role_remove",
|
|
"event-list": "event_list",
|
|
"event-create": "event_create",
|
|
"download-file": "download_file",
|
|
"upload-file": "upload_file",
|
|
"search": "search",
|
|
"set-profile": "set_profile",
|
|
"set-presence": "set_presence",
|
|
}
|
|
try:
|
|
return MessageAction(mapping[name])
|
|
except (KeyError, ValueError):
|
|
return None
|
|
|
|
|
|
def register_wechat_actions() -> None:
|
|
from yuxi.channels.message_actions import ActionDeclaration, ActionRegistry
|
|
|
|
declarations: dict = {}
|
|
for action_name, info in WECHAT_MESSAGE_ACTIONS.items():
|
|
action_enum = _action_name_to_enum(action_name)
|
|
if action_enum is None:
|
|
continue
|
|
declarations[action_enum] = ActionDeclaration(
|
|
action=action_enum,
|
|
status=_status_to_action_status(info.get("status", "unsupported")),
|
|
reason=info.get("reason", ""),
|
|
impl=info.get("impl", ""),
|
|
)
|
|
ActionRegistry.register_channel_actions("wechat", declarations)
|