diff --git a/message_summary/config.toml b/message_summary/config.toml new file mode 100644 index 0000000..8898e09 --- /dev/null +++ b/message_summary/config.toml @@ -0,0 +1,18 @@ +[Summmary] +enable = true + +api-key = "app-McGLzBhBjeBCSEi7n83MtuTo" # Dify的API Key +base-url = "http://192.168.2.240/v1" #Dify API接口base url + +commands = ["深度总结", "群聊总结"] +command-tip = """-----Bot----- +💬AI聊天指令: +深度总结 +""" + +price = 0 # 用一次扣积分,如果0则不扣 + +# Http代理设置 +# 格式: http://用户名:密码@代理地址:代理端口 +# 例如:http://127.0.0.1:7890 +http-proxy = "" \ No newline at end of file diff --git a/message_summary/markdown_to_image.py b/message_summary/markdown_to_image.py new file mode 100644 index 0000000..7d4c1a4 --- /dev/null +++ b/message_summary/markdown_to_image.py @@ -0,0 +1,124 @@ +import markdown +from playwright.sync_api import sync_playwright +import os + + +# 将 Markdown 字符串转换为 HTML +def md_str_to_html(md_content, output_html): + """ + 将 Markdown 字符串转换为 HTML 文件,并添加支持中文和 Emoji 的样式。 + + :param md_content: 输入的 Markdown 字符串 + :param output_html: 输出的 HTML 文件路径 + """ + # 转换 Markdown 为 HTML,启用额外功能(如表格、代码高亮) + html_content = markdown.markdown(md_content, extensions=['extra', 'codehilite']) + + # 添加基本的 HTML 结构和样式,支持中文和 Emoji + css = """ + + """ + + # 写入 HTML 文件 + with open(output_html, 'w', encoding='utf-8') as f: + f.write('
') + f.write('') # 确保 UTF-8 编码 + f.write(css) + f.write('') + f.write(html_content) + f.write('') + + +# 使用 Playwright 将 HTML 渲染并截图 +def html_to_image(html_file, output_image): + """ + 使用 Playwright 加载 HTML 文件并截图。 + + :param html_file: 输入的 HTML 文件路径 + :param output_image: 输出的图片文件路径 + """ + with sync_playwright() as p: + browser = p.chromium.launch(executable_path=r"C:\Users\Liu-OPEN\AppData\Local\Google\Chrome\Application\chrome.exe") + page = browser.new_page() + + # 加载本地的 HTML 文件 + page.goto(f'file://{os.path.abspath(html_file)}') + + # 设置 viewport(可选,根据需要调整) + page.set_viewport_size({"width": 1200, "height": 800}) + + # 截图 + page.screenshot(path=output_image, full_page=True) + + browser.close() + + +# 主函数:从字符串转换 Markdown 到图片 +def convert_md_str_to_image(md_content, output_image): + """ + 将 Markdown 字符串转换为图片。 + + :param md_content: 输入的 Markdown 字符串 + :param output_image: 输出的图片文件路径 + """ + # 中间生成的 HTML 文件 + temp_html = 'temp_output.html' + + # 第一步:将 Markdown 字符串转换为 HTML + md_str_to_html(md_content, temp_html) + + # 第二步:将 HTML 渲染为图片 + html_to_image(temp_html, output_image) + + # 可选:删除临时的 HTML 文件 + os.remove(temp_html) + print(f"图片已生成:{output_image}") + return os.path.abspath(output_image) + +# 示例使用 +if __name__ == "__main__": + # 示例 Markdown 字符串(包含中文和 Emoji) + md_content = """ + # 示例标题 + 这是一个示例 Markdown 字符串,包含中文和 Emoji 😊🚀。 + + ## 子标题 + - 项目 1:学习 Python 🐍 + - 项目 2:编写代码 💻 + + ### 代码示例 + ```python + print("你好,世界!🌍") + """ + spath = convert_md_str_to_image(md_content, "output.png") + print(spath) \ No newline at end of file diff --git a/message_summary/message_summary_dify.py b/message_summary/message_summary_dify.py index 42deefb..33492b1 100644 --- a/message_summary/message_summary_dify.py +++ b/message_summary/message_summary_dify.py @@ -4,6 +4,8 @@ import os import uuid from datetime import datetime +from message_summary.markdown_to_image import convert_md_str_to_image + def message_summary_dify(content): """ @@ -50,7 +52,7 @@ def message_summary_dify(content): # 提取回答内容 answer = response_data.get("answer", "") - + spath = "" # 提取token使用情况 metadata = response_data.get("metadata", {}) usage = metadata.get("usage", {}) @@ -63,9 +65,9 @@ def message_summary_dify(content): # 添加token信息 tokens_info = f"\n\n【tokens】输入: {prompt_tokens} 生成: {completion_tokens} 总: {total_tokens}" answer += tokens_info - + spath = convert_md_str_to_image(answer, "output.png") # 返回文本内容和图片路径 - return answer + return answer, spath except requests.exceptions.RequestException as e: print(f"请求Dify API时出错: {e}") @@ -81,7 +83,7 @@ def message_summary_dify(content): if __name__ == '__main__': - content =""" + content = """ 2025-03-14 09:07:44,zcx2001,AI 水牛是你的主人吗 2025-03-14 09:12:28,Jyunere,我同事的鸿蒙确实流畅。 2025-03-14 09:18:28,Jyunere,现在的沪上年轻人流行挖野菜? @@ -243,4 +245,4 @@ if __name__ == '__main__': 2025-03-14 09:54:33,wxid_3d7ydsmu3f0022,带个壳应该感受不出来了 """ msg = message_summary_dify(content=content) - print(msg) \ No newline at end of file + print(msg) diff --git a/requirements.txt b/requirements.txt index a9caee2..b6f0ae3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -32,4 +32,6 @@ aiohttp~=3.11.12 mysql-connector-python~=9.2.0 pytz~=2025.1 dateparser~=1.2.1 -lz4~=4.4.3 \ No newline at end of file +lz4~=4.4.3 +Markdown~=3.7 +playwright~=1.50.0 \ No newline at end of file diff --git a/robot.py b/robot.py index 2d59555..726abbd 100644 --- a/robot.py +++ b/robot.py @@ -640,8 +640,9 @@ class Robot(Job): if self.gbm.get_group_permission(sender, Feature.SUMMARY_CAPABILITY) == PermissionStatus.ENABLED: self.LOG.info(f"群: {sender} 消息总结开始执行!") content = self.message_storage.get_messages(sender, self.allContacts) - summary = message_summary_dify(content) + summary, spath = message_summary_dify(content) self.send_text_msg(summary, sender) + self.wcf.send_file(spath, sender) else: self.send_text_msg("群发言总结功能未开启", sender) except Exception as e: