我正在尝试从 CronJob 中调用 slackbot API,我希望在一天中的某个时间向 channel 发送一条消息,但从 CronJob 中调用时,相关方法不起作用。相关代码如下:
Shiftbot.prototype.run = function () {
Shiftbot.super_.call(this, this.settings);
this.on('start', this._onStart);
this.on('message', this._onMessage);
};
Shiftbot.prototype._onStart = function() {
this._loadBotUser();
new CronJob('* * 16 * * *', function(){
console.log('You will see this message every second ');
Shiftbot.prototype._sendReminder();
}, null, true, "America/New_York");
}
Shiftbot.prototype._sendReminder = function() {
console.log('test');
this.postMessageToChannel("shiftbot_beta", "Cron test", {as_user: true});
}
上面的 _sendReminder
方法在从另一个 Shiftbot.prototype.*
方法调用时工作正常,但不能从 cronjob 内部调用。任何帮助将不胜感激!
最佳答案
在 Shiftbot.prototype 范围内创建变量 self
并在 CronJob
范围内使用它,如下所示:
Shiftbot.prototype._onStart = function() {
var self = this;
this._loadBotUser();
new CronJob('* * 16 * * *', function(){
console.log('You will see this message every second ');
self._sendReminder();
}, null, true, "America/New_York");
}
关于javascript - 从 Node 中的 CronJob 调用 API 没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47541552/