飞书消息告警【python开发案例】

向飞书消息告警

1、在飞书群中找到告警机器人,并添加机器人。

2、复制webhook地址,用请求的方式往这个地址发送告警消息。
3、python案例教程【修改`webhook地址`,代码可运行,**童叟无欺**】
import requests
import json
class FeishuAlert():
    def __init__(self):
        self.webhook="替换成个人的飞书群webhook地址,即可运行"
        self.headers={'Content-Type': 'application/json'}

    def post_to_robot(self):
        # webhook:飞书群地址url
        webhook=self.webhook
        # headers: 请求头
        headers=self.headers

        # alert_headers: 告警消息标题
        alert_headers="飞书告警"
        # alert_content: 告警消息内容,用户可根据自身业务内容,定义告警内容
        alert_content="🌟告警内容👏!请尽快查阅💪!"
        # message_body: 请求信息主体
        message_body={
            "msg_type": "interactive",
            "card": {
              "config": {
                "wide_screen_mode": True
              },
              "elements": [
                {
                  "tag": "div",
                  "text": {
                    "content":alert_content,
                    "tag": "lark_md"
                  }
                }
              ],
              "header": {
                "template": "red",
                "title": {
                  "content":alert_headers,
                  "tag": "plain_text"
                }
              }
            }}
        response = requests.request("POST", webhook, headers=headers, data=json.dumps(message_body))
        print(response)

if __name__ == '__main__':
    alert=FeishuAlert()
    alert.post_to_robot()
    '''
    	"msg_type"参数说明: 飞书告警目前只支持类型4个参数
			post  富文本
			image 图片
			share_chat	分享群名片
			interactive	 消息卡片
		"template"参数说明: 主体颜色
	'''
	

运行结果:<Response [200]>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值