LoginSignup
15
15

More than 5 years have passed since last update.

slack + hubot + xmppでcron使って発言させる

Last updated at Posted at 2014-11-21

hubotの用意

npmのインストール
$ brew install npm (またはyum install nodejs npmとか)
npmでhubotのインストール
$ npm install -g hubot coffee-script yo generator-hubot 
hubotクローンの作成
$ yo hubot
                     _____________________________  
                    /                             \ 
   //\              |      Extracting input for    |
  ////\    _____    |   self-replication process   |
 //////\  /_____\   \                             / 
 ======= |[^_/\_]|   /----------------------------  
  |   | _|___@@__|__                                
  +===+/  ///     \_\                               
   | |_\ /// HUBOT/\\                             
   |___/\//      /  \\                            
         \      /   +---+                            
          \____/    |   |                            
           | //|    +===+                            
            \//      |xx|
? Owner: Your Name <your@mail.address>
? Bot name: hubot
? Description: A simple helpful robot for you
? Bot adapter: slack

  create ...(省略)
                    _____________________________  
 _____              /                             \ 
 \    \             |   Self-replication process   |
 |    |    _____    |          complete...         |
 |__\\|   /_____\   \     Good luck with that.    / 
   |//+  |[^_/\_]|   /----------------------------  
  |   | _|___@@__|__                                
  +===+/  ///     \_\                               
   | |_\ /// HUBOT/\\                             
   |___/\//      /  \\                            
         \      /   +---+                            
          \____/    |   |                            
           | //|    +===+                            
            \//      |xx|     

  hubot-...(省略)

動作確認してみる

$ bin/hubot

Hubot> hubot ping
Hubot> PONG

slackの設定

bot用アカウントの作成

botのアカウントを作成し、admin権限を付与する
権限変更はこちらのページから行える => https://<team_name>.slack.com/admin

XMPPの設定

slackのページからgatewayの設定をenableする
https://<team_name>.slack.com/account/gateways

xmppの設定ファイル読み込み設定
bin/hubot.diff
--- a/bin/hubot
+++ b/bin/hubot
@@ -5,4 +5,9 @@ set -e
 npm install
 export PATH="node_modules/.bin:node_modules/hubot/node_modules/.bin:$PATH"

+# slack config
+if [ -f "slack_xmpp.conf" ]; then
+    source "slack_xmpp.conf"
+fi
+
 exec node_modules/.bin/hubot "$@"
xmpp用の設定ファイルの作成
slack_xmpp.conf
# slack xmpp config
export HUBOT_XMPP_HOST=conference.<team_name>.xmpp.slack.com
export HUBOT_XMPP_ROOMS=<channel_name>@$HUBOT_XMPP_HOST
export HUBOT_XMPP_USERNAME=<bot_name>@<team_name>.xmpp.slack.com
export HUBOT_XMPP_PASSWORD=<password>

<password>はgatewayをenableにした後、https://<team_name>.slack.com/account/gatewaysのページで確認できます

cronの作成

cronで発言させるスクリプトの作成
cron.coffee

# Description:
#  cron jobs

cron = require('cron').CronJob

module.exports = (robot) ->
  # target room name
  room_name = "general"
  # every week-day at 10:00:00 AM JST
  new cron '0 00 10 * * 1-5', () =>
    robot.send {room: "#{room_name}, "@channel: 朝会の時間です!"
  , null, true, "Asia/Tokyo"

これだとチャンネルに発言してくれず。。。

cronスクリプトの改修
cron.coffee.diff
-    robot.send {room: "#{room_name}, "@channel: 朝会の時間です!"
+    robot.send {room: "#{room_name}@#{process.env.HUBOT_XMPP_HOST}"}, "@channel: 朝会の時間です!"

上記の用にルーム名に@#{process.env.HUBOT_XMPP_HOST}をつけたらうまくいった

15
15
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
15
15