diff --git a/README.md b/README.md index ffaf8f5..bbd86ab 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,11 @@ 一款用来适配Dify的chatbot的返回的markdown格式返回的消息处理 +**最新更新** ++ 图文混排异常修复 ++ 增加对换行的转化修复 + +**实现功能:** + + 提取Dify返回的Markdown图片链接中的网址,并修改ReplyType为IMAGE_URL,以便DoW自动下载Markdown链接中的图片; + 提取Dify返回的包含网址的Markdown链接中的图片网址,并修改ReplyType为IMAGE_URL,以便CoW自动下载Markdown链接中的图片; + 去掉每行结尾的Markdown链接中网址部分的小括号,避免微信误以为“)”是网址的一部分导致微信中无法打开该页面。 diff --git a/dowmd.py b/dowmd.py index c041037..9ed3779 100644 --- a/dowmd.py +++ b/dowmd.py @@ -13,7 +13,7 @@ from config import conf name="dow_markdown", desire_priority=66, desc="优化markdown返回结果中的图片和网址链接。", - version="0.5", + version="0.6", author="Kubbo", hidden=False ) @@ -70,7 +70,8 @@ class dow_markdown(Plugin): host = host[:-3] if 'coze' == conf().get('model'): host = "https://s.coze.cn/t/" - parts = re.split(r'\!\[[^\]]+\]\(?', content) + format_content = self.format_content(content) + parts = re.split(r'&分块&', format_content) parts = [p for p in parts if p.strip()] channel = e_context["channel"] context = e_context["context"] @@ -84,11 +85,16 @@ class dow_markdown(Plugin): elif re.search(r"\.(mp4)", part): reply.type = ReplyType.VIDEO_URL reply.content = self.extract_url(part.strip(), host) - if index == len(parts) - 1 and is_last: - e_context["reply"] = reply - e_context.action = EventAction.BREAK_PASS - else: - channel.send(reply, context) + try: + if index == len(parts) - 1 and is_last: + e_context["reply"] = reply + e_context.action = EventAction.BREAK_PASS + else: + channel.send(reply, context) + except Exception as e: + source_type = {ReplyType.IMAGE_URL: "图片", ReplyType.VIDEO_URL: "视频"}[reply.type] + itchat.send(f"[{source_type}]加载失败", toUserName=context.get("receiver")) + logger.warn("[dow_markdown] handle_send failed, error={}".format(e)) def extract_url(self, text, host): text = text.strip() @@ -98,4 +104,10 @@ class dow_markdown(Plugin): if text.startswith('/t/') and 'coze' == conf().get('model'): text = text[3:] text = host + text + logger.info("[extract_url] text={}".format(text)) return text + + def format_content(self, content): + content = re.sub(r"\!\[([^\]]+)\]\(([^)\\\s]+)\)", r"&分块&\2&分块&", content) + content = re.sub(r"\\n", "\n", content) + return content