diff --git a/plugins/dify/main.py b/plugins/dify/main.py index 6ca0a40..7427cce 100644 --- a/plugins/dify/main.py +++ b/plugins/dify/main.py @@ -18,6 +18,10 @@ from utils.media_downloader import MediaDownloader from utils.string_utils import remove_trailing_content from wechat_ipad import WechatAPIClient +# 常见的图片和视频文件扩展名 +IMAGE_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp'} +VIDEO_EXTENSIONS = {'.mp4', '.avi', '.mov', '.wmv', '.flv', '.mkv', '.webm'} + class DifyPlugin(MessagePluginInterface): """Dify AI聊天插件""" @@ -151,7 +155,13 @@ class DifyPlugin(MessagePluginInterface): # 判断是否为本地文件路径 if os.path.isfile(response): # 如果是文件路径,使用发送文件方法 - await bot.send_image_message((roomid if roomid else sender), Path(response)) + file_type = self.check_file_type(response) + if file_type == 1: + await bot.send_image_message((roomid if roomid else sender), Path(response)) + elif file_type == 2: + await bot.send_video_message((roomid if roomid else sender), Path(response)) + else: + return False, "获取媒资失败" else: # 如果是普通文本,则在长度大于100字时,转为图片发送 if len(response) > 800: @@ -218,7 +228,14 @@ class DifyPlugin(MessagePluginInterface): # 判断是否为本地文件路径 if os.path.isfile(response): # 如果是文件路径,使用发送文件方法 - await bot.send_image_message((roomid if roomid else sender), Path(response)) + # 判断是视频还是图片: + file_type = self.check_file_type(response) + if file_type == 1: + await bot.send_image_message((roomid if roomid else sender), Path(response)) + elif file_type == 2: + await bot.send_video_message((roomid if roomid else sender), Path(response)) + else: + return False, "获取媒资失败" else: # 如果是普通文本,则在长度大于100字时,转为图片发送 if len(response) > 800: @@ -453,3 +470,19 @@ class DifyPlugin(MessagePluginInterface): """重置所有会话上下文""" self.conversations.clear() self.last_activity.clear() + + def check_file_type(self, file_path) -> int: + if os.path.isfile(file_path): + # 获取文件扩展名并转换为小写 + file_extension = os.path.splitext(file_path)[1].lower() + + if file_extension in IMAGE_EXTENSIONS: + return 1 + # 在此处添加发送图片的逻辑 + elif file_extension in VIDEO_EXTENSIONS: + return 2 + # 在此处添加发送视频的逻辑 + else: + return 0 + else: + return 0