向飞书消息告警
1、在飞书群中找到告警机器人,并添加机器人。
|
|
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]>