v0.3功能更新

This commit is contained in:
aixianling
2024-08-05 18:42:38 +08:00
parent e27162d0d5
commit ef5d720157
2 changed files with 46 additions and 28 deletions

View File

@@ -3,6 +3,9 @@
+ 提取Dify返回的Markdown图片链接中的网址并修改ReplyType为IMAGE_URL以便DoW自动下载Markdown链接中的图片 + 提取Dify返回的Markdown图片链接中的网址并修改ReplyType为IMAGE_URL以便DoW自动下载Markdown链接中的图片
+ 提取Dify返回的包含网址的Markdown链接中的图片网址并修改ReplyType为IMAGE_URL以便CoW自动下载Markdown链接中的图片 + 提取Dify返回的包含网址的Markdown链接中的图片网址并修改ReplyType为IMAGE_URL以便CoW自动下载Markdown链接中的图片
+ 去掉每行结尾的Markdown链接中网址部分的小括号避免微信误以为“)”是网址的一部分导致微信中无法打开该页面。 + 去掉每行结尾的Markdown链接中网址部分的小括号避免微信误以为“)”是网址的一部分导致微信中无法打开该页面。
+ 增加分段发送消息,分割符为markdown语法中的换行标签`<br>`
+ 针对触发绘画的有loading消息触发
+ 支持图文消息拆分发送,Dify兼容性更好了
**安装方法:** **安装方法:**

View File

@@ -1,20 +1,18 @@
# encoding:utf-8 # encoding:utf-8
import re import re
import requests
import os
import plugins import plugins
from bridge.reply import Reply, ReplyType from bridge.reply import Reply, ReplyType
from common.log import logger
from plugins import *
from lib import itchat from lib import itchat
from plugins import *
@plugins.register( @plugins.register(
name="dow_markdown", name="dow_markdown",
desire_priority=66, desire_priority=66,
desc="优化markdown返回结果中的图片和网址链接。", desc="优化markdown返回结果中的图片和网址链接。",
version="0.2", version="0.3",
author="Kubbo", author="Kubbo",
hidden=False hidden=False
) )
@@ -28,45 +26,62 @@ class dow_markdown(Plugin):
except Exception as e: except Exception as e:
logger.warn("[dow_markdown] init failed, ignore.") logger.warn("[dow_markdown] init failed, ignore.")
raise e raise e
def on_handle_context(self,e_context:EventContext):
def on_handle_context(self, e_context: EventContext):
send_msg = e_context["context"]
try: try:
send_msg = e_context["context"]
text = send_msg["content"] text = send_msg["content"]
if send_msg["type"] == ReplyType.TEXT: if any(word in send_msg["content"] for word in ["", "/sd"]):
if any(word in send_msg["content"] for word in [""]): receiver = send_msg.get("receiver")
receiver = send_msg.get("receiver") itchat.send("我正在绘画中,可能需要多等待一会,请稍后...", toUserName=receiver)
itchat.send("我正在绘画中,可能需要多等待一会,请稍后...",toUserName=receiver) logger.info("[WX] sendMsg={}, receiver={}".format(text, receiver))
logger.info("[WX] sendMsg={}, receiver={}".format(text, receiver)) e_context.action = EventAction.BREAK_PASS
except Exception as e: except Exception as e:
text = send_msg["content"]
logger.warn(f"[dow_markdown] on_handle_context failed, content={text}, error={e}") logger.warn(f"[dow_markdown] on_handle_context failed, content={text}, error={e}")
finally: finally:
e_context.action = EventAction.CONTINUE e_context.action = EventAction.CONTINUE
def on_decorate_reply(self, e_context: EventContext): def on_decorate_reply(self, e_context: EventContext):
if e_context["reply"].type != ReplyType.TEXT: if e_context["reply"].type != ReplyType.TEXT:
return return
logger.info("[on_decorate_reply] yooooooooooooooooo")
try: try:
channel = e_context["channel"]
context = e_context["context"]
content = e_context["reply"].content.strip() content = e_context["reply"].content.strip()
# 避免图片无法下载时,重复调用插件导致没有响应的问题 # 避免图片无法下载时,重复调用插件导致没有响应的问题
if content.startswith("[DOWNLOAD_ERROR]"): if content.startswith("[DOWNLOAD_ERROR]"):
return return
has_md = re.search(r'\!\[[^\]]+\]\(?',content) content_arr = [m for m in content.split("<br>") if m.strip()]
if has_md: if len(content_arr) > 0:
host = os.environ.get('DIFY_API_BASE', 'http://121.37.155.68:35801') for index, msg in enumerate(content_arr):
if host.endswith('/v1'): self.handle_send(msg, e_context, index == len(content_arr) - 1)
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
except Exception as e: 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: finally:
e_context.action = EventAction.CONTINUE e_context.action = EventAction.CONTINUE
def get_help_text(self, **kwargs): def get_help_text(self, **kwargs):
return "优化返回结果中的图片和网址链接。" 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)