#33: N 予備校 Hubot の動かし方など

やったこと(Done)

  • N 予備校 プログラミング入門 Web アプリコース
    • 第 3 章 サーバーサイドプログラミング入門
      • 07. Hubot と Slack アダプタ

N 予備校 第 3 章

yarn で必要なモジュールをインストールします。

$ yarn global add yo@3.0.0 # Yeoman
$ yarn global add generator-hubot
$ yarn global add coffeescript@1.12.7
$ which yo
/home/vagrant/.yarn/bin/yo
$ which coffee
/home/vagrant/.yarn/bin/coffee
$ yo hubot --adapter=slack
$ ls
.   .editorconfig  Procfile   bin                    hubot-scripts.json  package-lock.json  scripts
..  .gitignore     README.md  external-scripts.json  node_modules        package.json

いろいろなファイルを用意してくれたようです。

VM の方には入っていなかったので tree をインストールします。

$ sudo apt intall tree

教材テキストのとおり、Hubot の動作を実装します。

Image from Gyazo

こんな感じでできました。

'use strict';
module.exports = (robot) => {
    robot.hear(/hello>/i, (msg) => {
        const username = msg.message.user.name;
        msg.send('Hello, ' + username);
    });
    robot.hear(/おみくじ/i, (msg) => {
        const username = msg.message.user.name;
        const lots = ['大吉', '吉', '中吉', '末吉', '凶'];
        const lot = lots[Math.floor(Math.random() * lots.length)];
        msg.send(`${lot}, ` + username);
    });
};

わかったこと(Fact)

  • Slack の username を取得するのは msg.message.user.name とする。
  • あるいは msg.message.user.id を使って <@${user_id}> とする。

参考文献(References)

その他

そのうちやること(Someday)

  • [ ] テスト用のディレクトリ、テストの雛形ファイルなどを Rake で生成したい。
  • [ ] RubyMine インストールしてみる?
  • [ ] Ruby を書くためのエディタの設定(Emacs
  • [ ] bash の設定ファイルに必要なエイリアスを追加する。