diff --git a/app/models/MAA.py b/app/models/MAA.py index 954b3cb..6d225d7 100644 --- a/app/models/MAA.py +++ b/app/models/MAA.py @@ -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"]: diff --git a/app/services/notification.py b/app/services/notification.py index 21486d5..a841934 100644 --- a/app/services/notification.py +++ b/app/services/notification.py @@ -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 diff --git a/app/utils/ImageUtils.py b/app/utils/ImageUtils.py new file mode 100644 index 0000000..2c1eb88 --- /dev/null +++ b/app/utils/ImageUtils.py @@ -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() diff --git a/resources/images/notification/six_star.png b/resources/images/notification/six_star.png new file mode 100644 index 0000000..2ef8c38 Binary files /dev/null and b/resources/images/notification/six_star.png differ diff --git a/resources/images/notification/test_notify.png b/resources/images/notification/test_notify.png new file mode 100644 index 0000000..2410fcc Binary files /dev/null and b/resources/images/notification/test_notify.png differ