详细的飞书机器人接口使用说明,包含完整的API调用示例
飞书API提供了完整的企业通信解决方案,支持群组管理、消息发送、历史查询等功能。本指南将详细介绍如何使用这些API接口。
首先需要配置应用凭证并获取访问令牌。
curl -X POST 'https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal' \
-H 'Content-Type: application/json' \
-d '{
"app_id": "your_app_id",
"app_secret": "your_app_secret"
}'
{
"code": 0,
"msg": "success",
"tenant_access_token": "t-g104498ae...",
"expire": 7200
}
获取机器人所在的所有群组信息。
curl -X GET 'https://open.feishu.cn/open-apis/im/v1/chats?page_size=20' \ -H 'Authorization: Bearer t-your_token_here'
| 参数 | 类型 | 说明 |
|---|---|---|
| page_size | int | 分页大小,默认20,最大100 |
| page_token | string | 分页标记,用于获取下一页 |
{
"code": 0,
"msg": "success",
"data": {
"items": [{
"chat_id": "oc_5e6688c96088d5d605f3658d2ccc88fc",
"name": "测试群组",
"description": "这是一个测试群组",
"avatar": "https://...",
"external": false
}],
"has_more": false,
"page_token": ""
}
}
向指定群组发送文本消息,支持@用户功能。
curl -X POST 'https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=chat_id' \
-H 'Authorization: Bearer t-your_token_here' \
-H 'Content-Type: application/json' \
-d '{
"receive_id": "oc_5e6688c96088d5d605f3658d2ccc88fc",
"msg_type": "text",
"content": "{\"text\":\"Hello, 这是一条测试消息!\"}"
}'
curl -X POST 'https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=chat_id' \
-H 'Authorization: Bearer t-your_token_here' \
-H 'Content-Type: application/json' \
-d '{
"receive_id": "oc_5e6688c96088d5d605f3658d2ccc88fc",
"msg_type": "text",
"content": "{\"text\":\"所有人 请注意重要通知!\"}"
}'
| 参数 | 类型 | 说明 |
|---|---|---|
| receive_id | string | 群组或用户ID |
| msg_type | string | 消息类型:text/image/interactive |
| content | string | 消息内容(JSON字符串) |
发送图片消息需要先上传图片获取image_key,然后发送消息。
curl -X POST 'https://open.feishu.cn/open-apis/im/v1/images' \ -H 'Authorization: Bearer t-your_token_here' \ -F 'image_type=message' \ -F 'image=@/path/to/your/image.png'
{
"code": 0,
"msg": "success",
"data": {
"image_key": "img_v3_02n3_453e9693-9833-4d13-96ef-b383477c49bg"
}
}
curl -X POST 'https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=chat_id' \
-H 'Authorization: Bearer t-your_token_here' \
-H 'Content-Type: application/json' \
-d '{
"receive_id": "oc_5e6688c96088d5d605f3658d2ccc88fc",
"msg_type": "image",
"content": "{\"image_key\":\"img_v3_02n3_453e9693-9833-4d13-96ef-b383477c49bg\"}"
}'
发送富文本消息卡片,支持复杂的交互内容。
curl -X POST 'https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=chat_id' \
-H 'Authorization: Bearer t-your_token_here' \
-H 'Content-Type: application/json' \
-d '{
"receive_id": "oc_5e6688c96088d5d605f3658d2ccc88fc",
"msg_type": "interactive",
"content": "{\"elements\":[{\"tag\":\"markdown\",\"content\":\"**重要通知**\\n\\n请所有同事注意:\\n- 明天召开全员会议\\n- 时间:下午2点\\n- 地点:会议室A\\n\\n所有人 \"}]}"
}'
curl -X POST 'https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=chat_id' \
-H 'Authorization: Bearer t-your_token_here' \
-H 'Content-Type: application/json' \
-d '{
"receive_id": "oc_5e6688c96088d5d605f3658d2ccc88fc",
"msg_type": "interactive",
"content": "{\"type\":\"template\",\"data\":{\"template_id\":\"your_template_id\",\"template_version_name\":\"1.0.0\",\"template_variable\":{\"title\":\"系统通知\",\"content\":\"您有新的任务待处理\"}}}"
}'
撤回已发送的消息(需要在24小时内)。
curl -X DELETE 'https://open.feishu.cn/open-apis/im/v1/messages/om_dc13264520392913993dd051dba21dcf' \ -H 'Authorization: Bearer t-your_token_here'
{
"code": 0,
"msg": "success",
"data": {}
}
{
"code": 230002,
"msg": "Bot/User can NOT be out of the chat.",
"error": {
"log_id": "202509121619514C0A9A4827E58F88583A"
}
}
获取指定群组的历史消息记录。
curl -X GET 'https://open.feishu.cn/open-apis/im/v1/messages?container_id=oc_5e6688c96088d5d605f3658d2ccc88fc&container_id_type=chat&start_time=1608594809&end_time=1609296809&page_size=50&sort_type=ByCreateTimeDesc' \ -H 'Authorization: Bearer t-your_token_here'
| 参数 | 类型 | 说明 |
|---|---|---|
| container_id | string | 群组ID |
| container_id_type | string | 固定值:chat |
| start_time | int | 开始时间(Unix时间戳) |
| end_time | int | 结束时间(Unix时间戳) |
| page_size | int | 分页大小,最大100 |
| sort_type | string | 排序方式:ByCreateTimeAsc/ByCreateTimeDesc |
{
"code": 0,
"msg": "success",
"data": {
"has_more": false,
"page_token": "",
"items": [{
"message_id": "om_dc13264520392913993dd051dba21dcf",
"create_time": "1615380573411",
"chat_id": "oc_5e6688c96088d5d605f3658d2ccc88fc",
"sender": {
"id": "ou_155184d1e73cbfb8973e5a9e698e74f2",
"sender_type": "user"
},
"body": {
"content": "{\"text\":\"这是一条测试消息\"}"
},
"msg_type": "text",
"deleted": false
}]
}
}
A: Token有效期为2小时,需要定期刷新。建议在Token过期前30分钟重新获取。
A: 请检查:
A: 请检查:
A: 撤回限制: