该提交实现了支持企业微信、微信公众号、个人微信桥接三种模式的完整微信渠道适配器,包含以下核心模块: 1. 基础认证与配置相关:auth_adapter、config_reload、setup_contract等 2. 消息处理与格式转换:format、attachment_adapter、outbound_adapter等 3. 多模式客户端支持:wecom/mp子模块,包含加解密、消息收发能力 4. 辅助能力:限速器、防抖、会话绑定、事件映射、模板渲染等 5. 扩展能力:二维码登录、消息读取、特权用户、心跳监控等 实现了完整的微信生态对接能力,支持消息收发、事件处理、API调用限流、配置热重载等功能。
59 lines
1.7 KiB
Python
59 lines
1.7 KiB
Python
from __future__ import annotations
|
|
|
|
import base64
|
|
|
|
|
|
class WeChatBase64Adapter:
|
|
@staticmethod
|
|
def encode_image(data: bytes) -> str:
|
|
return base64.b64encode(data).decode("ascii")
|
|
|
|
@staticmethod
|
|
def decode_image(data_url: str) -> bytes:
|
|
if data_url.startswith("data:"):
|
|
_, b64 = data_url.split(",", 1)
|
|
return base64.b64decode(b64)
|
|
return base64.b64decode(data_url)
|
|
|
|
@staticmethod
|
|
def build_image_data_url(mime_type: str, data: bytes) -> str:
|
|
return f"data:{mime_type};base64,{base64.b64encode(data).decode('ascii')}"
|
|
|
|
@staticmethod
|
|
def encode_voice(data: bytes) -> str:
|
|
return base64.b64encode(data).decode("ascii")
|
|
|
|
@staticmethod
|
|
def encode_video(data: bytes) -> str:
|
|
return base64.b64encode(data).decode("ascii")
|
|
|
|
@staticmethod
|
|
def decode_media(b64_data: str) -> bytes:
|
|
return base64.b64decode(b64_data)
|
|
|
|
@staticmethod
|
|
def get_media_mime_type(media_type: str) -> str:
|
|
mime_map = {
|
|
"image": "image/png",
|
|
"voice": "audio/amr",
|
|
"video": "video/mp4",
|
|
"file": "application/octet-stream",
|
|
}
|
|
return mime_map.get(media_type, "application/octet-stream")
|
|
|
|
@staticmethod
|
|
def encode_file(data: bytes) -> str:
|
|
return base64.b64encode(data).decode("ascii")
|
|
|
|
@staticmethod
|
|
def decode_file(b64_data: str) -> bytes:
|
|
return base64.b64decode(b64_data)
|
|
|
|
@staticmethod
|
|
def encode_text(text: str) -> str:
|
|
return base64.b64encode(text.encode("utf-8")).decode("ascii")
|
|
|
|
@staticmethod
|
|
def decode_text(b64_data: str) -> str:
|
|
return base64.b64decode(b64_data).decode("utf-8")
|