Files
GX-gp-notify/app/wechat/handler.py
T
v6ole 7455d7e426 chore: ruff 代码检查与修复
130 issues auto-fixed (import ordering, UP045/UP006 type annotations),
33 issues manually fixed (E712/E501/E402/E722 + N818 rename + per-file
wechat ignore for N8xx naming conventions). All 33 tests pass.
2026-05-09 14:28:09 +08:00

58 lines
1.6 KiB
Python

import xml.etree.ElementTree as ET
from app.config import settings
from app.wechat.crypto import WXBizMsgCrypt
class WeChatMessageHandler:
def __init__(self):
self.wxcpt = WXBizMsgCrypt(
sToken=settings.wechat_token,
sEncodingAESKey=settings.wechat_encoding_aes_key,
sReceiveId=settings.wechat_corp_id,
)
def verify_url(
self, msg_signature: str, timestamp: str, nonce: str, echostr: str
) -> str | None:
ret, sEchoStr = self.wxcpt.VerifyURL(
msg_signature, timestamp, nonce, echostr
)
if ret == 0:
return (
sEchoStr.decode("utf-8")
if isinstance(sEchoStr, bytes)
else sEchoStr
)
return None
def decrypt_message(
self,
post_data: str,
msg_signature: str,
timestamp: str,
nonce: str,
) -> ET.Element | None:
ret, xml_content = self.wxcpt.DecryptMsg(
post_data, msg_signature, timestamp, nonce
)
if ret != 0:
return None
return ET.fromstring(xml_content)
def encrypt_response(
self, response_xml: str, nonce: str, timestamp: str
) -> str | None:
ret, encrypted = self.wxcpt.EncryptMsg(response_xml, nonce, timestamp)
if ret == 0:
return encrypted
return None
def handle_event(
self, event: str, event_key: str | None, from_user: str
) -> str | None:
return None
def handle_text(self, content: str, from_user: str) -> str | None:
return None