一 夜莺飞书告警有存在问题
1、无法at某人。
2、所有告警级别无法通过颜色进行区分。
二 对夜莺飞书告警进行改造
(一)增加user_id字段
飞书v2机器人at某人是基于user_id实现的,需要在夜莺web用户管理界面增加user_id字段用于填写user_id。
1、修改webapi.conf,增加下面代码
[[ContactKeys]]
Label = "Feishu User ID"
# do not change Key
Key = "feishu_user_id"
2、重启n9e-webapi即可在web页面上看到相应的项

(二)修改告警使用python脚本
修改server.conf,更改内容如下,同时把notify.bak.py的内容再重新替换notify.py

(三)修改python脚本
1、增加获取user_id方式
# phones = {}
userids = {}
for u in users:
# if u.get("phone"):
# phones[u.get("phone")] = 1
contacts = u.get("contacts")
if contacts.get("feishu_robot_token", ""):
tokens[contacts.get("feishu_robot_token", "")] = 1
if contacts.get("feishu_user_id", ""):
userids[contacts.get("feishu_user_id", "")] = 1
2、修改原文本变成卡片形式
if re.findall("S1 Triggered",payload[u'tpls'][u'mailbody.tpl']):
color = "red"
elif re.findall("S2 Triggered",payload[u'tpls'][u'mailbody.tpl']):
color = "yellow"
elif re.findall("S3 Triggered",payload[u'tpls'][u'mailbody.tpl']):
color = "purple"
else:
color = "green"
if len(userids.keys()) != 0 and re.findall("S1 Triggered",payload[u'tpls'][u'mailbody.tpl']):
message = {"content": payload.get('tpls').get("feishu.tpl", "feishu.tpl not found") + "",
"tag": "lark_md"}
else:
message = {"content": payload.get('tpls').get("feishu.tpl", "feishu.tpl not found"),
"tag": "lark_md"}
print userids.keys()
print message
for t in tokens:
url = "https://open.feishu.cn/open-apis/bot/v2/hook/{}".format(t)
body = {
"msg_type": "interactive",
"card": {
"config": {
"wide_screen_mode": True
},
"header": {
"title": {
"tag": "plain_text",
"content": "
告警消息"
},
"template": color
},
"elements": [{"tag": "div",
"text": message }]}
}
三 实现后效果图
(一)实现效果图,实现不同级别不同颜色和严重级别at某人




(二)说明
1、上面只是测试的效果图,本人python脚本能力有限,代码只是提供实现方法参考。
2、也可以修改源码中go实现方法,但是需要手动编译。