I'm trying to make a function for hubot send a message every 5 minutes to a room without any command, just by himself.
module.exports = (robot, scripts) ->
setTimeout () ->
setInterval () ->
msg.send "foo"
, 5 * 60 * 1000
, 5 * 60 * 1000
What do I need to change?
Use node-cron.
$ npm install --save cron time
And your script should look like this:
# 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'
More details: https://leanpub.com/automation-and-monitoring-with-hubot/read#leanpub-auto-periodic-task-execution
Actually the hubot documentation shows the usage of setInterval()
and setTimeout()
without the use of extra modules.
You could even do it inline by binding onto the msg object. For example this:
module.exports = (robot) ->
updateId = null
robot.respond /start/i, (msg) ->
msg.update = () -> # this is your update
console.log "updating" # function
if not this.updateId # and here it
this.updateId = setInterval () -> # gets
msg.update() # periodically
, 60*1000 # triggered minutely
msg.send("starting")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With