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")
The real issue is that you are trying to use msg.send
without a message object. You should instead replace that line with a robot.messageRoom
command (or some equivalent command if you want to send a private message).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.