WishMeLz

生活其实很有趣

Server酱

NodeJs 封装一个简单的请求.

key:server酱SCKEY
text:标题
desp:内容(支持MarkDown)

const https = require('https');
var iconv = require("iconv-lite");
function serverBot(key = '', text = '', desp = '') {
    return new Promise((resolve, reject) => {
        let url = `https://sc.ftqq.com/${key}.send?text=${text}&desp=${desp}`
        https.get(url, function (res) {
            var datas = [];
            var size = 0;
            res.on('data', function (data) {
                datas.push(data);
                size += data.length;
            });
            res.on("end", function () {
                var buff = Buffer.concat(datas, size);
                var result = iconv.decode(buff, "gbk");
                let resData = JSON.parse(result);
                if (resData.errmsg == 'success') {
                    resolve(resData)
                } else {
                    reject(resData)
                }
            });
        })
    })
}

serverBot("SCKEY", '标题', "内容")
    .then(res => {
        console.log('发送成功');
    }).catch(err => {
        console.log(err);
    })