From b52790038b187fe39ecf74f534d61dab5e1c68ab Mon Sep 17 00:00:00 2001 From: CMJNB <53365071+CMJNB@users.noreply.github.com> Date: Wed, 10 Jul 2024 11:30:11 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9send=5Fmsg=E7=9A=84keywor?= =?UTF-8?q?d=E5=88=B0=E5=87=BD=E6=95=B0=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- feapder/utils/tools.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/feapder/utils/tools.py b/feapder/utils/tools.py index a0ccbf1..733e1f7 100644 --- a/feapder/utils/tools.py +++ b/feapder/utils/tools.py @@ -2684,13 +2684,12 @@ def feishu_warning(message, message_prefix=None, rate_limit=None, url=None, user return False -def send_msg(msg, level="DEBUG", message_prefix=""): +def send_msg(msg, level="DEBUG", message_prefix="", keyword="feapder报警系统\n"): if setting.WARNING_LEVEL == "ERROR": if level.upper() != "ERROR": return if setting.DINGDING_WARNING_URL: - keyword = "feapder报警系统\n" dingding_warning(keyword + msg, message_prefix=message_prefix) if setting.EMAIL_RECEIVER: @@ -2700,11 +2699,9 @@ def send_msg(msg, level="DEBUG", message_prefix=""): email_warning(msg, message_prefix=message_prefix, title=title) if setting.WECHAT_WARNING_URL: - keyword = "feapder报警系统\n" wechat_warning(keyword + msg, message_prefix=message_prefix) if setting.FEISHU_WARNING_URL: - keyword = "feapder报警系统\n" feishu_warning(keyword + msg, message_prefix=message_prefix) From 80a104f16625b2d1ff63aef94edc814f82a59b71 Mon Sep 17 00:00:00 2001 From: CMJNB <53365071+CMJNB@users.noreply.github.com> Date: Wed, 10 Jul 2024 11:30:48 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=96=B0=E5=A2=9EQmsg=E9=85=B1=E6=8A=A5?= =?UTF-8?q?=E8=AD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...46\345\217\212\347\233\221\346\216\247.md" | 14 +++++ feapder/setting.py | 4 ++ feapder/templates/project_template/setting.py | 4 ++ feapder/utils/tools.py | 52 +++++++++++++++++++ 4 files changed, 74 insertions(+) diff --git "a/docs/source_code/\346\212\245\350\255\246\345\217\212\347\233\221\346\216\247.md" "b/docs/source_code/\346\212\245\350\255\246\345\217\212\347\233\221\346\216\247.md" index 5756a0d..87dbc69 100644 --- "a/docs/source_code/\346\212\245\350\255\246\345\217\212\347\233\221\346\216\247.md" +++ "b/docs/source_code/\346\212\245\350\255\246\345\217\212\347\233\221\346\216\247.md" @@ -86,6 +86,20 @@ EMAIL_RECEIVER = "" # 收件人 支持列表,可指定多个 4. 将本邮箱账号添加到白名单中 +## Qmsg酱报警 + +Qmsg酱是一个QQ消息推送机器人,用来通知自己消息的免费服务。 + +可以参考文档:https://qmsg.zendee.cn/docs/api/ + +```python +# QMSG报警 +QMSG_WARNING_URL = "" # qmsg机器人api +QMSG_WARNING_QQ = "" # 指定要接收消息的QQ号或者QQ群。多个以英文逗号分割,例如:12345,12346,支持列表,可指定多人 +QMSG_WARNING_BOT = "" # 机器人的QQ号 +``` + + ## 报警间隔及报警级别 框架会对相同的报警进行过滤,防止刷屏,默认的报警时间间隔为1小时,可通过以下配置修改: diff --git a/feapder/setting.py b/feapder/setting.py index 93d9b89..1c366e8 100644 --- a/feapder/setting.py +++ b/feapder/setting.py @@ -182,6 +182,10 @@ WECHAT_WARNING_URL = "" # 企业微信机器人api WECHAT_WARNING_PHONE = "" # 报警人 将会在群内@此人, 支持列表,可指定多人 WECHAT_WARNING_ALL = False # 是否提示所有人, 默认为False +# QMSG报警 +QMSG_WARNING_URL = "" # qmsg机器人api +QMSG_WARNING_QQ = "" # 指定要接收消息的QQ号或者QQ群。多个以英文逗号分割,例如:12345,12346,支持列表,可指定多人 +QMSG_WARNING_BOT = "" # 机器人的QQ号 # 时间间隔 WARNING_INTERVAL = 3600 # 相同报警的报警时间间隔,防止刷屏; 0表示不去重 WARNING_LEVEL = "DEBUG" # 报警级别, DEBUG / INFO / ERROR diff --git a/feapder/templates/project_template/setting.py b/feapder/templates/project_template/setting.py index 6109790..a80cbe1 100644 --- a/feapder/templates/project_template/setting.py +++ b/feapder/templates/project_template/setting.py @@ -164,6 +164,10 @@ # WECHAT_WARNING_URL = "" # 企业微信机器人api # WECHAT_WARNING_PHONE = "" # 报警人 将会在群内@此人, 支持列表,可指定多人 # WECHAT_WARNING_ALL = False # 是否提示所有人, 默认为False +# # QMSG报警 +# QMSG_WARNING_URL = "" # qmsg机器人api +# QMSG_WARNING_QQ = "" # 指定要接收消息的QQ号或者QQ群。多个以英文逗号分割,例如:12345,12346,支持列表,可指定多人 +# QMSG_WARNING_BOT = "" # 机器人的QQ号 # # 时间间隔 # WARNING_INTERVAL = 3600 # 相同报警的报警时间间隔,防止刷屏; 0表示不去重 # WARNING_LEVEL = "DEBUG" # 报警级别, DEBUG / INFO / ERROR diff --git a/feapder/utils/tools.py b/feapder/utils/tools.py index 733e1f7..98d4c09 100644 --- a/feapder/utils/tools.py +++ b/feapder/utils/tools.py @@ -2684,6 +2684,55 @@ def feishu_warning(message, message_prefix=None, rate_limit=None, url=None, user return False +def qmsg_warning( + message, + message_prefix=None, + rate_limit=None, + url=None, + user_qq=None, + bot_qq=None +): + """qmsg报警""" + + # 为了加载最新的配置 + rate_limit = rate_limit if rate_limit is not None else setting.WARNING_INTERVAL + url = url or setting.QMSG_WARNING_URL + user_qq = user_qq or setting.QMSG_WARNING_QQ + bot_qq = bot_qq or setting.QMSG_WARNING_BOT + + if isinstance(user_qq, list): + user_qq = ','.join(map(str, user_qq)) + + if not all([url, message]): + return + + if reach_freq_limit(rate_limit, url, user_qq, message_prefix or message): + log.info("报警时间间隔过短,此次报警忽略。 内容 {}".format(message)) + return + + data = { + "msg": message, + "qq": user_qq, + "bot": bot_qq, + } + + headers = {"Content-Type": "application/json"} + + try: + response = requests.post( + url, headers=headers, data=json.dumps(data).encode("utf8") + ) + result = response.json() + response.close() + if result.get("code") == 0: + return True + else: + raise Exception(result.get("reason")) + except Exception as e: + log.error("报警发送失败。 报警内容 {}, error: {}".format(message, e)) + return False + + def send_msg(msg, level="DEBUG", message_prefix="", keyword="feapder报警系统\n"): if setting.WARNING_LEVEL == "ERROR": if level.upper() != "ERROR": @@ -2704,6 +2753,9 @@ def send_msg(msg, level="DEBUG", message_prefix="", keyword="feapder报警系统 if setting.FEISHU_WARNING_URL: feishu_warning(keyword + msg, message_prefix=message_prefix) + if setting.QMSG_WARNING_URL: + qmsg_warning(keyword + msg, message_prefix=message_prefix) + ###################