为了不错过每天晚上的晚餐,同时也在变胖的路上越走越远,研究了钉钉的各种功能后,决定用自定义提醒机器人撸一个定时提醒订饭的机器人。
环境准备
安装node、npm、钉钉
开始
- 钉钉创建自定义提醒机器人
选择一个群->群设置->智能群助手->添加机器人->选择自定义->选择一种加密方式(具体可见钉钉官网选择合适的加密方式 https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq)->w完成后生成一个url - 撸代码
const schedule = require('node-schedule');
const request = require('request');
//自己测试的url
const url = 'https://oapi.dingtalk.com/robot/send?access_token=6d317ac7d337dbc563901832ba5cd390d1c14f1c7476e836ade221d6ea3b3e76';
//网关
const url1 = 'https://oapi.dingtalk.com/robot/send?access_token=03cfd2b6a7b80ab507aca0331231913bf04e0b1729ce8b2c368f866c01a5e6e0';
const data = {
"msgtype": "text",
"text": {"content": "今天别忘了订饭哦"},
"at": {"isAtAll": false}
};
const sendtext = ()=>{
request.post(url1,{
json: data,
encoding: 'utf-8',
headers:{"content-type": "application/json"}
},function(error, response, body) {
if (!error && response.statusCode == 200) {
console.log("res",body)
}
})
};
const scheduleCronstyle = ()=>{
//每天下午4:30
schedule.scheduleJob('0 30 16 * * *',()=>{
sendtext();
console.log('scheduleCronstyle:' + new Date());
});
};
const isWorkDay = ()=>{
request.get(`https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=${(new Date()).getFullYear()}%E5%B9%B41%E6%9C%88&co=&resource_id=6018&t=1578357406127&ie=utf8&oe=gbk&format=json&tn=baidu&_=1578357382916`,
function (err, response, body) {
if (!err && response.statusCode == 200) {
const holidays = JSON.parse(body).data[0].holiday;
const days = {};
for(let holiday of holidays){
for(let x of holiday.list){
days[x.date] = x.status
}
}
const nowDate = `${(new Date()).getFullYear()}-${(new Date()).getMonth()+1}-${(new Date()).getDate()}`;
if(days[nowDate] && days[nowDate] !== 1){
//非节假日
return scheduleCronstyle();
}
if([1,2,3,4,5].includes((new Date()).getDay())){
//工作日周1-5
return scheduleCronstyle();
}
}
});
}
isWorkDay();
- 运行代码
成功,撒花🎉