fix: 群聊@人员改用markdown消息,解决他人看到原始userid问题

appchat text消息的<@userid>语法对非本人显示为原始文本,
改用markdown消息类型发送@提醒

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-12 14:37:52 +08:00
parent 149e9da59b
commit d2085f73be

View File

@@ -357,7 +357,7 @@ class WeChatService:
return None return None
async def send_group_text(self, chat_id: str, content: str) -> bool: async def send_group_text(self, chat_id: str, content: str) -> bool:
"""发送文本消息到群聊(支持 <@userid> 语法 @人员)""" """发送文本消息到群聊"""
if not self._enabled: if not self._enabled:
return False return False
try: try:
@@ -380,6 +380,30 @@ class WeChatService:
logger.error(f"发送群聊文本异常: {e}") logger.error(f"发送群聊文本异常: {e}")
return False 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: async def send_group_image(self, chat_id: str, media_id: str) -> bool:
"""发送图片消息到群聊""" """发送图片消息到群聊"""
if not self._enabled: if not self._enabled:
@@ -500,11 +524,11 @@ class WeChatService:
if not sent: if not sent:
success = False success = False
# ---- 3. @相关人员(text 消息 ---- # ---- 3. @相关人员(markdown 消息,@渲染更可靠 ----
if mention_user_ids: if mention_user_ids:
mentions = " ".join(f"<@{uid}>" for uid in mention_user_ids) mentions = " ".join(f"<@{uid}>" for uid in mention_user_ids)
text_content = f"{mentions} 请及时处理以上{type_name}告警" md_content = f"{mentions} 请及时处理以上**{type_name}告警**"
sent = await self.send_group_text(chat_id, text_content) sent = await self.send_group_markdown(chat_id, md_content)
if not sent: if not sent:
success = False success = False