通过飞书发送应用消息,及时收到线上异常消息
总的来说,飞书消息发送的前戏,要比企业微信和钉钉稍复杂
相关连接
参数准备
代码示例
# -*- coding: utf-8 -*-
"""
@File : demo.py
@Date : 2023-06-22
"""
import json
import requests
def get_access_token(app_id, app_secret):
"""
自建应用获取 tenant_access_token
https://open.feishu.cn/document/server-docs/authentication-management/access-token/tenant_access_token_internal
:param app_id: 应用唯一标识
:param app_secret: 应用秘钥
:return:
{
"code": 0,
"msg": "success",
"tenant_access_token": "t-xxx",
"expire": 7140
}
"""
url = 'https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal'
params = {
'app_id': app_id,
'app_secret': app_secret
}
res = requests.post(url, params=params)
return res.json()
def send_message(access_token, body, params):
"""
发送消息
https://open.feishu.cn/document/server-docs/im-v1/message/create
:param access_token:
:param body: 消息体
:param params: 查询参数 {"receive_id_type":"open_id"}
:return:
"""
url = 'https://open.feishu.cn/open-apis/im/v1/messages'
headers = {
'Authorization': 'Bearer ' + access_token
}
res = requests.post(url, params=params, headers=headers, json=body)
return res.json()
if __name__ == '__main__':
# App ID
app_id = '<App ID>'
# App Secret
app_secret = '<App Secret>'
# OpenId
open_id = '<OpenId>'
token = get_access_token(app_id, app_secret)
print(token)
params = {"receive_id_type": "open_id"}
payload = {
"receive_id": open_id,
"msg_type": "text",
"content": json.dumps({
"text": "你好,飞书!"
})
}
res = send_message(token['tenant_access_token'], payload, params)
print(res)
发送截图