2024-04-13 17:21:59 +08:00
|
|
|
|
# encoding:utf-8
|
|
|
|
|
|
|
2024-05-07 15:10:27 +08:00
|
|
|
|
import re
|
2024-08-01 14:08:56 +08:00
|
|
|
|
import requests
|
2024-08-03 02:27:37 +08:00
|
|
|
|
import os
|
2024-04-13 17:21:59 +08:00
|
|
|
|
|
|
|
|
|
|
import plugins
|
|
|
|
|
|
from bridge.reply import Reply, ReplyType
|
|
|
|
|
|
from common.log import logger
|
|
|
|
|
|
from plugins import *
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@plugins.register(
|
2024-08-03 02:50:38 +08:00
|
|
|
|
name="dowmd",
|
2024-05-07 15:10:27 +08:00
|
|
|
|
desire_priority=66,
|
2024-08-02 16:50:44 +08:00
|
|
|
|
desc="优化markdown返回结果中的图片和网址链接。",
|
|
|
|
|
|
version="0.1",
|
|
|
|
|
|
author="Kubbo",
|
2024-04-13 17:21:59 +08:00
|
|
|
|
)
|
2024-08-03 02:50:38 +08:00
|
|
|
|
class dowmd(Plugin):
|
2024-04-13 17:21:59 +08:00
|
|
|
|
def __init__(self):
|
|
|
|
|
|
super().__init__()
|
|
|
|
|
|
try:
|
2024-08-03 02:27:37 +08:00
|
|
|
|
self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_decorate_reply
|
|
|
|
|
|
logger.info("[dow_markdown] inited.")
|
2024-04-13 17:21:59 +08:00
|
|
|
|
except Exception as e:
|
2024-08-03 02:27:37 +08:00
|
|
|
|
logger.warn("[dow_markdown] init failed, ignore.")
|
2024-04-13 17:21:59 +08:00
|
|
|
|
raise e
|
|
|
|
|
|
def on_decorate_reply(self, e_context: EventContext):
|
|
|
|
|
|
if e_context["reply"].type != ReplyType.TEXT:
|
|
|
|
|
|
return
|
|
|
|
|
|
try:
|
2024-05-07 15:10:27 +08:00
|
|
|
|
channel = e_context["channel"]
|
|
|
|
|
|
context = e_context["context"]
|
|
|
|
|
|
content = e_context["reply"].content.strip()
|
|
|
|
|
|
# 避免图片无法下载时,重复调用插件导致没有响应的问题
|
2024-06-02 23:51:25 +08:00
|
|
|
|
if content.startswith("[DOWNLOAD_ERROR]"):
|
2024-05-07 15:10:27 +08:00
|
|
|
|
return
|
2024-08-03 02:38:38 +08:00
|
|
|
|
has_md = re.search(r'\!\[[^\]]+\]\(?',content)
|
|
|
|
|
|
logger.info(f"{content}是否含有md语法==>{has_md}")
|
|
|
|
|
|
if has_md:
|
2024-08-03 02:27:37 +08:00
|
|
|
|
host = os.environ.get('DIFY_API_BASE', 'http://121.37.155.68:35801')
|
|
|
|
|
|
image_path = re.search(r'!\[.*\]\((.*?)\)',reply.content).group(1)
|
|
|
|
|
|
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
|
2024-04-13 17:21:59 +08:00
|
|
|
|
# 去掉每行结尾的Markdown链接中网址部分的小括号,避免微信误以为“)”是网址的一部分导致微信中无法打开该页面
|
2024-05-07 15:10:27 +08:00
|
|
|
|
content_list = content.split('\n')
|
|
|
|
|
|
new_content_list = [re.sub(r'\((https?://[^\s]+)\)$', r' \1', line) for line in content_list]
|
|
|
|
|
|
if new_content_list != content_list:
|
2024-08-03 02:27:37 +08:00
|
|
|
|
logger.info(f"[dow_markdown] parenthesis in the url has been removed, content={content}")
|
2024-05-07 15:10:27 +08:00
|
|
|
|
reply = Reply(ReplyType.TEXT, '\n'.join(new_content_list).strip())
|
2024-04-13 17:21:59 +08:00
|
|
|
|
e_context["reply"] = reply
|
2024-05-07 15:10:27 +08:00
|
|
|
|
except Exception as e:
|
2024-08-03 02:27:37 +08:00
|
|
|
|
logger.warn(f"[dow_markdown] on_decorate_reply failed, content={content}, error={e}")
|
2024-05-07 15:10:27 +08:00
|
|
|
|
finally:
|
|
|
|
|
|
e_context.action = EventAction.CONTINUE
|
2024-04-13 17:21:59 +08:00
|
|
|
|
def get_help_text(self, **kwargs):
|
2024-08-03 02:28:04 +08:00
|
|
|
|
return "优化返回结果中的图片和网址链接。"
|