我正在尝试为hubot制作一个函数,每隔5分钟向一个房间发送一条消息而没有任何命令,只需要他自己.
module.exports = (robot, scripts) ->
setTimeout () ->
setInterval () ->
msg.send "foo"
, 5 * 60 * 1000
, 5 * 60 * 1000
Run Code Online (Sandbox Code Playgroud)
我需要改变什么?
使用node-cron.
$ npm install --save cron time
Run Code Online (Sandbox Code Playgroud)
你的脚本应该是这样的:
# Description:
# Defines periodic executions
module.exports = (robot) ->
cronJob = require('cron').CronJob
tz = 'America/Los_Angeles'
new cronJob('0 0 9 * * 1-5', workdaysNineAm, null, true, tz)
new cronJob('0 */5 * * * *', everyFiveMinutes, null, true, tz)
room = 12345678
workdaysNineAm = ->
robot.emit 'slave:command', 'wake everyone up', room
everyFiveMinutes = ->
robot.messageRoom room, 'I will nag you every 5 minutes'
Run Code Online (Sandbox Code Playgroud)
更多细节:https://leanpub.com/automation-and-monitoring-with-hubot/read#leanpub-auto-periodic-task-execution