はじめに

最近HipChatを使うことが多いので、HipChatと連携したボットを作ってみようと思います
今回はBot用のアカウントが作れなかったので自分のアカウントをボット化してみました

そのため少し、hipchat用のadaptarのコードを修正する必要がありました

前提

CentOS7

インストール

yumによるソフトウェアのインストール
yum -y install epel-release
yum -y install --enablerepo=epel npm redis
yum update openssl
npm によるNode.jsのパッケージインストール
npm update -g npm
npm install -g coffee-script npm install -g yo generator-hubot npm install -g yo@latest
npm install -g forever

起動設定

Redisの起動設定
systemctl enable redis
systemctl start redis
Firewallの停止設定
systemctl stop firewalld
systemctl disable firewal

Hubotの設定

Hubot用アカウントの作成
useradd hubot
passwd *****
su - hubot
Chatbot用のNodejsディレクトリ
mkdir chatbot
cd /home/hubot/chatbot
Hipchat用のNodejsプロジェクトを作成
yo hubot --adapter hipchat

hubot起動スクリプトの編集
vi /home/hubot/catbot/bin/hubot
#!/bin/sh

set -e

npm install
export PATH="node_modules/.bin:node_modules/hubot/node_modules/.bin:$PATH"
export HUBOT_HIPCHAT_JID="******@chat.btf.hipchat.com"
export HUBOT_HIPCHAT_PASSWORD="*********"
export HUBOT_HIPCHAT_ROOMS="*******@conf.btf.hipchat.com"
export HUBOT_LOG_LEVEL="debug"
export HUBOT_HIPCHAT_HOST="xx.xx.xx.xx"
export HUBOT_HIPCHAT_XMPP_DOMAIN="btf.hipchat.com"

exec node_modules/.bin/hubot --name "ChipChat上でのFullName" "$@"


※ HUBOT_HIPCHAT_XMPP_DOMAIN に btf.hipchat.com と書くこと  conf が自動的に付与される仕様のため

HUBOT_HIPCHAT_JID       :ボットがログインするアカウントJabber ID
HUBOT_HIPCHAT_PASSWORD   :ボットがログインするアカウントのパスワード
HUBOT_HIPCHAT_ROOMS     :ボットがログインするルームID
HUBOT_LOG_LEVEL        :標準出力されるログのレベル
HUBOT_HIPCHAT_HOST      :HipChatサーバのIP
HUBOT_HIPCHAT_XMPP_DOMAIN :HipChatサーバのXMPPドメイン名

HubotはXMPPによりHipChatとやり取りします

hubot-hipchatのconnectorコード修正

# vi /home/hubot/chatbot/node_modules/hubot-hipchat/src/connector.coffee
# 429,430行目辺りをコメントアウトする
# Ignore our own messages return if fromNick is @name

# # Ignore our own messages # return if fromNick is @name

上記コードは、自分からのメッセージは無視するというコードなのですが、
今回は自分自身をBot化するので、このコードはコメントアウトさせました


foreverによるHubotの起動スクリプト

run.sh
#!/bin/sh
forever start -c sh bin/hubot -a hipchat

stop.sh
#!/bin/sh
forever stop bin/hubot

起動
# ./run.sh
停止
# ./stop.sh

CoffeeScriptによるBotプログラミング

超簡単なプログラミングですが

module.exports = (robot) ->
	robot.respond /あなたのを教えて/i, (res) ->
		res.send "私の名前はChatBotです"
robot.respond :メンション付きで該当メッセージを受け取った場合
res.send    :返信するメッセージ