From d8bfa777055fe3eea299e4e8481ce8e41838043f Mon Sep 17 00:00:00 2001 From: befantasy <31535803+befantasy@users.noreply.github.com> Date: Fri, 15 Sep 2023 16:56:51 +0800 Subject: [PATCH] Update keyword.py --- plugins/keyword/keyword.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/plugins/keyword/keyword.py b/plugins/keyword/keyword.py index 2dc87ff..5c9f48e 100644 --- a/plugins/keyword/keyword.py +++ b/plugins/keyword/keyword.py @@ -2,7 +2,7 @@ import json import os - +import requests import plugins from bridge.context import ContextType from bridge.reply import Reply, ReplyType @@ -51,15 +51,31 @@ class Keyword(Plugin): content = e_context["context"].content.strip() logger.debug("[keyword] on_handle_context. content: %s" % content) if content in self.keyword: - logger.debug(f"[keyword] 匹配到关键字【{content}】") + logger.info(f"[keyword] 匹配到关键字【{content}】") reply_text = self.keyword[content] # 判断匹配内容的类型 - if (reply_text.startswith("http://") or reply_text.startswith("https://")) and any(reply_text.endswith(ext) for ext in [".jpg", ".jpeg", ".png", ".gif", ".webp"]): - # 如果是以 http:// 或 https:// 开头,且.jpg/.jpeg/.png/.gif结尾,则认为是图片 URL + if (reply_text.startswith("http://") or reply_text.startswith("https://")) and any(reply_text.endswith(ext) for ext in [".jpg", ".jpeg", ".png", ".gif", ".img"]): + # 如果是以 http:// 或 https:// 开头,且".jpg", ".jpeg", ".png", ".gif", ".img"结尾,则认为是图片 URL。 reply = Reply() reply.type = ReplyType.IMAGE_URL reply.content = reply_text + + elif (reply_text.startswith("http://") or reply_text.startswith("https://")) and any(reply_text.endswith(ext) for ext in [".pdf", ".doc", ".docx", ".xls", "xlsx",".zip", ".rar"]): + # 如果是以 http:// 或 https:// 开头,且".pdf", ".doc", ".docx", ".xls", "xlsx",".zip", ".rar"结尾,则下载文件到tmp目录并发送给用户 + file_path = "tmp" + if not os.path.exists(file_path): + os.makedirs(file_path) + file_name = reply_text.split("/")[-1] # 获取文件名 + file_path = os.path.join(file_path, file_name) + response = requests.get(reply_text) + with open(file_path, "wb") as f: + f.write(response.content) + #channel/wechat/wechat_channel.py和channel/wechat_channel.py中缺少ReplyType.FILE类型。 + reply = Reply() + reply.type = ReplyType.FILE + reply.content = file_path + else: # 否则认为是普通文本 reply = Reply() @@ -68,7 +84,7 @@ class Keyword(Plugin): e_context["reply"] = reply e_context.action = EventAction.BREAK_PASS # 事件结束,并跳过处理context的默认逻辑 - + def get_help_text(self, **kwargs): help_text = "关键词过滤" return help_text