From 905532b681a96ad2b2989d3abe2eba4c98b09e8c Mon Sep 17 00:00:00 2001 From: befantasy <31535803+befantasy@users.noreply.github.com> Date: Fri, 15 Sep 2023 16:21:27 +0800 Subject: [PATCH 1/6] Update chat_channel.py to support ReplyType.FILE --- channel/chat_channel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/channel/chat_channel.py b/channel/chat_channel.py index 4aa17be..ceada35 100644 --- a/channel/chat_channel.py +++ b/channel/chat_channel.py @@ -241,7 +241,7 @@ class ChatChannel(Channel): reply.content = reply_text elif reply.type == ReplyType.ERROR or reply.type == ReplyType.INFO: reply.content = "[" + str(reply.type) + "]\n" + reply.content - elif reply.type == ReplyType.IMAGE_URL or reply.type == ReplyType.VOICE or reply.type == ReplyType.IMAGE: + elif reply.type == ReplyType.IMAGE_URL or reply.type == ReplyType.VOICE or reply.type == ReplyType.IMAGE or reply.type == ReplyType.FILE: pass else: logger.error("[WX] unknown reply type: {}".format(reply.type)) From 6bd286e8d51f68fefdcaa6ed62193c22531fba16 Mon Sep 17 00:00:00 2001 From: befantasy <31535803+befantasy@users.noreply.github.com> Date: Fri, 15 Sep 2023 16:22:46 +0800 Subject: [PATCH 2/6] Update wechat_channel.py to support ReplyType.FILE --- channel/wechat/wechat_channel.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index 4d01d92..061440a 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -208,3 +208,7 @@ class WechatChannel(ChatChannel): image_storage.seek(0) itchat.send_image(image_storage, toUserName=receiver) logger.info("[WX] sendImage, receiver={}".format(receiver)) + elif reply.type == ReplyType.FILE: # 新增文件回复类型 + file_storage = reply.content + itchat.send_file(file_storage, toUserName=receiver) + logger.info("[WX] sendFile, receiver={}".format(receiver)) 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 3/6] 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 From 151e8c69f92db510402581e8437216388b6a40d8 Mon Sep 17 00:00:00 2001 From: befantasy <31535803+befantasy@users.noreply.github.com> Date: Fri, 15 Sep 2023 18:22:10 +0800 Subject: [PATCH 4/6] Update keyword.py --- plugins/keyword/keyword.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/plugins/keyword/keyword.py b/plugins/keyword/keyword.py index 5c9f48e..87cd054 100644 --- a/plugins/keyword/keyword.py +++ b/plugins/keyword/keyword.py @@ -75,6 +75,12 @@ class Keyword(Plugin): reply = Reply() reply.type = ReplyType.FILE reply.content = file_path + + elif (reply_text.startswith("http://") or reply_text.startswith("https://")) and any(reply_text.endswith(ext) for ext in [".mp4"]): + # 如果是以 http:// 或 https:// 开头,且".mp4"结尾,则下载视频到tmp目录并发送给用户 + reply = Reply() + reply.type = ReplyType.VIDEO_URL + reply.content = reply_text else: # 否则认为是普通文本 From a74aa12641b7c100983cbe2174df6e9ee089accd Mon Sep 17 00:00:00 2001 From: befantasy <31535803+befantasy@users.noreply.github.com> Date: Fri, 15 Sep 2023 18:37:05 +0800 Subject: [PATCH 5/6] Update wechat_channel.py --- channel/wechat/wechat_channel.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index 061440a..b8c1ac6 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -212,3 +212,20 @@ class WechatChannel(ChatChannel): file_storage = reply.content itchat.send_file(file_storage, toUserName=receiver) logger.info("[WX] sendFile, receiver={}".format(receiver)) + elif reply.type == ReplyType.VIDEO: # 新增视频回复类型 + video_storage = reply.content + itchat.send_video(video_storage, toUserName=receiver) + logger.info("[WX] sendFile, receiver={}".format(receiver)) + elif reply.type == ReplyType.VIDEO_URL: # 新增视频URL回复类型 + video_url = reply.content + logger.debug(f"[WX] start download video, video_url={video_url}") + video_res = requests.get(video_url, stream=True) + video_storage = io.BytesIO() + size = 0 + for block in video_res.iter_content(1024): + size += len(block) + video_storage.write(block) + logger.info(f"[WX] download video success, size={size}, video_url={video_url}") + video_storage.seek(0) + itchat.send_video(video_storage, toUserName=receiver) + logger.info("[WX] sendVideo url={}, receiver={}".format(video_url, receiver)) From 3dd83aa6b7b43d152c4bad23af1b1b7bde1e967f Mon Sep 17 00:00:00 2001 From: befantasy <31535803+befantasy@users.noreply.github.com> Date: Fri, 15 Sep 2023 18:38:31 +0800 Subject: [PATCH 6/6] Update chat_channel.py --- channel/chat_channel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/channel/chat_channel.py b/channel/chat_channel.py index ceada35..42530ab 100644 --- a/channel/chat_channel.py +++ b/channel/chat_channel.py @@ -241,7 +241,7 @@ class ChatChannel(Channel): reply.content = reply_text elif reply.type == ReplyType.ERROR or reply.type == ReplyType.INFO: reply.content = "[" + str(reply.type) + "]\n" + reply.content - elif reply.type == ReplyType.IMAGE_URL or reply.type == ReplyType.VOICE or reply.type == ReplyType.IMAGE or reply.type == ReplyType.FILE: + elif reply.type == ReplyType.IMAGE_URL or reply.type == ReplyType.VOICE or reply.type == ReplyType.IMAGE or reply.type == ReplyType.FILE or reply.type == ReplyType.VIDEO or reply.type == ReplyType.VIDEO_URL: pass else: logger.error("[WX] unknown reply type: {}".format(reply.type))