钉钉消息异常通知

钉钉消息异常通知

支持多线程
支持多进程
支持@多人
支持@所有人

from pprint import pprint

import requests
import json
import threading
import multiprocessing
from datetime import datetime


class DingTalkAlert:
    def __init__(self, webhook_url, secret=None):
        self.webhook_url = webhook_url
        self.secret = secret

    def send_message(self, title, error_message, **kwargs):
        at_all = kwargs.get('at_all', False)
        at_mobiles = kwargs.get('at_mobiles', [])
        timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

        message_content = f"**标题**: {title}\n\n**异常信息**: {error_message}\n\n**发生时间**: {timestamp}"

        # headers = {
        #     'Content-Type': 'application/json'
        # }
        data = {
            "msgtype": "text",
            "text": {
                "title": title,
                "content": message_content
            },
            "at": {
                "isAtAll": at_all,
                "atUserIds": [],
                "atMobiles": at_mobiles
            }
        }
        pprint(data)
        response = requests.post(self.webhook_url, data=json.dumps(data), headers={'Content-Type': 'application/json'})
        print(response.status_code)
        return response.json()

    def send_message_thread(self, title, error_message, **kwargs):
        thread = threading.Thread(target=self.send_message, args=(title, error_message), kwargs=kwargs)
        thread.start()
        thread.join()

    def send_message_process(self, title, error_message, **kwargs):
        process = multiprocessing.Process(target=self.send_message, args=(title, error_message), kwargs=kwargs)
        process.start()
        process.join()


# 使用示例
if __name__ == "__main__":
    webhook_url = 'https://oapi.dingtalk.com/robot/send?access_token='
    alert = DingTalkAlert(webhook_url)

    # 发送消息,@所有人
    alert.send_message("告警通知", "这是一个告警消息,测试阶段,消息打扰,请多多包涵", at_all=True)

    # 发送消息,@具体的多个负责人
    alert.send_message("i告警通知", "这是一个告警消息,测试阶段,消息打扰,请多多包涵", at_mobiles=["1881xxxx363", ])

    # 多线程发送消息,@所有人
    alert.send_message_thread("告警通知", "这是一个多线程告警消息,测试阶段,消息打扰,请多多包涵", at_all=True)

    # 多进程发送消息,@具体的多个负责人
    alert.send_message_process("i告警通知", "这是一个多进程告警消息,测试阶段,消息打扰,请多多包涵", at_mobiles=["1881xxxx363", "13900000000"])

相关推荐

  1. 消息异常通知

    2024-07-10 01:34:06       20 阅读
  2. Python 发送消息(markdown格式)

    2024-07-10 01:34:06       30 阅读
  3. 监控指定任务,结束通知

    2024-07-10 01:34:06       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 01:34:06       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 01:34:06       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 01:34:06       58 阅读
  4. Python语言-面向对象

    2024-07-10 01:34:06       69 阅读

热门阅读

  1. python 学习

    2024-07-10 01:34:06       21 阅读
  2. 【Unix/Linux】Unix/Linux如何查看系统版本

    2024-07-10 01:34:06       20 阅读
  3. 【Unix/Linux】$bash-3.2是什么

    2024-07-10 01:34:06       21 阅读
  4. Win11系统vscode配置C语言环境

    2024-07-10 01:34:06       24 阅读
  5. Mojo有哪些优势和劣势

    2024-07-10 01:34:06       19 阅读
  6. 论文阅读:Large Language Models for Education: A Survey

    2024-07-10 01:34:06       25 阅读
  7. ARM汇编的基础语法

    2024-07-10 01:34:06       24 阅读
  8. postman

    postman

    2024-07-10 01:34:06      20 阅读
  9. Redis

    Redis

    2024-07-10 01:34:06      20 阅读
  10. [Linux安全运维] Linux命令相关

    2024-07-10 01:34:06       26 阅读
  11. PCL 点云最小外接球形包围盒

    2024-07-10 01:34:06       20 阅读
  12. Pytest单元测试系列[v1.0.0][高级技巧]

    2024-07-10 01:34:06       19 阅读
  13. CLIP-EBC:通过增强的逐块分类,CLIP能够准确计数

    2024-07-10 01:34:06       24 阅读