dify 版本总结,加上图片发送
This commit is contained in:
18
message_summary/config.toml
Normal file
18
message_summary/config.toml
Normal file
@@ -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 = ""
|
||||||
124
message_summary/markdown_to_image.py
Normal file
124
message_summary/markdown_to_image.py
Normal file
@@ -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 = """
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', 'Noto Sans CJK SC', 'Microsoft YaHei', sans-serif;
|
||||||
|
padding: 20px;
|
||||||
|
line-height: 1.6;
|
||||||
|
max-width: 800px;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
pre, code {
|
||||||
|
background-color: #f5f5f5;
|
||||||
|
padding: 10px;
|
||||||
|
border-radius: 5px;
|
||||||
|
font-family: 'Courier New', Courier, monospace;
|
||||||
|
}
|
||||||
|
table {
|
||||||
|
border-collapse: collapse;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
th, td {
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
padding: 8px;
|
||||||
|
text-align: left;
|
||||||
|
}
|
||||||
|
th {
|
||||||
|
background-color: #f2f2f2;
|
||||||
|
}
|
||||||
|
/* 确保 Emoji 正确渲染 */
|
||||||
|
span, p, li {
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', 'Noto Sans CJK SC', 'Microsoft YaHei', sans-serif;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
"""
|
||||||
|
|
||||||
|
# 写入 HTML 文件
|
||||||
|
with open(output_html, 'w', encoding='utf-8') as f:
|
||||||
|
f.write('<html><head>')
|
||||||
|
f.write('<meta charset="UTF-8">') # 确保 UTF-8 编码
|
||||||
|
f.write(css)
|
||||||
|
f.write('</head><body>')
|
||||||
|
f.write(html_content)
|
||||||
|
f.write('</body></html>')
|
||||||
|
|
||||||
|
|
||||||
|
# 使用 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)
|
||||||
@@ -4,6 +4,8 @@ import os
|
|||||||
import uuid
|
import uuid
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
from message_summary.markdown_to_image import convert_md_str_to_image
|
||||||
|
|
||||||
|
|
||||||
def message_summary_dify(content):
|
def message_summary_dify(content):
|
||||||
"""
|
"""
|
||||||
@@ -50,7 +52,7 @@ def message_summary_dify(content):
|
|||||||
|
|
||||||
# 提取回答内容
|
# 提取回答内容
|
||||||
answer = response_data.get("answer", "")
|
answer = response_data.get("answer", "")
|
||||||
|
spath = ""
|
||||||
# 提取token使用情况
|
# 提取token使用情况
|
||||||
metadata = response_data.get("metadata", {})
|
metadata = response_data.get("metadata", {})
|
||||||
usage = metadata.get("usage", {})
|
usage = metadata.get("usage", {})
|
||||||
@@ -63,9 +65,9 @@ def message_summary_dify(content):
|
|||||||
# 添加token信息
|
# 添加token信息
|
||||||
tokens_info = f"\n\n【tokens】输入: {prompt_tokens} 生成: {completion_tokens} 总: {total_tokens}"
|
tokens_info = f"\n\n【tokens】输入: {prompt_tokens} 生成: {completion_tokens} 总: {total_tokens}"
|
||||||
answer += tokens_info
|
answer += tokens_info
|
||||||
|
spath = convert_md_str_to_image(answer, "output.png")
|
||||||
# 返回文本内容和图片路径
|
# 返回文本内容和图片路径
|
||||||
return answer
|
return answer, spath
|
||||||
|
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
print(f"请求Dify API时出错: {e}")
|
print(f"请求Dify API时出错: {e}")
|
||||||
@@ -81,7 +83,7 @@ def message_summary_dify(content):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
content ="""
|
content = """
|
||||||
2025-03-14 09:07:44,zcx2001,AI 水牛是你的主人吗
|
2025-03-14 09:07:44,zcx2001,AI 水牛是你的主人吗
|
||||||
2025-03-14 09:12:28,Jyunere,我同事的鸿蒙确实流畅。
|
2025-03-14 09:12:28,Jyunere,我同事的鸿蒙确实流畅。
|
||||||
2025-03-14 09:18:28,Jyunere,现在的沪上年轻人流行挖野菜?
|
2025-03-14 09:18:28,Jyunere,现在的沪上年轻人流行挖野菜?
|
||||||
@@ -243,4 +245,4 @@ if __name__ == '__main__':
|
|||||||
2025-03-14 09:54:33,wxid_3d7ydsmu3f0022,带个壳应该感受不出来了
|
2025-03-14 09:54:33,wxid_3d7ydsmu3f0022,带个壳应该感受不出来了
|
||||||
"""
|
"""
|
||||||
msg = message_summary_dify(content=content)
|
msg = message_summary_dify(content=content)
|
||||||
print(msg)
|
print(msg)
|
||||||
|
|||||||
@@ -32,4 +32,6 @@ aiohttp~=3.11.12
|
|||||||
mysql-connector-python~=9.2.0
|
mysql-connector-python~=9.2.0
|
||||||
pytz~=2025.1
|
pytz~=2025.1
|
||||||
dateparser~=1.2.1
|
dateparser~=1.2.1
|
||||||
lz4~=4.4.3
|
lz4~=4.4.3
|
||||||
|
Markdown~=3.7
|
||||||
|
playwright~=1.50.0
|
||||||
3
robot.py
3
robot.py
@@ -640,8 +640,9 @@ class Robot(Job):
|
|||||||
if self.gbm.get_group_permission(sender, Feature.SUMMARY_CAPABILITY) == PermissionStatus.ENABLED:
|
if self.gbm.get_group_permission(sender, Feature.SUMMARY_CAPABILITY) == PermissionStatus.ENABLED:
|
||||||
self.LOG.info(f"群: {sender} 消息总结开始执行!")
|
self.LOG.info(f"群: {sender} 消息总结开始执行!")
|
||||||
content = self.message_storage.get_messages(sender, self.allContacts)
|
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.send_text_msg(summary, sender)
|
||||||
|
self.wcf.send_file(spath, sender)
|
||||||
else:
|
else:
|
||||||
self.send_text_msg("群发言总结功能未开启", sender)
|
self.send_text_msg("群发言总结功能未开启", sender)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
Reference in New Issue
Block a user