Hubot + CoffeeScript ではじめるやわらかプログラミング入門
- パッケージ
- インターナル
- 組み込み
- ゲーム
- 使い捨て
$ curl -L git.io/nodebrew | perl - setup
$ echo 'export PATH=$HOME/.nodebrew/current/bin:$PATH' >> ~/.bash_profile
$ source ~/.bash_profile
$ nodebrew install-binary v0.10.28
$ nodebrew use v0.10.28
$ npm install -g coffee-script hubot
$ hubot -c yawaraka-hubot
$ cd yawaraka-hubot
$ npm install
$ echo "[]" > hubot-scripts.json
(ファイルの中身 ["redis-brain.coffee", "shipit.coffee"] -> [] )
$ ./bin/hubot
Hubot>
Hubot> hubot, ping
Hubot> PONG
$ npm install --save hubot-irc
bin/hubot-irc
というファイルを新規作成して実行権限を付ける#!/bin/sh
export PATH="node_modules/.bin:node_modules/hubot/node_modules/.bin:$PATH"
npm install
export HUBOT_IRC_SERVER="irc.example.jp" # サーバ名
export HUBOT_IRC_PORT=6668 # ポート
export HUBOT_IRC_ROOMS="#bot_test" # channel
export HUBOT_IRC_NICK="hubot-udzura" # ニックネーム
export HUBOT_IRC_PASSWORD="XXXXXX" # パスワード
export HUBOT_IRC_USESSL="true" # SSLを使う
export HUBOT_IRC_SERVER_FAKE_SSL="true" # SSLが社内独自なので
export HUBOT_IRC_SEND_NOTICE_MODE="true" # 全発言をNotice
export HUBOT_IRC_DEBUG="true" # デバッグ
exec node_modules/.bin/hubot "$@"
@echo off
SET HUBOT_IRC_SERVER=irc.example.jp
SET HUBOT_IRC_PORT=6668
SET HUBOT_IRC_ROOMS=#bot_test
SET HUBOT_IRC_NICK=hubot-udzura
SET HUBOT_IRC_PASSWORD=XXXXXX
SET HUBOT_IRC_USESSL=true
SET HUBOT_IRC_SERVER_FAKE_SSL=true
SET HUBOT_IRC_SEND_NOTICE_MODE=true
SET HUBOT_IRC_DEBUG=true
npm install && node_modules\.bin\hubot.cmd %*
./bin/hubot-irc
$ coffee
coffee> 1 + 2
coffee> 3 * 9
coffee> Math.power 3, 2
coffee> process.exit()
./sample.coffee
というファイルに以下を書く(ここからのサンプルも同様)
power2 = (num) ->
num * num
console.log power(1234)
$ coffee sample.coffee
すると?
is_odd_even = (num) ->
if num % 2 == 0
'even'
else
'odd'
console.log '4 is', is_odd_even(4)
console.log '185 is', is_odd_even(185)
for i in [1 .. 10]
console.log "Hello! #{i}"
1, 2, 3 #=> 整数
1.2, 3.45 #=> 小数
"Coffee" #=> 文字列
(val) -> console.log(val)
#=> 関数、手続き
fib = [1, 1, 2, 3, 5, 8]
branches = ["Tokyo", "Fukuoka", "Kyoto"]
# 繰り返しに便利
for city in branches
console.log "Welcome to #{city}!!"
whereis = {
"lolipop": "Fukuoka",
"minne": ["Fukuoka", "Tokyo", "Kyoto"],
"suzuri": "Tokyo"
}
console.log 'lolipop is located in', whereis["lolipop"]
$ open scripts/yawaraka.coffee
# Description:
# 説明を書く
#
# Commands:
# コマンドの説明を書く
# hubot hello, I am <名前> - <名前> に挨拶をする
#
module.exports = (robot) ->
robot.respond /hello, I am (.*)/i, (msg) ->
# この中で処理を書く
name = msg.match[1] # () の中が取れる
msg.send "Test" # 発言をする
msg.send "ちょりーっす、 #{name}"
robot.respond /goodbye (.*)/i, (msg) ->
# いくつでもコマンドを追加できる
$ ./bin/hubot
Hubot> hubot, hello ...
# ローカルで試してからIRCでも試す!
module.exports = (robot) ->
robot.respond /hello, I am (.*)/i, (msg) ->
name = msg.match[1]
if name == "horaotoko"
msg.send "べ、別にあんたに挨拶なんかしたくないんだからねっ!"
else
msg.send "ちょりーっす、 #{name}"
module.exports = (robot) ->
robot.respond /hello, I am (.*)/i, (msg) ->
name = msg.match[1]
if name == "kyokutyo"
for i in [1 .. 3]
msg.send "ちょりーっす、 #{name}"
else
msg.send "ちょりーっす、 #{name}"
random = (n) -> Math.floor(Math.random() * n)
module.exports = (robot) ->
robot.respond /今日の運勢/i, (msg) ->
fortunes = [
'大吉',
'末吉',
'大凶'
]
result = fortunes[random(3)]
msg.send "今日の運勢: #{result}"
module.exports = (robot) ->
robot.respond /about (.*)/i, (msg) ->
name = msg.match[1]
profiles = {
'udzura' : '基盤ティーム',
'horaotoko' : '3.5期生',
'kyokutyo' : '隠れ変態'
}
result = profiles[name]
if result
msg.send "#{name}さんのプロフィール: #{result}"
else
msg.send "#{name}さんのことなんて知りません"
now = new Date
now.toString()
now.getFullYear(), now.getFullYear(), now.getDay() #...
$ npm install --save request
module.exports = (robot) ->
robot.respond /tiqav (.*)/i, (msg) ->
request = require('request');
request.get("http://api.tiqav.com/search.json?q=#{msg.match[1]}", (error, response, body) ->
if error or response.statusCode != 200
return msg.send('画像検索に失敗しました...')
data = JSON.parse(body)[0]
# robot.logger.info data
msg.send "画像の様子です: http://img.tiqav.com/#{data.id}.#{data.ext}" )
dokku001.tokyo.pb
$ chmod 600 ~/.ssh/id_dsa.pb
$ ssh -i ~/.ssh/id_dsa.pb dokku@dokku001.tokyo.lan help
.ssh/config
を編集Host dokku001.tokyo.lan
HostName dokku001.tokyo.lan
User dokku
IdentityFile ~/.ssh/id_dsa.pb
Procfile
を編集web: bin/hubot-irc -a irc
$ git init .
$ git add .
$ git commit
$ git remote add dokku dokku@dokku001.tokyo.lan:udzura-hubot
$ git push dokku master