时间:2020-02-20来源:系统城作者:电脑系统城
飞书提供了丰富的api来实现消息的通知,包括文本消息、图片消息、富文本消息,本次介绍使用飞书api发送文本消息,以下是实现思路
飞书API地址:https://open.feishu.cn/document/ukTMukTMukTM/uITNz4iM1MjLyUzM
1.需要获取三个授权凭证
2.根据zabbix报警的收信人手机号获取user_id,用于后面在群里@相关负责人,或者直接发给某个责任人
3.chat_id用于发送给指定的群,这里我提供两种方法获取chat_id,后面会介绍
4.传入zabbix报警消息,并艾特相关负责人发送到飞书群里或者个人
登录开发者后台,在“我的应用”页面创建企业自建应用。进入企业自建应用详情页,获取App ID和App Secret。
一种方法是通过企业自建应用方式获取,另一种是通过应用商店应用获取,这里我使用第一种方法,直接创建应用即可
def gettenant_access_token():
tokenurl="https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/"
headers={"Content-Type":"application/json"}
data={
"app_id":"cli_9ec625abcdefg",
"app_secret":"f716Gi27Yi25n5K0Wbafgwghhstv"
}
request=requests.post(url=tokenurl,headers=headers,json=data)
response=json.loads(request.content)['tenant_access_token']
return response
user_id可以根据注册的手机号或邮箱获取,可以在zabbix中定义用户的手机号,然后传入参数获取user_id
def getuserid(tenant_access_token):
#mobiles="15101234584"
userurl="https://open.feishu.cn/open-apis/user/v1/batch_get_id?mobiles=%s"%mobiles
headers={"Authorization":"Bearer %s"%tenant_access_token}
request=requests.get(url=userurl,headers=headers)
response=json.loads(request.content)['data']['mobile_users'][mobiles][0]['user_id']
return response
这里我提供两种方法获取chat_id,一种是将机器人加入到群里,获取群信息中的chat_id;另一种是通过机器人创建群聊获取群信息,当然还有其他的方法,这里我就不过多介绍了,我将使用第一种方法来获取chat_id
def getchatid(tenant_access_token):
#获取chatid
chaturl="https://open.feishu.cn/open-apis/chat/v4/list?page_size=20"
headers={"Authorization":"Bearer %s"%tenant_access_token,"Content-Type":"application/json"}
request=requests.get(url=chaturl,headers=headers)
response=json.loads(request.content)['data']['groups'][0]['chat_id']
return response
这里需要三个参数,一个是user_id,一个是chat_id,另一个是tenant_access_token,并传入报警信息即可发送
def sendmes(user_id,chat_id,tenant_access_token):
#向群里发送消息
sendurl="https://open.feishu.cn/open-apis/message/v4/send/"
headers={"Authorization":"Bearer %s"%tenant_access_token,"Content-Type":"application/json"}
data={"chat_id":chat_id,
"msg_type":"text",
"content":{
"text":"%s<at user_id=\"%s\">test</at>"%(messages,user_id)
}
}
#给个人发送消息
# data={"user_id":user_id,
# "msg_type":"text",
# "content":{
# "text":"%s<at user_id=\"%s\">test</at>"%(messages,user_id)
# }
# }
request=requests.post(url=sendurl,headers=headers,json=data)
print(request.content)
注意参数顺序不能乱
也就是用户注册飞书的手机号
后续会添加带有图片信息的报警,完整代码请访问github组织遮阳笔记
2023-07-08
win10系统任务栏颜色透明怎么更改教程2023-07-05
台式电脑音箱没有声音怎么解决 新买的音响插电脑上没声音怎么设置2023-07-05
miui15系统怎么彻底删除垃圾文件教程最近有很多小伙伴在使用联想笔记本的时候出现了问题,说自己不小心把联想笔记本键盘锁住了,想要解开却不知道联想笔记本键盘锁了怎么解开,不要着急,万能的系统城小编为大家整理好了win10键盘被锁住按什么键恢复的方法,下面...
2023-07-03
很多用户在使用电脑时候会遇到耳机插到电脑上,但是却没有声音的现象,很多小伙伴不知道遇到这一问题该如何解决,其实出现这种情况原因有很多,比如插口问题、设置问题等等,下面和小编一起来看看具体的解决办法吧~...
2023-06-01