Skip to content

Commit 3273b16

Browse files
committed
fix: 企业微信markdown渲染长度上限4096,超过会报异常,超过上限,则转文本发送
1 parent 4359c2a commit 3273b16

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

biz/utils/im/wecom.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,31 @@ def send_message(self, content, msg_type='text', title=None, is_at_all=False, pr
107107

108108
if response.status_code != 200:
109109
logger.error(f"企业微信消息发送失败! webhook_url:{post_url}, error_msg:{response.text}")
110+
111+
try:
112+
data = json.loads(response.text)
113+
errmsg = data.get("errmsg")
114+
if errmsg and "markdown.content exceed max length" in errmsg:
115+
# markdown渲染过长了,尝试text发送
116+
data = {
117+
"msgtype": "text",
118+
"text": {
119+
"content": content,
120+
"mentioned_list": ["@all"] if is_at_all else []
121+
}
122+
}
123+
124+
response = requests.post(
125+
url=post_url,
126+
json=data,
127+
headers={'Content-Type': 'application/json'}
128+
)
129+
130+
except json.JSONDecodeError as e:
131+
print(f"JSON 解析失败: {e}")
132+
except Exception as e:
133+
logger.error(f"企业微信消息发送失败! ", e)
134+
110135
return
111136

112137
result = response.json()

0 commit comments

Comments
 (0)