feat(notification): 企业微信群机器人支持图片推送

- 新增 ImageUtils 类,提供图像处理相关工具方法
- 在 MAA.py 中集成 ImageUtils,用于获取和处理通知图片
- 在 notification.py 中实现 CompanyWebHookBotPushImage 方法,支持企业微信群机器人推送图片
- 修改测试通知方法,增加图片推送测试
This commit is contained in:
2025-06-11 17:36:11 +08:00
parent 428b849bcc
commit 3c371cd079
5 changed files with 117 additions and 0 deletions

View File

@@ -40,6 +40,7 @@ from typing import Union, List, Dict
from app.core import Config, MaaConfig, MaaUserConfig
from app.services import Notify, Crypto, System, skland_sign_in
from app.utils.ImageUtils import ImageUtils
class MaaManager(QObject):
@@ -1998,6 +1999,13 @@ class MaaManager(QObject):
"好羡慕~\n\nAUTO_MAA 敬上",
Config.get(Config.notify_CompanyWebHookBotUrl),
)
app_path = Config.apppath
image_path = app_path / "resources/images/notification/six_star.png"
image_base64 = ImageUtils.get_base64_from_file(image_path)
image_md5 = ImageUtils.calculate_md5_from_file(image_path)
Notify.CompanyWebHookBotPushImage(
image_base64, image_md5, user_data["Notify"]["CompanyWebHookBotUrl"]
)
# 发送用户单独通知
if user_data["Notify"]["Enabled"] and user_data["Notify"]["IfSendSixStar"]:

View File

@@ -40,6 +40,7 @@ from plyer import notification
from app.core import Config
from app.services.security import Crypto
from app.utils.ImageUtils import ImageUtils
class Notification(QObject):
@@ -271,6 +272,83 @@ class Notification(QObject):
)
return f"使用企业微信群机器人推送通知时出错:{err}"
def CompanyWebHookBotPushImage(self, base64_content, md5_content, webhook_url):
"""使用企业微信群机器人推送图片通知
Args:
base64_content (str): 图片的base64编码字符串
md5_content (str): 图片的MD5值
webhook_url (str): 群组机器人WebHook地址
Returns:
bool: 推送是否成功
"""
if webhook_url == "":
logger.error("请正确设置企业微信群机器人的WebHook地址")
self.push_info_bar.emit(
"error",
"企业微信群机器人通知推送异常",
"请正确设置企业微信群机器人的WebHook地址",
-1,
)
return False
try:
# 构造请求数据
data = {
"msgtype": "image",
"image": {
"base64": base64_content,
"md5": md5_content
}
}
# 发送请求
for _ in range(3):
try:
response = requests.post(
url=webhook_url,
json=data,
timeout=10,
)
info = response.json()
break
except Exception as e:
err = e
time.sleep(0.1)
else:
logger.error(f"推送企业微信群机器人图片时出错:{err}")
self.push_info_bar.emit(
"error",
"企业微信群机器人图片推送失败",
f"使用企业微信群机器人推送图片时出错:{err}",
-1,
)
return False
if info["errcode"] == 0:
logger.info("企业微信群机器人推送图片成功")
return True
else:
logger.error(f"企业微信群机器人推送图片失败:{info}")
self.push_info_bar.emit(
"error",
"企业微信群机器人图片推送失败",
f"使用企业微信群机器人推送图片时出错:{info}",
-1,
)
return False
except Exception as e:
logger.error(f"推送企业微信群机器人图片时出错:{e}")
self.push_info_bar.emit(
"error",
"企业微信群机器人图片推送失败",
f"使用企业微信群机器人推送图片时出错:{e}",
-1,
)
return False
def send_test_notification(self):
"""发送测试通知到所有已启用的通知渠道"""
# 发送系统通知
@@ -307,6 +385,16 @@ class Notification(QObject):
"这是 AUTO_MAA 外部通知测试信息。如果你看到了这段内容,说明 AUTO_MAA 的通知功能已经正确配置且可以正常工作!",
Config.get(Config.notify_CompanyWebHookBotUrl),
)
app_path = Config.app_path
# 拼接图片路径
image_path = app_path / "resources/images/notification/test_notify.png"
image_base64 = ImageUtils.get_base64_from_file(image_path)
image_md5 = ImageUtils.calculate_md5_from_file(image_path)
self.CompanyWebHookBotPushImage(
image_base64,
image_md5,
Config.get(Config.notify_CompanyWebHookBotUrl),
)
return True

21
app/utils/ImageUtils.py Normal file
View File

@@ -0,0 +1,21 @@
import base64
import hashlib
class ImageUtils:
@staticmethod
def get_base64_from_file(image_path):
"""从本地文件读取并返回base64编码字符串"""
with open(image_path, "rb") as f:
return base64.b64encode(f.read()).decode("utf-8")
@staticmethod
def calculate_md5_from_file(image_path):
"""从本地文件读取并返回md5值hex字符串"""
with open(image_path, "rb") as f:
return hashlib.md5(f.read()).hexdigest()
@staticmethod
def calculate_md5_from_base64(base64_content):
"""从base64字符串计算md5"""
image_data = base64.b64decode(base64_content)
return hashlib.md5(image_data).hexdigest()