mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-06-02 00:57:41 +08:00
Merge pull request #2344 from 6vision/markdown_format_display
Optimize markdown format display
This commit is contained in:
@@ -20,7 +20,7 @@ from common.expired_dict import ExpiredDict
|
|||||||
from common.log import logger
|
from common.log import logger
|
||||||
from common.singleton import singleton
|
from common.singleton import singleton
|
||||||
from common.time_check import time_checker
|
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 config import conf, get_appdata_dir
|
||||||
from lib import itchat
|
from lib import itchat
|
||||||
from lib.itchat.content import *
|
from lib.itchat.content import *
|
||||||
@@ -213,9 +213,11 @@ class WechatChannel(ChatChannel):
|
|||||||
def send(self, reply: Reply, context: Context):
|
def send(self, reply: Reply, context: Context):
|
||||||
receiver = context["receiver"]
|
receiver = context["receiver"]
|
||||||
if reply.type == ReplyType.TEXT:
|
if reply.type == ReplyType.TEXT:
|
||||||
|
reply.content = remove_markdown_symbol(reply.content)
|
||||||
itchat.send(reply.content, toUserName=receiver)
|
itchat.send(reply.content, toUserName=receiver)
|
||||||
logger.info("[WX] sendMsg={}, receiver={}".format(reply, receiver))
|
logger.info("[WX] sendMsg={}, receiver={}".format(reply, receiver))
|
||||||
elif reply.type == ReplyType.ERROR or reply.type == ReplyType.INFO:
|
elif reply.type == ReplyType.ERROR or reply.type == ReplyType.INFO:
|
||||||
|
reply.content = remove_markdown_symbol(reply.content)
|
||||||
itchat.send(reply.content, toUserName=receiver)
|
itchat.send(reply.content, toUserName=receiver)
|
||||||
logger.info("[WX] sendMsg={}, receiver={}".format(reply, receiver))
|
logger.info("[WX] sendMsg={}, receiver={}".format(reply, receiver))
|
||||||
elif reply.type == ReplyType.VOICE:
|
elif reply.type == ReplyType.VOICE:
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ from channel.wechatcom.wechatcomapp_client import WechatComAppClient
|
|||||||
from channel.wechatcom.wechatcomapp_message import WechatComAppMessage
|
from channel.wechatcom.wechatcomapp_message import WechatComAppMessage
|
||||||
from common.log import logger
|
from common.log import logger
|
||||||
from common.singleton import singleton
|
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 config import conf, subscribe_msg
|
||||||
from voice.audio_convert import any_to_amr, split_audio
|
from voice.audio_convert import any_to_amr, split_audio
|
||||||
|
|
||||||
@@ -52,7 +52,7 @@ class WechatComAppChannel(ChatChannel):
|
|||||||
def send(self, reply: Reply, context: Context):
|
def send(self, reply: Reply, context: Context):
|
||||||
receiver = context["receiver"]
|
receiver = context["receiver"]
|
||||||
if reply.type in [ReplyType.TEXT, ReplyType.ERROR, ReplyType.INFO]:
|
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)
|
texts = split_string_by_utf8_length(reply_text, MAX_UTF8_LEN)
|
||||||
if len(texts) > 1:
|
if len(texts) > 1:
|
||||||
logger.info("[wechatcom] text too long, split into {} parts".format(len(texts)))
|
logger.info("[wechatcom] text too long, split into {} parts".format(len(texts)))
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ from channel.wechatmp.common import *
|
|||||||
from channel.wechatmp.wechatmp_client import WechatMPClient
|
from channel.wechatmp.wechatmp_client import WechatMPClient
|
||||||
from common.log import logger
|
from common.log import logger
|
||||||
from common.singleton import singleton
|
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 config import conf
|
||||||
from voice.audio_convert import any_to_mp3, split_audio
|
from voice.audio_convert import any_to_mp3, split_audio
|
||||||
|
|
||||||
@@ -81,7 +81,7 @@ class WechatMPChannel(ChatChannel):
|
|||||||
receiver = context["receiver"]
|
receiver = context["receiver"]
|
||||||
if self.passive_reply:
|
if self.passive_reply:
|
||||||
if reply.type == ReplyType.TEXT or reply.type == ReplyType.INFO or reply.type == ReplyType.ERROR:
|
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))
|
logger.info("[wechatmp] text cached, receiver {}\n{}".format(receiver, reply_text))
|
||||||
self.cache_dict[receiver].append(("text", reply_text))
|
self.cache_dict[receiver].append(("text", reply_text))
|
||||||
elif reply.type == ReplyType.VOICE:
|
elif reply.type == ReplyType.VOICE:
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import io
|
import io
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from common.log import logger
|
from common.log import logger
|
||||||
@@ -68,3 +69,9 @@ def convert_webp_to_png(webp_image):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to convert WEBP to PNG: {e}")
|
logger.error(f"Failed to convert WEBP to PNG: {e}")
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
def remove_markdown_symbol(text: str):
|
||||||
|
# 移除markdown格式,目前先移除**
|
||||||
|
if not text:
|
||||||
|
return text
|
||||||
|
return re.sub(r'\*\*(.*?)\*\*', r'\1', text)
|
||||||
|
|||||||
Reference in New Issue
Block a user