#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import json
import sys
import os
import datetime
import base64
import hashlib
import hmac
from time import time
class FeiShuNotify:
def __init__(self, subject, message, access_token=None):
timestamp, sign = FeiShuNotify.encode(access_token)
self.content = {'timestamp': timestamp, 'sign': sign}
self.subject = subject
self.message = message
self.timestamp = timestamp
self.sign = sign
@staticmethod
def encode(access_token):
"""
飞书机器人webhook签名验证加密方法
:return:
"""
# timestamp = str(round(time() * 1000))
timestamp = str(int(time()))
# 拼接timestamp和secret
string_to_sign = '{}\n{}'.format(timestamp, access_token)
hmac_code = hmac.new(string_to_sign.encode("utf-8"), digestmod=hashlib.sha256).digest()
# 对结果进行base64处理
sign = base64.b64encode(hmac_code).decode('utf-8')
return timestamp, sign
def to_json(self):
"""
飞书消息卡片返回格式
:return:
"""
return {
"msg_type": "interactive",
"card": {
"config": {
"wide_screen_mode": True
},
"header": {
"title": {
"tag": "plain_text",
"content": self.subject
},
"template": "blue"
},
"elements": [
{
"tag": "markdown",
"content": self.message,
}
]
},
'timestamp': self.timestamp,
'sign': self.sign
}
def send_message(message, subject):
"""
向飞书webhook url发送告警消息
:param message:
:param subject:
:return:
"""
payload_message = FeiShuNotify(subject, message, access_token="C8tW9fHPXct0S9TDOuNTLb").to_json()
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST",
"换成你的机器人地址",
headers=headers, data=json.dumps(payload_message))
return response
if __name__ == '__main__':
text = sys.argv[3]
subject = sys.argv[2]
send_message(text, subject)

Thomas2 年前
发表在:SQL Server2008报错:值不能为空[S35]
Thomas2 年前
发表在:解决Composer Installing dependencies from lock file你这都看不清了
1114 年前
发表在:Cetnos7 新手安装ZABBIX4.0.5详解教程有人嘛
Tyreunorm2020-08-27 22:51
发表在:php5.6 连接SQL SERVER<a href=https://mega...
耗子2020-04-08 15:02
发表在:写给正在创业的自己看到了,想写点。可能我开过这网站了,以后...
Thomas2019-11-19 19:57
发表在:twcms您的IP地址已改变,为了安全考虑,请重新登录这个好
酷喵2016-08-03 15:22
发表在:我家侄女成长记前排支持啦!
五爷2015-12-01 03:26
发表在:关于centos中的相关查看命令@admin:呵呵
admin2015-12-01 01:49
发表在:关于centos中的相关查看命令非常不错