Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can Hubot run function regularly, without any command?

Tags:

hubot

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?

like image 993
user3586235 Avatar asked Feb 13 '23 07:02

user3586235


2 Answers

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

like image 154
Spajus Avatar answered Feb 19 '23 04:02

Spajus


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")
like image 24
coderofsalvation Avatar answered Feb 19 '23 04:02

coderofsalvation