diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index d79bda042..ce3f0861e 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -20,7 +20,7 @@ from common.log import logger from common.singleton import singleton from common.time_check import time_checker -from common.utils import convert_webp_to_png +from common.utils import convert_webp_to_png, remove_markdown_symbol from config import conf, get_appdata_dir from lib import itchat from lib.itchat.content import * @@ -213,9 +213,11 @@ def handle_group(self, cmsg: ChatMessage): def send(self, reply: Reply, context: Context): receiver = context["receiver"] if reply.type == ReplyType.TEXT: + reply.content = remove_markdown_symbol(reply.content) itchat.send(reply.content, toUserName=receiver) logger.info("[WX] sendMsg={}, receiver={}".format(reply, receiver)) elif reply.type == ReplyType.ERROR or reply.type == ReplyType.INFO: + reply.content = remove_markdown_symbol(reply.content) itchat.send(reply.content, toUserName=receiver) logger.info("[WX] sendMsg={}, receiver={}".format(reply, receiver)) elif reply.type == ReplyType.VOICE: diff --git a/channel/wechatcom/wechatcomapp_channel.py b/channel/wechatcom/wechatcomapp_channel.py index 5ed329681..41c824cde 100644 --- a/channel/wechatcom/wechatcomapp_channel.py +++ b/channel/wechatcom/wechatcomapp_channel.py @@ -17,7 +17,7 @@ from channel.wechatcom.wechatcomapp_message import WechatComAppMessage from common.log import logger from common.singleton import singleton -from common.utils import compress_imgfile, fsize, split_string_by_utf8_length, convert_webp_to_png +from common.utils import compress_imgfile, fsize, split_string_by_utf8_length, convert_webp_to_png, remove_markdown_symbol from config import conf, subscribe_msg from voice.audio_convert import any_to_amr, split_audio @@ -52,7 +52,7 @@ def startup(self): def send(self, reply: Reply, context: Context): receiver = context["receiver"] if reply.type in [ReplyType.TEXT, ReplyType.ERROR, ReplyType.INFO]: - reply_text = reply.content + reply_text = remove_markdown_symbol(reply.content) texts = split_string_by_utf8_length(reply_text, MAX_UTF8_LEN) if len(texts) > 1: logger.info("[wechatcom] text too long, split into {} parts".format(len(texts))) diff --git a/channel/wechatmp/wechatmp_channel.py b/channel/wechatmp/wechatmp_channel.py index fa2397681..5fc7b8c91 100644 --- a/channel/wechatmp/wechatmp_channel.py +++ b/channel/wechatmp/wechatmp_channel.py @@ -19,7 +19,7 @@ from channel.wechatmp.wechatmp_client import WechatMPClient from common.log import logger from common.singleton import singleton -from common.utils import split_string_by_utf8_length +from common.utils import split_string_by_utf8_length, remove_markdown_symbol from config import conf from voice.audio_convert import any_to_mp3, split_audio @@ -81,7 +81,7 @@ def send(self, reply: Reply, context: Context): receiver = context["receiver"] if self.passive_reply: if reply.type == ReplyType.TEXT or reply.type == ReplyType.INFO or reply.type == ReplyType.ERROR: - reply_text = reply.content + reply_text = remove_markdown_symbol(reply.content) logger.info("[wechatmp] text cached, receiver {}\n{}".format(receiver, reply_text)) self.cache_dict[receiver].append(("text", reply_text)) elif reply.type == ReplyType.VOICE: diff --git a/common/utils.py b/common/utils.py index 2349898e4..f509cb9d9 100644 --- a/common/utils.py +++ b/common/utils.py @@ -1,5 +1,6 @@ import io import os +import re from urllib.parse import urlparse from PIL import Image from common.log import logger @@ -68,3 +69,9 @@ def convert_webp_to_png(webp_image): except Exception as e: logger.error(f"Failed to convert WEBP to PNG: {e}") raise + +def remove_markdown_symbol(text: str): + # 移除markdown格式,目前先移除** + if not text: + return text + return re.sub(r'\*\*(.*?)\*\*', r'\1', text)