飞书开放平台:https://www.feishu.cn/hc/zh-cn/articles/360024984973-%E5%9C%A8%E7%BE%A4%E8%81%8A%E4%B8%AD%E4%BD%BF%E7%94%A8%E6%9C%BA%E5%99%A8%E4%BA%BA
以下是进行接入飞书预警的个人实例,仅供参考:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2017-06-22 14:32:10
import os
import requests
import json
from json import JSONDecodeError
SENDURL = OA地址
FEISHUURL = 飞书群的机器人的 webhook 地址
"""
使用方法
ret,errmsg = sendwxmsg(content,userid,sender)
"""
'''
def sendwxmsg(content, userid, sender):
ret = False
msg = ''
params = {
'c': 'weChat',
'a': 'sendMessage',
'msgtype': 'text',
'type': 'group',
'userid': userid,
'sender': sender,
'content': content,
}
headers = {
'content-type': "application/json",
'User-Agent': ("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36"
" (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"),
}
try:
r = requests.get(SENDURL, params=params, headers=headers, timeout=15)
except TimeoutError as e:
return (ret, 'TimeoutError')
if r.status_code == requests.codes.ok:
result = r.content.decode('utf-8')
try:
jsond = json.loads(result)
except JSONDecodeError:
return (ret, 'JSONDecodeError')
if jsond.get('code', None) == '0':
ret = True
else:
ret = False
msg = jsond.get('msg', result)
msg = '{}_{}'.format(r.status_code, msg)
return (ret, msg)
'''
"""
飞书消息推送使用方法
ret,errmsg = sendwxmsg_feishu(content, botid)
Method: POST
参数:
{
"title": "Hello Feishu",
"text": "Good Feishu"
}
返回:
{
"ok": true
}
"""
def sendfeishumsg(content,botid,report):
ret = False
msg = ''
url = '{}{}'.format(FEISHUURL, botid)
data = {}
headers = {
'content-type': "application/json",
'User-Agent': ("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36"
" (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"),
}
data['msg_type'] = "post"
data['content'] = {
"post":{
"zh_cn":{
"title":"壁纸官网预警",
"content":[
[
{
"tag":"text",
"text":content
},
{
"tag":"a",
"text":"点击查看",
"href":report
}
]
]
}
}
}
try:
r = requests.post(url, json=data, headers=headers, timeout=15)
except TimeoutError as e:
print(e)
return (ret, 'TimeoutError')
if r.status_code == requests.codes.ok:
result = r.content.decode('utf-8')
try:
jsond = json.loads(result)
except JSONDecodeError:
return (ret, 'JSONDecodeError')
if jsond.get('code', None) == '0':
ret = True
else:
ret = False
msg = jsond.get('msg', result)
print(msg)
msg = '{}_{}'.format(r.status_code, msg)
return (ret, msg)
if __name__ == '__main__':
sendfeishumsg('测试预警发布:','15fe742d-6030-4341-861a-a5ec07d9f8d9','https://www.duba.com/?f=liebaont')
简单效果如下图:
