我正在尝试为hubot创建一个功能,每5分钟向一个房间发送一条消息,而无需任何命令,只需他自己。
module.exports = (robot, scripts) ->
setTimeout () ->
setInterval () ->
msg.send "foo"
, 5 * 60 * 1000
, 5 * 60 * 1000
我需要改变什么?
我正在尝试为hubot创建一个功能,每5分钟向一个房间发送一条消息,而无需任何命令,只需他自己。
module.exports = (robot, scripts) ->
setTimeout () ->
setInterval () ->
msg.send "foo"
, 5 * 60 * 1000
, 5 * 60 * 1000
我需要改变什么?
使用节点-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
命令(或者如果您想发送私人消息,则使用一些等效的命令)。