# common function: feishu report infomation
# file_name is feishu_report.py
import json
import datetime
import requests
class get_feishu_oj:
def __init__(self, url):
self.url = url
def _report(self, content, report_level, url=None,):
url = self.url
date = datetime.datetime.now().strftime("%H:%M:%S")
#title show the report date_str
title = date + "--[" + report_level + "]"
data_json = json.dumps({
"msg_type": "post",
"content":
{"post":
{"zh_cn":
{"title": title,
"content": [[
{"tag": "text",
"text": content
},
]]}}}})
r_json = requests.post(url, data_json)
def info(self, content):
self._report(content=content, report_level="INFO")
def debug(self, content):
self._report(content=content, report_level="DEBUG")
def warn(self, content):
self._report(content=content, report_level="WARIN")
def error(self, content):
self._report(content=content, report_level="ERROR")
# 需要多几个的话
def get_feishu_test():
feishu = get_feishu_oj(feishu_report_url['test'])
return feishu
def get_feishu_product():
feishu = get_feishu_oj(feishu_report_url['product'])
return feishu
# url填入自己的机器人webhook
feishu_report_url = {
"test": "https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxx2"
"product" : "https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxx1"
}
添加机器人
调用测试
import feishu_report
feishu=feishu_report.get_feishu_test()
feishu.info("Info test\ntestinfo")
feishu.warn("Hello Mother Fuck")
效果
这么写的调用的方式类似于
logging模块
生成一个log实例对象后,
也是用
log.error(“str_a”),
log.info(“str_b”)可以打印对应的日志,
生成一个feishu对象,
不同级别的告警调用feishu对象的info,error,warn,debug函数
也可以生成不同的机器人,告警到不同的群里
只需要输入需要告警的字符串内容就行,时间自动生成,告警等级调整也很方便