最近在用 nodejs 写爬虫,之前的 nodejs 爬虫代码用 js 写的,感觉可维护性太差,也没有智能提示,于是把js改用ts(typescript)重写一下,提升代码质量。
爬虫启动之后不
nodejs 通过钉钉群机器人推送消息
import * as request from "request";
import * as log4js from "log4js";
const logger = log4js.getLogger("DingdingBot");
const ApplicationTypeHeader:string = "application/json;charset=utf-8";
// DingdingBot
// https://open-doc.dingtalk.com/microapp/serverapi2/qf2nxq
export class DingdingBot{
private readonly _webhookUrl:string;
constructor(webhookUrl:string){
this._webhookUrl = webhookUrl;
}
public pushMsg (msg: string,atMobiles?: Array<string>): boolean{
try {
let options: request.CoreOptions = {
headers: {
"Content-Type": ApplicationTypeHeader
},json: {
"msgtype": "text","text": {
"content": msg
},"at": {
"atMobiles": atMobiles == null ? [] : atMobiles,"isAtAll": false
}
}
};
request.post(this._webhookUrl,options,function(error,response,body){
logger.debug(`push msg ${msg},response: ${JSON.stringify(body)}`);
});
}
catch(err) {
console.error(err);
return false;
}
}
}
// botWebhookUrl 为对应钉钉机器人的 webhook 地址
let bot = new DingdingBot(botWebhookUrl);;
// 直接推送消息
bot.pushMsg("测试消息");
// 推送消息并 @ 某些人
var mobiles = new Array<string>();
mobiles.push("13255573334");
bot.pushMsg("测试消息并@",mobiles);