最近在用 nodejs 写,之前的 nodejs 爬虫代码用 js 写的,感觉可维护性太差,也没有智能提示,于是把js改用ts(typescript)重写一下,提升代码质量。
爬虫启动之后不定期会出现验证码反爬虫,需要输入验证码才能继续,于是想在需要输入验证码时推送一个消息给用户,让用户输入验证码以继续爬虫的整个流程。我们平时用钉钉办公,钉钉群有个机器人,很方便于是就实现了一个通过钉钉的群机器人实现消息推送。
代码是 ts 实现的,用了 request 发起http请求,具体参数参考钉钉官方文档,只实现了文本消息的推送,其它消息类似,再进行一层封装,实现代码如下:
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);
随着 Web 的发展,用户对于 Web 的实时推送要求也越来越高 ,比如,工业运行监控、Web 在线通讯、即时报价系统、在线游戏等,都需要将后台发生的变化主动地、实时地传送到浏览器端,而不需要用户手动地刷新页面。本文对过去和现在流行的 Web 实时推送技术进行了比较与总结。
度推出熊掌号,为了让hexo支持熊掌号推送,我在hexo-baidu-url-submit这个插件的基础上加上了熊掌号推送功能.当天推送的24小时内会直接收录. 用这个,百度的收录速度是非常快的.以后再也不用等百度收录等到死也不会理你了
随着HTML、浏览器等各项技术、标准的发展,依次生成了不同的手段与方法能够实现服务端主动推送消息,它们分别是:AJAX,Comet,ServerSent以及WebSocket。
内容以共享、参考、研究为目的,不存在任何商业目的。其版权属原作者所有,如有侵权或违规,请与小编联系!情况属实本人将予以删除!