# ============================================================================= # 企微IT智能服务台 — 企微回调消息 Pydantic Schema # ============================================================================= # 说明:定义企微回调消息的数据结构 # 包含:GET验证请求、POST消息体、加解密相关 # ============================================================================= from typing import Optional from pydantic import BaseModel, Field # -------------------------------------------------------------------------- # 企微回调验证请求 Schema(GET 请求) # -------------------------------------------------------------------------- class WecomCallbackVerify(BaseModel): """企微回调URL验证请求 Schema。 企微管理后台配置回调URL时,会发送GET请求验证。 需要验证签名并返回解密后的 echostr。 Attributes: msg_signature: 企微签名(用于验证请求来源) timestamp: 时间戳 nonce: 随机数 echostr: 加密的验证字符串(解密后返回给企微) """ msg_signature: str = Field(..., description="企微签名") timestamp: str = Field(..., description="时间戳") nonce: str = Field(..., description="随机数") echostr: str = Field(..., description="加密的验证字符串") # -------------------------------------------------------------------------- # 企微回调消息体 Schema(POST 请求解析后) # -------------------------------------------------------------------------- class WecomCallbackMessage(BaseModel): """企微回调消息体 Schema。 企微推送消息时发送的XML解析后的结构。 包含加密的消息内容。 Attributes: to_user_name: 接收方(企业ID) agent_id: 应用AgentID encrypt: 加密的消息内容 """ to_user_name: str = Field(default="", description="接收方企业ID") agent_id: str = Field(default="", description="应用AgentID") encrypt: str = Field(..., description="加密的消息内容") # -------------------------------------------------------------------------- # 企微消息内容 Schema(解密后的消息) # -------------------------------------------------------------------------- class WecomMessageContent(BaseModel): """企微消息内容 Schema(解密后)。 AES解密后的XML消息解析结果。 Attributes: to_user_name: 接收方 from_user_name: 发送者企微UserID create_time: 消息创建时间戳 msg_type: 消息类型(text/image等) content: 消息内容 msg_id: 消息ID agent_id: 应用AgentID """ to_user_name: str = Field(default="", description="接收方") from_user_name: str = Field(..., description="发送者企微UserID") create_time: int = Field(default=0, description="消息创建时间戳") msg_type: str = Field(default="text", description="消息类型") content: str = Field(default="", description="消息内容") msg_id: str = Field(default="", description="消息ID") agent_id: str = Field(default="", description="应用AgentID")