|
|
1 روز پیش | |
|---|---|---|
| .. | ||
| ACCESS_TOKEN.md | 1 روز پیش | |
| API_REFERENCE.md | 1 روز پیش | |
| README.md | 1 روز پیش | |
本模块提供了飞书机器人消息推送功能,支持向飞书群组或个人发送消息。通过简单的 HTTP 接口调用,前端可以轻松实现飞书消息推送。
在使用接口之前,需要先在飞书群组中创建自定义机器人:
https://open.feishu.cn/open-apis/bot/v2/hook/xxx)重要提示:
飞书机器人支持以下安全设置:
本接口暂不支持签名验证,如需使用请在飞书后台配置 IP 白名单或关键词验证。
http://your-server:3000/api/feishu
接口地址: POST /api/feishu/send
功能说明: 支持发送所有类型的飞书消息,灵活性最高。
请求参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| webhook_url | string | 是 | 飞书机器人的 Webhook URL |
| msg_type | string | 是 | 消息类型:text(文本)、post(富文本)、interactive(卡片) |
| content | object | 是 | 消息内容,根据 msg_type 不同而不同 |
请求示例:
// 发送文本消息
fetch('http://your-server:3000/api/feishu/send', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
webhook_url: 'https://open.feishu.cn/open-apis/bot/v2/hook/xxx',
msg_type: 'text',
content: {
text: '这是一条测试消息'
}
})
})
.then(res => res.json())
.then(data => console.log(data));
响应示例:
{
"success": true,
"data": {
"message": "消息发送成功",
"feishu_response": {
"code": 0,
"msg": "success"
}
},
"timestamp": "2026-03-09T11:45:00.000Z"
}
接口地址: POST /api/feishu/send/text
功能说明: 快速发送纯文本消息,接口更简洁。
请求参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| webhook_url | string | 是 | 飞书机器人的 Webhook URL |
| text | string | 是 | 消息文本内容 |
请求示例:
fetch('http://your-server:3000/api/feishu/send/text', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
webhook_url: 'https://open.feishu.cn/open-apis/bot/v2/hook/xxx',
text: '这是一条简单的文本消息'
})
})
.then(res => res.json())
.then(data => console.log(data));
响应示例:
{
"success": true,
"data": {
"message": "文本消息发送成功",
"feishu_response": {
"code": 0,
"msg": "success"
}
},
"timestamp": "2026-03-09T11:45:00.000Z"
}
接口地址: POST /api/feishu/send/post
功能说明: 发送富文本消息,支持标题、多段落、@用户、链接等丰富格式。
请求参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| webhook_url | string | 是 | 飞书机器人的 Webhook URL |
| title | string | 否 | 消息标题 |
| content | array | 是 | 消息内容数组,详见下方说明 |
content 格式说明:
content 是一个二维数组,第一层代表段落,第二层代表段落内的元素。
支持的元素类型:
text:普通文本a:链接at:@用户请求示例 1:发送带标题的富文本消息
fetch('http://your-server:3000/api/feishu/send/post', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
webhook_url: 'https://open.feishu.cn/open-apis/bot/v2/hook/xxx',
title: '系统通知',
content: [
[
{ tag: 'text', text: '您有一条新的任务待处理' }
],
[
{ tag: 'text', text: '任务详情:' },
{ tag: 'a', text: '点击查看', href: 'https://example.com/task/123' }
]
]
})
})
.then(res => res.json())
.then(data => console.log(data));
请求示例 2:@单个用户
fetch('http://your-server:3000/api/feishu/send/post', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
webhook_url: 'https://open.feishu.cn/open-apis/bot/v2/hook/xxx',
title: '任务提醒',
content: [
[
{ tag: 'at', user_id: 'ou_xxxxxx' },
{ tag: 'text', text: ' 您有一个新任务需要处理' }
]
]
})
})
.then(res => res.json())
.then(data => console.log(data));
请求示例 3:@所有人
fetch('http://your-server:3000/api/feishu/send/post', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
webhook_url: 'https://open.feishu.cn/open-apis/bot/v2/hook/xxx',
title: '重要通知',
content: [
[
{ tag: 'at', user_id: 'all' },
{ tag: 'text', text: ' 系统将于今晚 22:00 进行维护,请提前保存工作' }
]
]
})
})
.then(res => res.json())
.then(data => console.log(data));
请求示例 4:@多个用户
fetch('http://your-server:3000/api/feishu/send/post', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
webhook_url: 'https://open.feishu.cn/open-apis/bot/v2/hook/xxx',
title: '项目进度更新',
content: [
[
{ tag: 'at', user_id: 'ou_user1' },
{ tag: 'text', text: ' ' },
{ tag: 'at', user_id: 'ou_user2' },
{ tag: 'text', text: ' 请查看最新的项目进度' }
],
[
{ tag: 'text', text: '详情:' },
{ tag: 'a', text: '项目看板', href: 'https://example.com/project' }
]
]
})
})
.then(res => res.json())
.then(data => console.log(data));
响应示例:
{
"success": true,
"data": {
"message": "富文本消息发送成功",
"feishu_response": {
"code": 0,
"msg": "success"
}
},
"timestamp": "2026-03-09T11:45:00.000Z"
}
{
"success": false,
"error": "错误信息",
"timestamp": "2026-03-09T11:45:00.000Z"
}
| HTTP 状态码 | 错误信息 | 说明 |
|---|---|---|
| 400 | webhook_url 是必填项 | 未提供 webhook_url |
| 400 | msg_type 是必填项 | 未提供 msg_type |
| 400 | content 是必填项 | 未提供 content |
| 400 | webhook_url 格式不正确 | webhook_url 不是有效的飞书 Webhook URL |
| 500 | 飞书消息发送失败 | 飞书 API 返回错误 |
| 500 | 消息发送失败 | 网络错误或其他异常 |
// 当系统检测到异常时,发送告警消息
async function sendAlert(errorMessage) {
const response = await fetch('http://your-server:3000/api/feishu/send/post', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
webhook_url: 'https://open.feishu.cn/open-apis/bot/v2/hook/xxx',
title: '⚠️ 系统告警',
content: [
[
{ tag: 'at', user_id: 'all' },
{ tag: 'text', text: ' 系统检测到异常' }
],
[
{ tag: 'text', text: `错误信息:${errorMessage}` }
],
[
{ tag: 'text', text: `时间:${new Date().toLocaleString()}` }
]
]
})
});
return response.json();
}
// 当任务分配给用户时,发送通知
async function notifyTaskAssignment(userId, taskName, taskUrl) {
const response = await fetch('http://your-server:3000/api/feishu/send/post', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
webhook_url: 'https://open.feishu.cn/open-apis/bot/v2/hook/xxx',
title: '📋 新任务分配',
content: [
[
{ tag: 'at', user_id: userId },
{ tag: 'text', text: ` 您有一个新任务:${taskName}` }
],
[
{ tag: 'text', text: '查看详情:' },
{ tag: 'a', text: '点击这里', href: taskUrl }
]
]
})
});
return response.json();
}
// 向多个用户发送通知
async function notifyMultipleUsers(userIds, message) {
// 构建 @用户 的内容
const atUsers = userIds.flatMap(userId => [
{ tag: 'at', user_id: userId },
{ tag: 'text', text: ' ' }
]);
const response = await fetch('http://your-server:3000/api/feishu/send/post', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
webhook_url: 'https://open.feishu.cn/open-apis/bot/v2/hook/xxx',
title: '通知',
content: [
atUsers,
[
{ tag: 'text', text: message }
]
]
})
});
return response.json();
}
// 使用示例
notifyMultipleUsers(
['ou_user1', 'ou_user2', 'ou_user3'],
'会议将于 10 分钟后开始,请准时参加'
);
Webhook URL 安全
消息频率限制
用户 ID 获取
ou_xxxxxx)user_id: 'all'消息格式
tag 字段,根据不同 tag 提供对应的字段错误处理
// feishu-bot.js
class FeishuBot {
constructor(baseUrl = 'http://your-server:3000/api/feishu') {
this.baseUrl = baseUrl;
}
/**
* 发送文本消息
*/
async sendText(webhookUrl, text) {
return this._request('/send/text', {
webhook_url: webhookUrl,
text
});
}
/**
* 发送富文本消息
*/
async sendPost(webhookUrl, title, content) {
return this._request('/send/post', {
webhook_url: webhookUrl,
title,
content
});
}
/**
* @单个用户
*/
async mentionUser(webhookUrl, userId, message, title = '通知') {
return this.sendPost(webhookUrl, title, [
[
{ tag: 'at', user_id: userId },
{ tag: 'text', text: ` ${message}` }
]
]);
}
/**
* @所有人
*/
async mentionAll(webhookUrl, message, title = '重要通知') {
return this.sendPost(webhookUrl, title, [
[
{ tag: 'at', user_id: 'all' },
{ tag: 'text', text: ` ${message}` }
]
]);
}
/**
* @多个用户
*/
async mentionMultiple(webhookUrl, userIds, message, title = '通知') {
const atUsers = userIds.flatMap(userId => [
{ tag: 'at', user_id: userId },
{ tag: 'text', text: ' ' }
]);
return this.sendPost(webhookUrl, title, [
atUsers,
[{ tag: 'text', text: message }]
]);
}
/**
* 内部请求方法
*/
async _request(endpoint, data) {
try {
const response = await fetch(`${this.baseUrl}${endpoint}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
});
const result = await response.json();
if (!result.success) {
throw new Error(result.error || '发送失败');
}
return result;
} catch (error) {
console.error('飞书消息发送失败:', error);
throw error;
}
}
}
// 使用示例
const bot = new FeishuBot();
// 发送文本消息
bot.sendText(
'https://open.feishu.cn/open-apis/bot/v2/hook/xxx',
'这是一条测试消息'
);
// @单个用户
bot.mentionUser(
'https://open.feishu.cn/open-apis/bot/v2/hook/xxx',
'ou_xxxxxx',
'您有新的任务'
);
// @所有人
bot.mentionAll(
'https://open.feishu.cn/open-apis/bot/v2/hook/xxx',
'系统维护通知'
);
// @多个用户
bot.mentionMultiple(
'https://open.feishu.cn/open-apis/bot/v2/hook/xxx',
['ou_user1', 'ou_user2'],
'会议即将开始'
);