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