Skip to content

Commit 7aa82fe

Browse files
feat: Integrate Feishu for notification (#482)
Co-authored-by: Albert Yang <albert.yang@porsche.digital>
1 parent 08a6359 commit 7aa82fe

3 files changed

Lines changed: 83 additions & 2 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ buvid3亦可不填 使用随机生成值
218218
| `TG_PROXY_PORT` | Telegram 代理的端口 | 例子:http代理 <http://127.0.0.1:1080> 则填写 1080 |
219219
| `DD_BOT_TOKEN` | 钉钉推送 | 钉钉推送(`DD_BOT_TOKEN``DD_BOT_SECRET`两者必需)[官方文档](https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq) ,只需`https://oapi.dingtalk.com/robot/send?access_token=XXX` 等于`=`符号后面的XXX即可 |
220220
| `DD_BOT_SECRET` | 钉钉推送 | (`DD_BOT_TOKEN``DD_BOT_SECRET`两者必需) ,密钥,机器人安全设置页面,加签一栏下面显示的SEC开头的`SECXXXXXXXXXX`等字符 , 注:钉钉机器人安全设置只需勾选`加签`即可,其他选项不要勾选,再不懂看 [这个图](doc/pic/DD_bot.png) |
221+
| `FS_BOT_WEBHOOK` | 飞书机器人 | 飞书机器人 webhook,创建自定义机器人后复制 webhook 地址,[官方文档](https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot) |
222+
| `FS_BOT_SECRET` | 飞书机器人 | 飞书机器人安全设置中的签名密钥(若开启“签名校验”则必填),[官方文档](https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot) |
221223
| `IGOT_PUSH_KEY` | iGot推送 | iGot聚合推送,支持多方式推送,确保消息可达。 [参考文档](https://wahao.github.io/Bark-MP-helper ) |
222224
| `QQ_SKEY` | 酷推(Cool Push)推送 | 推送所需的Skey,登录后获取Skey [参考文档](https://cp.xuthus.cc/) |
223225
| `QQ_MODE` | 酷推(Cool Push)推送 | 推送方式(send或group或者wx,默认send) [参考文档](https://cp.xuthus.cc/) |

env.example.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ module.exports = Object.freeze({
109109
SMTP_PASS: '',
110110
SMTP_TO_USER: '',
111111
GOTIFY_URL: '',
112-
GOTIFY_APPKEY: ''
112+
GOTIFY_APPKEY: '',
113+
FS_BOT_WEBHOOK: '',
114+
FS_BOT_SECRET: ''
113115
},
114116

115117
/**

lib/helper/notify.js

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ let GOTIFY_URL = '';
8888
// 此处填你想推送的Application的Token(不包含推送时额外添加的前缀 Bearer )
8989
let GOTIFY_APPKEY = '';
9090

91+
// =======================================飞书机器人通知设置区域===========================================
92+
// 此处填你飞书机器人的 webhook(详见文档 https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot)
93+
// 注:此处设置github action用户填写到Settings-Secrets里面(Name输入FS_BOT_WEBHOOK)
94+
let FS_BOT_WEBHOOK = '';
95+
// 签名密钥(如果在飞书机器人安全设置里开启了“签名校验”)
96+
// 注:此处设置github action用户填写到Settings-Secrets里面(Name输入FS_BOT_SECRET)
97+
let FS_BOT_SECRET = '';
98+
9199
//==========================云端环境变量的判断与接收=========================
92100
if (process.env.SCKEY) {
93101
SCKEY = process.env.SCKEY;
@@ -211,6 +219,14 @@ if (process.env.GOTIFY_URL) {
211219
}
212220
}
213221

222+
if (process.env.FS_BOT_WEBHOOK) {
223+
FS_BOT_WEBHOOK = process.env.FS_BOT_WEBHOOK;
224+
}
225+
226+
if (process.env.FS_BOT_SECRET) {
227+
FS_BOT_SECRET = process.env.FS_BOT_SECRET;
228+
}
229+
214230
//==========================云端环境变量的判断与接收=========================
215231

216232
async function sendNotify(text, desp, params = {}) {
@@ -246,7 +262,9 @@ async function sendNotify(text, desp, params = {}) {
246262
//电子邮件
247263
email(text, desp),
248264
// Gotify
249-
gotifyNotify(text, desp)
265+
gotifyNotify(text, desp),
266+
// 飞书机器人
267+
feishuNotify(text, desp)
250268
]);
251269
}
252270

@@ -885,6 +903,65 @@ function gotifyNotify(text, desp) {
885903
});
886904
}
887905

906+
function feishuNotify(text, desp) {
907+
return new Promise(resolve => {
908+
if (FS_BOT_WEBHOOK) {
909+
const payload = {
910+
msg_type: 'text',
911+
content: {
912+
text: `${text}\n\n${desp}`
913+
}
914+
};
915+
916+
if (FS_BOT_SECRET) {
917+
const crypto = require('crypto');
918+
const timestamp = Math.floor(Date.now() / 1000);
919+
const signStr = `${timestamp}\n${FS_BOT_SECRET}`;
920+
const sign = crypto
921+
.createHmac('sha256', FS_BOT_SECRET)
922+
.update(signStr)
923+
.digest('base64');
924+
payload.timestamp = `${timestamp}`;
925+
payload.sign = sign;
926+
}
927+
928+
send({
929+
method: 'POST',
930+
url: FS_BOT_WEBHOOK,
931+
contents: payload,
932+
config: {
933+
retry: false
934+
},
935+
headers: {
936+
accept: 'application/json, text/plain, */*',
937+
'content-type': 'application/json',
938+
},
939+
success: res => {
940+
try {
941+
const data = JSON.parse(res.body);
942+
if (data.code === 0) {
943+
log.info('发送通知', '飞书机器人发送通知消息完成。');
944+
} else {
945+
log.error('发送通知', `${data.msg || '飞书机器人发送通知异常'}`);
946+
}
947+
} catch (e) {
948+
log.error('发送通知', e);
949+
} finally {
950+
resolve();
951+
}
952+
},
953+
failure: err => {
954+
log.error('发送通知', '飞书机器人发送通知消息失败!!' + err);
955+
resolve();
956+
}
957+
});
958+
} else {
959+
log.debug('发送通知', '您未提供飞书机器人推送所需的FS_BOT_WEBHOOK,取消飞书机器人推送消息通知');
960+
resolve();
961+
}
962+
});
963+
}
964+
888965
async function qmsg(text, desp) {
889966
return new Promise(resolve => {
890967
if (QMSG_KEY) {

0 commit comments

Comments
 (0)