修改完毕
This commit is contained in:
@@ -1,14 +1,14 @@
|
|||||||
一款用来适配Dify的chatbot的返回的markdown格式返回的消息处理
|
一款用来适配Dify的chatbot的返回的markdown格式返回的消息处理
|
||||||
|
|
||||||
+ 提取Dify返回的Markdown图片链接中的网址,并修改ReplyType为IMAGE_URL,以便DoW自动下载Markdown链接中的图片;
|
+ 提取Dify返回的Markdown图片链接中的网址,并修改ReplyType为IMAGE_URL,以便DoW自动下载Markdown链接中的图片;
|
||||||
+ 提取Dify返回的包含 https://s.coze.cn/t/xxx 网址的Markdown链接中的图片网址,并修改ReplyType为IMAGE_URL,以便CoW自动下载Markdown链接中的图片;
|
+ 提取Dify返回的包含网址的Markdown链接中的图片网址,并修改ReplyType为IMAGE_URL,以便CoW自动下载Markdown链接中的图片;
|
||||||
+ 去掉每行结尾的Markdown链接中网址部分的小括号,避免微信误以为“)”是网址的一部分导致微信中无法打开该页面。
|
+ 去掉每行结尾的Markdown链接中网址部分的小括号,避免微信误以为“)”是网址的一部分导致微信中无法打开该页面。
|
||||||
|
|
||||||
|
|
||||||
**安装方法:**
|
**安装方法:**
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
#installp https://github.com/wangxyd/nicecoze.git
|
#installp https://github.com/AceyKubbo/dow-markdown.git
|
||||||
#scanp
|
#scanp
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -18,5 +18,5 @@
|
|||||||
|
|
||||||
**更新方法:**
|
**更新方法:**
|
||||||
```sh
|
```sh
|
||||||
#updatep nicecoze
|
#updatep dow_markdown
|
||||||
```
|
```
|
||||||
62
markdown.py
62
markdown.py
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
import requests
|
import requests
|
||||||
|
import os
|
||||||
|
|
||||||
import plugins
|
import plugins
|
||||||
from bridge.reply import Reply, ReplyType
|
from bridge.reply import Reply, ReplyType
|
||||||
@@ -10,23 +11,22 @@ from plugins import *
|
|||||||
|
|
||||||
|
|
||||||
@plugins.register(
|
@plugins.register(
|
||||||
name="dow-markdown",
|
name="dow_markdown",
|
||||||
desire_priority=66,
|
desire_priority=66,
|
||||||
hidden=False,
|
hidden=False,
|
||||||
desc="优化markdown返回结果中的图片和网址链接。",
|
desc="优化markdown返回结果中的图片和网址链接。",
|
||||||
version="0.1",
|
version="0.1",
|
||||||
author="Kubbo",
|
author="Kubbo",
|
||||||
)
|
)
|
||||||
class NiceCoze(Plugin):
|
class dow_markdown(Plugin):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
try:
|
try:
|
||||||
self.handlers[Event.ON_DECORATE_REPLY] = self.on_decorate_reply
|
self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_decorate_reply
|
||||||
logger.info("[Nicecoze] inited.")
|
logger.info("[dow_markdown] inited.")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warn("[Nicecoze] init failed, ignore.")
|
logger.warn("[dow_markdown] init failed, ignore.")
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
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
|
||||||
@@ -37,46 +37,22 @@ class NiceCoze(Plugin):
|
|||||||
# 避免图片无法下载时,重复调用插件导致没有响应的问题
|
# 避免图片无法下载时,重复调用插件导致没有响应的问题
|
||||||
if content.startswith("[DOWNLOAD_ERROR]"):
|
if content.startswith("[DOWNLOAD_ERROR]"):
|
||||||
return
|
return
|
||||||
# 提取Coze返回的Markdown图片链接中的网址,并修改ReplyType为IMAGE_URL,以便CoW自动下载Markdown链接中的图片
|
if bool(re.search(r'\!\[[^\]]+\]\(?',content)):
|
||||||
#if all(x in content for x in ['![', 'http']) and any(x in content for x in ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp']):
|
host = os.environ.get('DIFY_API_BASE', 'http://121.37.155.68:35801')
|
||||||
if 'http' in content and any(x in content for x in ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp']):
|
image_path = re.search(r'!\[.*\]\((.*?)\)',reply.content).group(1)
|
||||||
logger.debug(f"[Nicecoze] starting decorate_markdown_image, content={content}")
|
logger.info(f"提取到的数据==>host:{host},url:{image_path}")
|
||||||
replies = self.decorate_markdown_image(content)
|
reply = Reply(ReplyType.IMAGE_URL, f"{host}{image_path}")
|
||||||
if replies:
|
e_context["reply"] = reply
|
||||||
logger.info(f"[Nicecoze] sending {len(replies)} images ...")
|
e_context.action = EventAction.BREAK_PASS
|
||||||
e_context["reply"].content = "[DOWNLOAD_ERROR]\n" + e_context["reply"].content
|
|
||||||
for reply in replies:
|
|
||||||
channel.send(reply, context)
|
|
||||||
#e_context["reply"] = Reply(ReplyType.TEXT, f"{len(replies)}张图片已发送,收到了吗?")
|
|
||||||
# “x张图片已发送,收到了吗?”提示的初衷是告诉我们画/搜了几张图片以及下载/发送失败了几张图片,可以将e_context["reply"]设置为None关闭该提示!
|
|
||||||
e_context["reply"] = None
|
|
||||||
e_context.action = EventAction.BREAK_PASS
|
|
||||||
return
|
|
||||||
# 提取Coze返回的包含https://s.coze.cn/t/xxx网址的Markdown链接中的图片网址
|
|
||||||
markdown_s_coze_cn = r"([\S\s]*)\!?\[(?P<link_name>.*)\]\((?P<link_url>https\:\/\/s\.coze\.cn\/t\/[\S]*?)\)([\S\s]*)"
|
|
||||||
match_obj_s_coze_cn = re.fullmatch(markdown_s_coze_cn, content)
|
|
||||||
if match_obj_s_coze_cn and match_obj_s_coze_cn.group('link_url'):
|
|
||||||
link_url = match_obj_s_coze_cn.group('link_url')
|
|
||||||
logger.info(f"[Nicecoze] match_obj_s_coze_cn found, link_url={link_url}")
|
|
||||||
response = requests.get(url=link_url, allow_redirects=False)
|
|
||||||
original_url = response.headers.get('Location')
|
|
||||||
if response.status_code in [301, 302] and original_url and any(x in original_url for x in ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp']):
|
|
||||||
logger.info(f"[Nicecoze] match_obj_s_coze_cn found and original_url is a image url, original_url={original_url}")
|
|
||||||
reply = Reply(ReplyType.IMAGE_URL, original_url)
|
|
||||||
e_context["reply"] = reply
|
|
||||||
e_context.action = EventAction.BREAK_PASS
|
|
||||||
return
|
|
||||||
else:
|
|
||||||
logger.info(f"[Nicecoze] match_obj_s_coze_cn found but failed to get original_url or original_url is not a image url, response.status_code={response.status_code}, original_url={original_url}")
|
|
||||||
# 去掉每行结尾的Markdown链接中网址部分的小括号,避免微信误以为“)”是网址的一部分导致微信中无法打开该页面
|
# 去掉每行结尾的Markdown链接中网址部分的小括号,避免微信误以为“)”是网址的一部分导致微信中无法打开该页面
|
||||||
content_list = content.split('\n')
|
content_list = content.split('\n')
|
||||||
new_content_list = [re.sub(r'\((https?://[^\s]+)\)$', r' \1', line) for line in content_list]
|
new_content_list = [re.sub(r'\((https?://[^\s]+)\)$', r' \1', line) for line in content_list]
|
||||||
if new_content_list != content_list:
|
if new_content_list != content_list:
|
||||||
logger.info(f"[Nicecoze] parenthesis in the url has been removed, content={content}")
|
logger.info(f"[dow_markdown] parenthesis in the url has been removed, content={content}")
|
||||||
reply = Reply(ReplyType.TEXT, '\n'.join(new_content_list).strip())
|
reply = Reply(ReplyType.TEXT, '\n'.join(new_content_list).strip())
|
||||||
e_context["reply"] = reply
|
e_context["reply"] = reply
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warn(f"[Nicecoze] on_decorate_reply failed, content={content}, error={e}")
|
logger.warn(f"[dow_markdown] on_decorate_reply failed, content={content}, error={e}")
|
||||||
finally:
|
finally:
|
||||||
e_context.action = EventAction.CONTINUE
|
e_context.action = EventAction.CONTINUE
|
||||||
|
|
||||||
@@ -86,7 +62,7 @@ class NiceCoze(Plugin):
|
|||||||
match_obj_official = re.fullmatch(markdown_image_official, content)
|
match_obj_official = re.fullmatch(markdown_image_official, content)
|
||||||
if match_obj_official and match_obj_official.group('image_url'):
|
if match_obj_official and match_obj_official.group('image_url'):
|
||||||
image_name, image_url = match_obj_official.group('image_name'), match_obj_official.group('image_url')
|
image_name, image_url = match_obj_official.group('image_name'), match_obj_official.group('image_url')
|
||||||
logger.info(f"[Nicecoze] markdown_image_official found, image_name={image_name}, image_url={image_url}")
|
logger.info(f"[dow_markdown] markdown_image_official found, image_name={image_name}, image_url={image_url}")
|
||||||
reply = Reply(ReplyType.IMAGE_URL, image_url)
|
reply = Reply(ReplyType.IMAGE_URL, image_url)
|
||||||
return [reply]
|
return [reply]
|
||||||
# 完全匹配一张Markdown图片(格式:``)
|
# 完全匹配一张Markdown图片(格式:``)
|
||||||
@@ -94,7 +70,7 @@ class NiceCoze(Plugin):
|
|||||||
match_obj_single = re.fullmatch(markdown_image_single, content, re.DOTALL)
|
match_obj_single = re.fullmatch(markdown_image_single, content, re.DOTALL)
|
||||||
if match_obj_single and match_obj_single.group('image_url'):
|
if match_obj_single and match_obj_single.group('image_url'):
|
||||||
image_name, image_url = match_obj_single.group('image_name'), match_obj_single.group('image_url')
|
image_name, image_url = match_obj_single.group('image_name'), match_obj_single.group('image_url')
|
||||||
logger.info(f"[Nicecoze] markdown_image_single found, image_name={image_name}, image_url={image_url}")
|
logger.info(f"[dow_markdown] markdown_image_single found, image_name={image_name}, image_url={image_url}")
|
||||||
reply = Reply(ReplyType.IMAGE_URL, image_url)
|
reply = Reply(ReplyType.IMAGE_URL, image_url)
|
||||||
return [reply]
|
return [reply]
|
||||||
# 匹配多张Markdown图片(格式:`url\n`)
|
# 匹配多张Markdown图片(格式:`url\n`)
|
||||||
@@ -103,13 +79,13 @@ class NiceCoze(Plugin):
|
|||||||
replies = []
|
replies = []
|
||||||
for match in match_iter_multi:
|
for match in match_iter_multi:
|
||||||
image_url = match.group('image_url')
|
image_url = match.group('image_url')
|
||||||
logger.info(f"[Nicecoze] markdown_image_multi found, image_url={image_url}")
|
logger.info(f"[dow_markdown] markdown_image_multi found, image_url={image_url}")
|
||||||
reply = Reply(ReplyType.IMAGE_URL, image_url)
|
reply = Reply(ReplyType.IMAGE_URL, image_url)
|
||||||
replies.append(reply)
|
replies.append(reply)
|
||||||
if replies:
|
if replies:
|
||||||
return replies
|
return replies
|
||||||
if content.startswith('![') and 'http' in content and any(img in content for img in ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp']):
|
if content.startswith('![') and 'http' in content and any(img in content for img in ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp']):
|
||||||
logger.info(f"[Nicecoze] it seems markdown image in the content but not matched, content={content}.")
|
logger.info(f"[dow_markdown] it seems markdown image in the content but not matched, content={content}.")
|
||||||
|
|
||||||
def get_help_text(self, **kwargs):
|
def get_help_text(self, **kwargs):
|
||||||
return "优化Coze返回结果中的图片和网址链接。"
|
return "优化Coze返回结果中的图片和网址链接。"
|
||||||
|
|||||||
Reference in New Issue
Block a user