飞书机器人 - 使用Node发送签名消息
2022-08-05 本文已影响0人
清霆
import axios from 'axios';
import crypto from 'crypto';
const secret = 'nIMtb7r3N9pkXyXdtAbx6e';
const webhook = 'https://open.feishu.cn/open-apis/bot/v2/hook/5e3d587e-d78e-4a07-bd07-265f269f86fd';
const encode = (secret) => {
const timestamp = Math.floor(Date.now() / 1000);
const str = Buffer.from(`${timestamp}\n${secret}`, 'utf8');
const sign = crypto.createHmac('SHA256', str);
sign.update(Buffer.alloc(0));
return { timestamp, sign: sign.digest('base64') };
};
const send = async (text) => {
const { timestamp, sign } = encode(secret);
const params = {
timestamp: timestamp.toString(),
sign,
msg_type: 'text',
content: {
text
}
};
const { data: result } = await axios.post(
webhook,
params
);
return console.log(result);
};
send('request with sign');