From d2085f73be74c69478580492760612f90efed1c5 Mon Sep 17 00:00:00 2001 From: 16337 <1633794139@qq.com> Date: Thu, 12 Mar 2026 14:37:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=BE=A4=E8=81=8A@=E4=BA=BA=E5=91=98?= =?UTF-8?q?=E6=94=B9=E7=94=A8markdown=E6=B6=88=E6=81=AF=EF=BC=8C=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E4=BB=96=E4=BA=BA=E7=9C=8B=E5=88=B0=E5=8E=9F=E5=A7=8B?= =?UTF-8?q?userid=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit appchat text消息的<@userid>语法对非本人显示为原始文本, 改用markdown消息类型发送@提醒 Co-Authored-By: Claude Opus 4.6 --- app/services/wechat_service.py | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/app/services/wechat_service.py b/app/services/wechat_service.py index 4c93225..01fcfbe 100644 --- a/app/services/wechat_service.py +++ b/app/services/wechat_service.py @@ -357,7 +357,7 @@ class WeChatService: return None async def send_group_text(self, chat_id: str, content: str) -> bool: - """发送文本消息到群聊(支持 <@userid> 语法 @人员)""" + """发送文本消息到群聊""" if not self._enabled: return False try: @@ -380,6 +380,30 @@ class WeChatService: logger.error(f"发送群聊文本异常: {e}") return False + async def send_group_markdown(self, chat_id: str, content: str) -> bool: + """发送 markdown 消息到群聊(@人员使用此方式)""" + if not self._enabled: + return False + try: + access_token = await self._get_access_token() + msg = { + "chatid": chat_id, + "msgtype": "markdown", + "markdown": {"content": content}, + } + url = f"https://qyapi.weixin.qq.com/cgi-bin/appchat/send?access_token={access_token}" + async with httpx.AsyncClient(timeout=10) as client: + resp = await client.post(url, json=msg) + data = resp.json() + if data.get("errcode") != 0: + logger.error(f"群聊markdown发送失败: {data}") + return False + logger.info(f"群聊markdown已发送: chatid={chat_id}") + return True + except Exception as e: + logger.error(f"发送群聊markdown异常: {e}") + return False + async def send_group_image(self, chat_id: str, media_id: str) -> bool: """发送图片消息到群聊""" if not self._enabled: @@ -500,11 +524,11 @@ class WeChatService: if not sent: success = False - # ---- 3. @相关人员(text 消息) ---- + # ---- 3. @相关人员(markdown 消息,@渲染更可靠) ---- if mention_user_ids: mentions = " ".join(f"<@{uid}>" for uid in mention_user_ids) - text_content = f"{mentions} 请及时处理以上{type_name}告警" - sent = await self.send_group_text(chat_id, text_content) + md_content = f"{mentions} 请及时处理以上**{type_name}告警**" + sent = await self.send_group_markdown(chat_id, md_content) if not sent: success = False