1 先创建一个卡片消息
根据自己的需要创建一个消息卡片:
可以在 卡片源代码 中看到这个卡片的代码信息
2 Python脚本
2.1 告警测试
test.py 文件
"""
飞书群机器人发送通知
"""
import time
import urllib3
import datetime
import json
import logging
import requests
from flask import Flask,jsonify,request
# 获取当前时间
# current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# 配置日志记录器
logging.basicConfig(level=logging.INFO)
Logger = logging.getLogger(__name__)
urllib3.disable_warnings()
webhook_url="https://open.feishu.cn/open-apis/bot/v2/hook/xxxxx"
class FlybookRobotAlert():
def __init__(self):
self.webhook = webhook_url
self.headers = {
"Content-Type": "application/json; charset=UTF-8"}
def post_to_robot(self, post_data):
'''
给飞书机器人发送请求
:param data:
:return:
'''
try:
resp = requests.request(method="POST", url=self.webhook, data=post_data, headers=self.headers).json()
if resp.get("StatusCode") == 0 and resp.get("msg") == "success":
Logger.info(f"飞书通知发送成功,msg={
resp}")
else:
Logger.warning(f"飞书通知发送失败,{
resp}")
except Exception as e:
Logger.warning("飞书通知发送异常")
Logger.warning(e)
pass
def send_message(self, msg, color):
# 飞书通知标题
robot_headers = "LibreNMS告警"
# msg:告警信息
# color:来定义卡片的颜色
execute_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# 定义卡片
card = json.dumps({
"config"