【botkit/slack】匿名でslackに投稿するbotを作って、herokuで動かす
会社でslackを使っていて、匿名の投稿チャンネルを作ろうとなった。 その時のメモ
ここを参考にしている
流れ
Appのannonymusに何か書くと、指定のチャンネルに匿名で投稿できる
botkitの準備
基本的に、githubのドキュメントを参考に進める
# botki install npm install -g botkit # create new bot いろいろ入力させられる botkit new --platform slack # いろいろ入力させられたときに入力したボット名 cd ボット名
bot.jsを下記で全て上書きする channel_idだけ変更する
var env = require('node-env-file'); env(__dirname + '/.env'); var Botkit = require('botkit'); var debug = require('debug')('botkit:main'); var controller = Botkit.slackbot({ debug: true, }); var bot = controller.spawn({ token: process.env.token }).startRTM(); controller.on('direct_message',function(bot,message) { console.log("hhoge") bot.reply(message,"匿名で投稿しました:mailbox_with_mail:"); // 投稿先がチャンネルの場合 bot.startConversation({channel: "channel_id"}, function(response, convo){ convo.say(message); }) });
これで、slack bot の tokenをつかって実際にテストできる
token=token node bot.js
herokuに登録する
Procfileだけ新しく作成
worker: node bot.js
あとは
heroku login heroku create git push heroku master heroku config:add token=slackのbotのtoken
してherokuの設定画面で、
webをoffにして、workerをONにする