From d326467a04dfd2c58bd28311b0a06af309b7ec39 Mon Sep 17 00:00:00 2001 From: liuwei Date: Tue, 27 May 2025 17:29:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=88=AA=E5=9B=BE=EF=BC=8C?= =?UTF-8?q?=E4=BD=86=E6=98=AF=E4=BC=9A=E6=AE=8B=E7=95=99=E5=A4=A7=E9=87=8F?= =?UTF-8?q?=E7=9A=84=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/dify/main.py | 6 ++++-- utils/markdown_to_image.py | 17 +++++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/plugins/dify/main.py b/plugins/dify/main.py index 32d57f9..a248c6a 100644 --- a/plugins/dify/main.py +++ b/plugins/dify/main.py @@ -154,7 +154,8 @@ class DifyPlugin(MessagePluginInterface): # 如果是普通文本,则在长度大于100字时,转为图片发送 if len(response) > 400: # 转图片 - respath = await convert_md_str_to_image(response, "dify_output.jpg") + output_image = f"dify_output_{int(time.time())}.png" + respath = await convert_md_str_to_image(response, output_image) await bot.send_image_message((roomid if roomid else sender), Path(respath)) else: # 如果是普通文本,使用发送文本方法 @@ -217,7 +218,8 @@ class DifyPlugin(MessagePluginInterface): # 如果是普通文本,则在长度大于100字时,转为图片发送 if len(response) > 400: # 转图片 - respath = await convert_md_str_to_image(response, "dify_output.jpg") + output_image = f"dify_output_{int(time.time())}.png" + respath = await convert_md_str_to_image(response, output_image) await bot.send_image_message((roomid if roomid else sender), Path(respath)) else: # 如果是普通文本,使用发送文本方法 diff --git a/utils/markdown_to_image.py b/utils/markdown_to_image.py index 4244835..02bf75c 100644 --- a/utils/markdown_to_image.py +++ b/utils/markdown_to_image.py @@ -211,12 +211,17 @@ async def html_to_image(html_file, output_image): browser = await p.chromium.launch(executable_path=browser_path) # 业务逻辑不变 - page = await browser.new_page() - await page.goto(f'file://{os.path.abspath(html_file)}') - await page.set_viewport_size({"width": 750, "height": 800}) - await page.wait_for_timeout(500) - await page.screenshot(path=output_image, full_page=True) - await browser.close() + try: + page = await browser.new_page() + await page.goto(f'file://{os.path.abspath(html_file)}') + await page.set_viewport_size({"width": 750, "height": 800}) + await page.wait_for_timeout(500) + await page.screenshot(path=output_image, full_page=True) + except Exception as e: + print(f"截图失败: {e}") + finally: + await page.close() + await browser.close() except Exception as e: print(f"浏览器操作失败: {e}")