该提交实现了支持企业微信、微信公众号、个人微信桥接三种模式的完整微信渠道适配器,包含以下核心模块: 1. 基础认证与配置相关:auth_adapter、config_reload、setup_contract等 2. 消息处理与格式转换:format、attachment_adapter、outbound_adapter等 3. 多模式客户端支持:wecom/mp子模块,包含加解密、消息收发能力 4. 辅助能力:限速器、防抖、会话绑定、事件映射、模板渲染等 5. 扩展能力:二维码登录、消息读取、特权用户、心跳监控等 实现了完整的微信生态对接能力,支持消息收发、事件处理、API调用限流、配置热重载等功能。
38 lines
1.5 KiB
Python
38 lines
1.5 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
DEFAULT_WELCOME_TEMPLATE = "你好!我是 AI 助手,有什么可以帮你的?"
|
|
DEFAULT_PAIRING_TEMPLATE = "收到你的访问请求,请输入配对码: {code}"
|
|
DEFAULT_APPROVED_TEMPLATE = "你的访问请求已获批准!"
|
|
DEFAULT_REJECTED_TEMPLATE = "你的访问请求已被拒绝。"
|
|
DEFAULT_ERROR_TEMPLATE = "抱歉,处理你的消息时出错: {error}"
|
|
|
|
|
|
class WeChatTemplateAdapter:
|
|
@staticmethod
|
|
def render_welcome(config: dict[str, Any], context: dict[str, Any] | None = None) -> str:
|
|
return config.get("welcome_message", DEFAULT_WELCOME_TEMPLATE)
|
|
|
|
@staticmethod
|
|
def render_pairing(config: dict[str, Any], code: str) -> str:
|
|
template = config.get("pairing_message", DEFAULT_PAIRING_TEMPLATE)
|
|
return template.format(code=code)
|
|
|
|
@staticmethod
|
|
def render_approved(config: dict[str, Any], context: dict[str, Any] | None = None) -> str:
|
|
return config.get("approved_message", DEFAULT_APPROVED_TEMPLATE)
|
|
|
|
@staticmethod
|
|
def render_rejected(config: dict[str, Any], context: dict[str, Any] | None = None) -> str:
|
|
return config.get("rejected_message", DEFAULT_REJECTED_TEMPLATE)
|
|
|
|
@staticmethod
|
|
def render_error(config: dict[str, Any], error: str) -> str:
|
|
template = config.get("error_message", DEFAULT_ERROR_TEMPLATE)
|
|
return template.format(error=error)
|
|
|
|
@staticmethod
|
|
def render_system_notice(config: dict[str, Any], notice: str) -> str:
|
|
return notice
|