LoginSignup
14
19

More than 5 years have passed since last update.

Hubot で cron を動かしながら Slack に発言するとき(v3)

Posted at

以下のようなスクリプトを組んだとして

cron_task.coffee
cronJob = require('cron').CronJob

module.export = (robot) ->
  task1 = new cronJob('00 00 00 * * 1-5', () ->
    # 発言したい

robot しか無いので、robot.send で発言させます。
この時

cron_task.coffee
cronJob = require('cron').CronJob

module.export = (robot) ->
  task1 = new cronJob('00 00 00 * * 1-5', () ->
    envelope = room: "#channel"
    robot.send envelope, "ほげら"

とかすると、


[Mon Mar 02 2015 00:00:00 GMT+0000 (UTC)] ERROR TypeError: Cannot call method 'send' of undefined
  at SlackBot.send (/path/to/hubot/node_modules/hubot-slack/src/slack.coffee:181:16, <js>:216:33)
  at Robot.send (/path/to/hubot/node_modules/hubot/src/robot.coffee:395:19, <js>:365:42)
  at Object.cronJob.onTick (/path/to/hubot/scripts/cron_task.coffee:8:7, <js>:15:15)
  at Object.CronJob._callback (/path/to/hubot/node_modules/cron/lib/cron.js:343:26)
  at [object Object].callbackWrapper [as _onTimeout] (/path/to/hubot/node_modules/cron/lib/cron.js:405:14)
  at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

とか言われます。一件 robot が存在しなくて send 呼んじゃったっぽく見えますが、hubot-slack v3 からチャンネルの指定の仕方が変更され、以下のようにしないと動きません。

cron_task.coffee

cronJob = require('cron').CronJob

module.export = (robot) ->
  task1 = new cronJob('00 00 00 * * 1-5', () ->
    envelope = room: "channel"
    robot.send envelope, "ほげら"

チャンネル名のシャープは取りましょう。

14
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
19