簡體   English   中英

Hubot如何在沒有任何命令的情況下定期運行功能?

[英]How can Hubot run function regularly, without any command?

我正在嘗試為hubot制作一個函數,每隔5分鍾向一個房間發送一條消息而沒有任何命令,只需要他自己。

module.exports = (robot, scripts) ->
  setTimeout () ->
    setInterval () ->
      msg.send "foo"
    , 5 * 60 * 1000
  , 5 * 60 * 1000

我需要改變什么?

使用node-cron。

$ npm install --save cron time

你的腳本應該是這樣的:

# 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'

更多細節: https//leanpub.com/automation-and-monitoring-with-hubot/read#leanpub-auto-periodic-task-execution

實際上, hubot文檔顯示了setInterval()setTimeout()的用法,而沒有使用額外的模塊。

您甚至可以通過綁定到msg對象來內聯。 例如:

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")

真正的問題是你試圖在沒有消息對象的情況下使用msg.send 您應該使用robot.messageRoom命令替換該行(或者如果要發送私人消息,則替換一些等效命令)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM