hubotインストール、スクリプト作成、外部コマンド実行

RaspberryPi1 B+で動作を確認

## 参考

* [Hubot をインストールし HipChat と連携を行う - Qiita](http://qiita.com/kotarella1110/items/cf4d96cdf3aeead9c83b)

## インストール

```bash:
# sudo apt-get install -y redis-server
sudo npm install -g npm nodejs 
sudo npm install -g coffee-script yo generator-hubot
mkdir k
cd k
yo hubot --defaults
```

```diff:external-scripts.json
 [
 ...
-   "hubot-heroku-keepalive",
-   "hubot-redis-brain",
 ...
 ]
```

```bash:空なので削除
rm hubot-scripts.json
```

## 起動

```bash:起動
bin/hubot
```

```apache:コンソール
k> k ping
k> PONG

k> k help
k> Shell: k adapter - Reply with the adapter
k animate me <query> - The same thing as `image me`, except adds a few parameters to try to return an animated GIF instead.
k echo <text> - Reply back with <text>
k help - Displays all of the help commands that Hubot knows about.
k help <query> - Displays all help commands that match <query>.
k image me <query> - The Original. Queries Google Images for <query> and returns a random top result.
k map me <query> - Returns a map view of the area returned by `query`.
k mustache me <url|query> - Adds a mustache to the specified URL or query result.
k ping - Reply with pong
k pug bomb N - get N pugs
k pug me - Receive a pug
k the rules - Make sure hubot still knows the rules.
k time - Reply with current time
k translate me <phrase> - Searches for a translation for the <phrase> and then prints that bad boy out.
k translate me from <source> into <target> <phrase> - Translates <phrase> from <source> into <target>. Both <source> and <target> are optional
ship it - Display a motivation squirrel
```

## PINGの応答を変化させてみる

> 〜.coffee is using deprecated documentation syntax

* [Hubot起動時に"INFO [scriptのパス] is using depercated documentation syntax"が出力される理由と対応 - Qiita](http://qiita.com/StupidDog/items/5269e21fb9394bfccd64)

Descriptionは必要だそう。

```coffee:scripts/ping.coffee
# Description:
#   Example scripts for you to examine and try out.

module.exports = (robot) ->
  robot.respond /PING$/i, (res) ->
    res.reply "何ですか？"
````

```bash:
bin/hubot

k> k ping
k> Shell: 何ですか？
PONG
```

## 外部コマンドの実行

### ping

* [hubot-scripts/shell-command.coffee at master · sapg/hubot-scripts · GitHub](https://github.com/sapg/hubot-scripts/blob/master/shell-command.coffee)

```coffee:
# Description:
# Commands:
#  hubot ping <hostname>

module.exports = (robot) ->
 robot.respond /ping (.*)$/i, (msg) ->
    hostname = msg.match[1]
    @exec = require('child_process').exec
    command = "ping -c5 #{hostname}"
    msg.send command

    @exec command, (error, stdout, stderr) ->
      #msg.send error
      msg.send stdout
      #msg.send stderr
```

```bash:
k> k ping 8.8.8.8

ping -c5 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=54 time=5.12 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=54 time=4.83 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=54 time=3.07 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=54 time=4.05 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=54 time=5.26 ms

--- 8.8.8.8 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4005ms
rtt min/avg/max/mdev = 3.074/4.472/5.263/0.814 ms
```

### 天気(curl)

* [英和・和英翻訳と今日明日明後日の天気をターミナルから調べる便利コマンドのご紹介 - Qiita](http://qiita.com/b-kaxa/items/0c81102f9e1fc6b78525)

![天気.png](https://qiita-image-store.s3.amazonaws.com/0/25728/a06914de-9928-d4f8-f3bc-cb22a316ba0c.png)

## 英和/和英(trans)

* [Translate Shell](https://www.soimort.org/translate-shell/)

```bash:
sudo apt-get install -y gawk
wget git.io/trans
chmod +x ./trans
sudo mv trans /usr/local/bin/
```

```bash:transコマンドの動作確認
$ trans :ja binary
$ trans :en 変える
```

```coffee:scripts/trans.coffee
# Description:
# Commands:
#  hubot trans :(ja|en) <word>

module.exports = (robot) ->
 robot.respond /trans :(ja|en) (.*)$/i, (msg) ->
    lang = msg.match[1]
    word = msg.match[2]
    @exec = require('child_process').exec
    command = "trans :#{lang} #{word}"
    msg.send command

    @exec command, (error, stdout, stderr) ->
      #msg.send error
      msg.send stdout
      #msg.send stderr
```

```bash:transコマンドの動作確認
k> @k trans :ja binary
```

## wget

```coffee:scripts/wget.coffee
# Description:
# Commands:
#  hubot wget <url>

module.exports = (robot) ->
 robot.respond /wget (.*)$/i, (msg) ->
    url = msg.match[1]
    @exec = require('child_process').exec
    command = "wget -P ./Downloads/ #{url}"
    msg.send command

    @exec command, (error, stdout, stderr) ->
      msg.send stdout
```

```bash:準備
mkdir Downloads
```

```bash:動作確認
k wget https://pbs.twimg.com/media/Civ07I7UgAAXLOC.jpg
```

## ls

```coffee:scripts/ls.coffee
# Description:
# Commands:
#  hubot ls

module.exports = (robot) ->
 robot.respond /ls$/i, (msg) ->
    @exec = require('child_process').exec
    command = "ls -lh ./Downloads/"
    msg.send command

    @exec command, (error, stdout, stderr) ->
      #msg.send error
      msg.send stdout
      #msg.send stderr
```

## youtube-dl

```coffee:scripts/
scripts/youtube-dl.coffee 
# Description:
# Commands:
#  hubot youtube-dl <url>

module.exports = (robot) ->
 robot.respond /youtube-dl (.*)$/i, (msg) ->
    url = msg.match[1]
    @exec = require('child_process').exec
    command = "(cd ./Downloads/ && youtube-dl #{url})"
    msg.send command

    @exec command, (error, stdout, stderr) ->
      msg.send stdout
```

## hubotの名前を変えたい場合

* myhubotをkに変更例

```bash:
sed -i -e 's/myhubot/k/g' package.json 
sed -i -e 's/myhubot/k/g' README.md   
sed -i -e 's/myhubot/k/g' bin/hubot
sed -i -e 's/myhubot/k/g' bin/hubot.cmd 
```