diff --git a/README.md b/README.md
index 93b1645..12fad70 100644
--- a/README.md
+++ b/README.md
@@ -3,6 +3,9 @@
+ 提取Dify返回的Markdown图片链接中的网址,并修改ReplyType为IMAGE_URL,以便DoW自动下载Markdown链接中的图片;
+ 提取Dify返回的包含网址的Markdown链接中的图片网址,并修改ReplyType为IMAGE_URL,以便CoW自动下载Markdown链接中的图片;
+ 去掉每行结尾的Markdown链接中网址部分的小括号,避免微信误以为“)”是网址的一部分导致微信中无法打开该页面。
++ 增加分段发送消息,分割符为markdown语法中的换行标签`
`
++ 针对触发绘画的有loading消息触发
++ 支持图文消息拆分发送,Dify兼容性更好了
**安装方法:**
diff --git a/dowmd.py b/dowmd.py
index 18839f2..6a63cc3 100644
--- a/dowmd.py
+++ b/dowmd.py
@@ -1,20 +1,18 @@
# encoding:utf-8
import re
-import requests
-import os
import plugins
from bridge.reply import Reply, ReplyType
-from common.log import logger
-from plugins import *
from lib import itchat
+from plugins import *
+
@plugins.register(
name="dow_markdown",
desire_priority=66,
desc="优化markdown返回结果中的图片和网址链接。",
- version="0.2",
+ version="0.3",
author="Kubbo",
hidden=False
)
@@ -28,45 +26,62 @@ class dow_markdown(Plugin):
except Exception as e:
logger.warn("[dow_markdown] init failed, ignore.")
raise e
- def on_handle_context(self,e_context:EventContext):
+
+ def on_handle_context(self, e_context: EventContext):
+ send_msg = e_context["context"]
try:
- send_msg = e_context["context"]
text = send_msg["content"]
- if send_msg["type"] == ReplyType.TEXT:
- if any(word in send_msg["content"] for word in ["画"]):
- receiver = send_msg.get("receiver")
- itchat.send("我正在绘画中,可能需要多等待一会,请稍后...",toUserName=receiver)
- logger.info("[WX] sendMsg={}, receiver={}".format(text, receiver))
+ if any(word in send_msg["content"] for word in ["画", "/sd"]):
+ receiver = send_msg.get("receiver")
+ itchat.send("我正在绘画中,可能需要多等待一会,请稍后...", toUserName=receiver)
+ logger.info("[WX] sendMsg={}, receiver={}".format(text, receiver))
+ e_context.action = EventAction.BREAK_PASS
except Exception as e:
+ text = send_msg["content"]
logger.warn(f"[dow_markdown] on_handle_context failed, content={text}, error={e}")
finally:
e_context.action = EventAction.CONTINUE
+
def on_decorate_reply(self, e_context: EventContext):
if e_context["reply"].type != ReplyType.TEXT:
return
+ logger.info("[on_decorate_reply] yooooooooooooooooo")
try:
- channel = e_context["channel"]
- context = e_context["context"]
content = e_context["reply"].content.strip()
# 避免图片无法下载时,重复调用插件导致没有响应的问题
if content.startswith("[DOWNLOAD_ERROR]"):
return
- has_md = re.search(r'\!\[[^\]]+\]\(?',content)
- if has_md:
- host = os.environ.get('DIFY_API_BASE', 'http://121.37.155.68:35801')
- if host.endswith('/v1'):
- host = host[:-3]
- image_path = re.search(r'!\[.*\]\((.*?)\)',content).group(1)
- # 排除网络图片,不做特殊处理
- if image_path.startswith("http"):
- host = ""
- logger.info(f"提取到的数据==>host:{host},url:{image_path}")
- reply = Reply(ReplyType.IMAGE_URL, f"{host}{image_path}")
- e_context["reply"] = reply
- e_context.action = EventAction.BREAK_PASS
+ content_arr = [m for m in content.split("
") if m.strip()]
+ if len(content_arr) > 0:
+ for index, msg in enumerate(content_arr):
+ self.handle_send(msg, e_context, index == len(content_arr) - 1)
except Exception as e:
- logger.warn(f"[dow_markdown] on_decorate_reply failed, content={content}, error={e}")
+ logger.warn(f"[dow_markdown] on_decorate_reply failed, error={e}")
finally:
e_context.action = EventAction.CONTINUE
+
def get_help_text(self, **kwargs):
return "优化返回结果中的图片和网址链接。"
+
+ def handle_send(self, content, e_context, is_last):
+ host = os.environ.get('DIFY_API_BASE', 'http://121.37.155.68:35801')
+ if host.endswith('/v1'):
+ host = host[:-3]
+ parts = re.split(r'\!\[[^\]]+\]\(', content)
+ parts = [p for p in parts if p.strip()]
+ channel = e_context["channel"]
+ context = e_context["context"]
+ logger.info("[handle_send] parts={}".format(parts))
+ if len(parts) > 0:
+ for index, part in enumerate(parts):
+ reply = Reply(content=part.strip(), type=ReplyType.TEXT)
+ if re.search(r"\.(gif|jpg|png|jpeg|webp)", part):
+ reply.type = ReplyType.IMAGE_URL
+ reply.content = part[:-1].strip()
+ if not part.startswith('http'):
+ reply.content = host + reply.content
+ if index == len(parts) - 1 and is_last:
+ e_context["reply"] = reply
+ e_context.action = EventAction.BREAK_PASS
+ else:
+ channel.send(reply, context)