diff --git a/README.md b/README.md index 77dc1e7..65bd1ba 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ -一款优化coze-discord-proxy返回结果的chatgpt-on-wechat插件。 - -安装方法: - -```sh -#installp https://github.com/wangxyd/nicecoze.git -#scanp -``` - +一款优化coze-discord-proxy返回结果的chatgpt-on-wechat插件。 + +安装方法: + +```sh +#installp https://github.com/wangxyd/nicecoze.git +#scanp +``` + 配置方法:无需任何配置! \ No newline at end of file diff --git a/nicecoze.py b/nicecoze.py index abf1882..50d985a 100644 --- a/nicecoze.py +++ b/nicecoze.py @@ -1,11 +1,8 @@ # encoding:utf-8 -import json -import os -from urllib.parse import urlparse +import re import plugins -from bridge.context import ContextType from bridge.reply import Reply, ReplyType from common.log import logger from plugins import * @@ -13,10 +10,10 @@ from plugins import * @plugins.register( name="Nicecoze", - desire_priority=100, - hidden=True, + desire_priority=66, + hidden=False, desc="一款优化coze-discord-proxy返回结果的插件。", - version="1.0", + version="1.1", author="空心菜", ) class Nicecoze(Plugin): @@ -32,37 +29,60 @@ class Nicecoze(Plugin): def on_decorate_reply(self, e_context: EventContext): if e_context["reply"].type != ReplyType.TEXT: return - reply = e_context["reply"] try: - content_list = reply.content.strip().split('\n') + channel = e_context["channel"] + context = e_context["context"] + content = e_context["reply"].content.strip() + # 避免图片无法下载时,重复调用插件导致没有响应的问题 + if content.startswith("[NICECOZE提醒您图片下载失败了,点击网址将就看吧]"): + return # 提取CDP返回的Markdown图片链接中的网址,并修改ReplyType为IMAGE_URL,以便CoW自动下载Markdown链接中的图片 - if len(content_list)==2 and self.is_url(content_list[0]): - if content_list[1] == f"![Image]({content_list[0]})": - reply = Reply(ReplyType.IMAGE_URL, content_list[0]) - e_context["reply"] = reply - e_context.action = EventAction.CONTINUE - logger.info(f"[Nicecoze] Change ReplyType from TEXT to IMAGE_URL: {content_list[0]}") - else: - logger.debug(f"[Nicecoze] URL in content but not a markdown image url.") + if all(x in content for x in ['![', 'http']) and any(x in content for x in ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp']): + logger.debug(f"[Nicecoze] starting decorate_markdown_image, content={content}") + replies = self.decorate_markdown_image(content) + if replies: + logger.info(f"[Nicecoze] sending {len(replies)} images ...") + e_context["reply"].content = "[NICECOZE提醒您图片下载失败了,点击网址将就看吧]\n" + e_context["reply"].content + for reply in replies: + channel.send(reply, context) + e_context["reply"] = Reply(ReplyType.TEXT, f"{len(replies)}张图片已发送,收到了吗?") + e_context.action = EventAction.BREAK_PASS + return # 去掉每行结尾的Markdown链接中网址部分的小括号,避免微信误以为“)”是网址的一部分导致微信中无法打开该页面 - else: - new_content_list = [re.sub(r'\((https?://[^\s]+)\)$', r' \1', line) for line in content_list] - reply = Reply(ReplyType.TEXT, '\n'.join(new_content_list)) + 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: + logger.info(f"[Nicecoze] parenthesis in the url has been removed, content={content}") + reply = Reply(ReplyType.TEXT, '\n'.join(new_content_list).strip()) e_context["reply"] = reply - e_context.action = EventAction.CONTINUE - except Exception: - pass + except Exception as e: + logger.warn(f"[Nicecoze] on_decorate_reply failed, content={content}, error={e}") + finally: + e_context.action = EventAction.CONTINUE - def is_url(self, string): - """ - 判断字符串是否为URL的urllib库方法 - """ - try: - result = urlparse(string) - return all([result.scheme, result.netloc]) - except ValueError: - return False + def decorate_markdown_image(self, content): + # 完全匹配一张Markdown图片(格式:`![name](url)`) + markdown_image1 = r"\!\[(?P.*)\]\((?Phttps?\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}(:[0-9]{1,5})?(\/[\S]*)\.(jpg|jpeg|png|gif|bmp|webp)(\?[\S]*)?)\)" + match_obj1 = re.fullmatch(markdown_image1, content, re.DOTALL) + if match_obj1 and match_obj1.group('image_url'): + image_name, image_url = match_obj1.group('image_name'), match_obj1.group('image_url') + logger.info(f"[Nicecoze] markdown_image1 found, image_name={image_name}, image_url={image_url}") + reply = Reply(ReplyType.IMAGE_URL, image_url) + return [reply] + # 匹配多张Markdown图片(格式:`url\n![Image](url)`) + markdown_image2 = r"(?Phttps?\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}(:[0-9]{1,5})?(\/[\S]*)\.(jpg|jpeg|png|gif|bmp|webp)(\?[\S]*)?)\n*\!\[Image\]\((?P=image_url)\)" + match_iter2 = re.finditer(markdown_image2, content) + replies = [] + for match in match_iter2: + image_url = match.group('image_url') + logger.info(f"[Nicecoze] markdown_image2 found, image_url={image_url}") + reply = Reply(ReplyType.IMAGE_URL, image_url) + replies.append(reply) + if replies: + return replies + if content.startswith('![') and 'http' in content and any(img in content for img in ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp']): + logger.info(f"[Nicecoze] it seems markdown image in the content but not matched, content={content}.") def get_help_text(self, **kwargs): - return "一款优化coze-discord-proxy返回结果的插件。" + return "优化coze-discord-proxy返回结果的插件。"