suppoert s.coze.cn

This commit is contained in:
wangxyd
2024-08-01 14:08:56 +08:00
parent 423c3bec64
commit 43daf731d1
2 changed files with 22 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
一款优化Coze返回结果中的图片和网址链接的chatgpt-on-wechat插件包含两个功能
+ 提取Coze包括coze.com和coze.cn返回的Markdown图片链接中的网址并修改ReplyType为IMAGE_URL以便CoW自动下载Markdown链接中的图片
+ 提取Coze返回的包含 https://s.coze.cn/t/xxx 网址的Markdown链接中的图片网址并修改ReplyType为IMAGE_URL以便CoW自动下载Markdown链接中的图片
+ 去掉每行结尾的Markdown链接中网址部分的小括号避免微信误以为“)”是网址的一部分导致微信中无法打开该页面。

View File

@@ -1,6 +1,7 @@
# encoding:utf-8
import re
import requests
import plugins
from bridge.reply import Reply, ReplyType
@@ -13,7 +14,7 @@ from plugins import *
desire_priority=66,
hidden=False,
desc="优化Coze返回结果中的图片和网址链接。",
version="1.3",
version="1.5",
author="空心菜",
)
class NiceCoze(Plugin):
@@ -46,9 +47,27 @@ class NiceCoze(Plugin):
e_context["reply"].content = "[DOWNLOAD_ERROR]\n" + e_context["reply"].content
for reply in replies:
channel.send(reply, context)
e_context["reply"] = Reply(ReplyType.TEXT, f"{len(replies)}张图片已发送,收到了吗?")
#e_context["reply"] = Reply(ReplyType.TEXT, f"{len(replies)}张图片已发送,收到了吗?")
# “x张图片已发送收到了吗”提示的初衷是告诉我们画/搜了几张图片以及下载/发送失败了几张图片可以将e_context["reply"]设置为None关闭该提示
e_context["reply"] = None
e_context.action = EventAction.BREAK_PASS
return
# 提取Coze返回的包含https://s.coze.cn/t/xxx网址的Markdown链接中的图片网址
markdown_s_coze_cn = r"([\S\s]*)\!?\[(?P<link_name>.*)\]\((?P<link_url>https\:\/\/s\.coze\.cn\/t\/[\S]*?)\)([\S\s]*)"
match_obj_s_coze_cn = re.fullmatch(markdown_s_coze_cn, content)
if match_obj_s_coze_cn and match_obj_s_coze_cn.group('link_url'):
link_url = match_obj_s_coze_cn.group('link_url')
logger.info(f"[Nicecoze] match_obj_s_coze_cn found, link_url={link_url}")
response = requests.get(url=link_url, allow_redirects=False)
original_url = response.headers.get('Location')
if response.status_code in [301, 302] and original_url and any(x in original_url for x in ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp']):
logger.info(f"[Nicecoze] match_obj_s_coze_cn found and original_url is a image url, original_url={original_url}")
reply = Reply(ReplyType.IMAGE_URL, original_url)
e_context["reply"] = reply
e_context.action = EventAction.BREAK_PASS
return
else:
logger.info(f"[Nicecoze] match_obj_s_coze_cn found but failed to get original_url or original_url is not a image url, response.status_code={response.status_code}, original_url={original_url}")
# 去掉每行结尾的Markdown链接中网址部分的小括号避免微信误以为“)”是网址的一部分导致微信中无法打开该页面
content_list = content.split('\n')
new_content_list = [re.sub(r'\((https?://[^\s]+)\)$', r' \1', line) for line in content_list]