Explorar el Código

Update keyword.py

befantasy hace 2 años
padre
commit
d8bfa77705
Se han modificado 1 ficheros con 21 adiciones y 5 borrados
  1. 21 5
      plugins/keyword/keyword.py

+ 21 - 5
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