通过飞书发送应用消息,及时收到线上异常消息
总的来说,飞书消息发送的前戏,要比企业微信和钉钉稍复杂
相关连接
- 官网 https://www.feishu.cn/
- 管理后台 https://www.feishu.cn/admin
- 开放平台 https://open.feishu.cn/
参数准备
- 首先,在 飞书开放平台 创建应用,提交审批
- 再到 飞书管理后台 审批应用
- 回到 飞书开放平台 添加应用能力 / 机器人,获取应用凭证
App_ID
和App_Secret
- 在 飞书开放平台 ,通过接口调试,获取
open_id
代码示例
# -*- coding: utf-8 -*-
"""
@File : demo.py
@Date : 2023-06-22
"""import json
import requestsdef get_access_token(app_id, app_secret):"""自建应用获取 tenant_access_tokenhttps://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 IDapp_id = '<App ID>'# App Secretapp_secret = '<App Secret>'# OpenIdopen_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)
发送截图