Giter VIP home page Giter VIP logo

chabot's Introduction

Chabot

Chabot is Web-hook receiver for ChatWork.

Chabot とは

Chabot は ChatWork と Webhook を提供しているサービスとを連携させるアプリケーションです。

WebHook で受け取ったデータを、ejs テンプレートで定義したメッセージを指定したチャットに送信することが出来ます。

インストール

npm install -g chabot

使い方

インストールが完了すると chabot コマンドが利用できるようになります。

$ chabot -h

  Usage: chabot [options] [command]

  Commands:

    create [options] [appname] create chabot app

  Options:

    -h, --help     output usage information
    -V, --version  output the version number

Chabot アプリケーションを作成する

chabot アプリの作成は chabot create です。-d オプションで作成したい場所のディレクトリパスを指定します。

$ chabot create -h

  Usage: create [options] [appname]

  Options:

    -h, --help        output usage information
    -d, --dist [dir]  the directory to place the app in [default: CWD]
    -f, --force       overwrite existing directory

コマンドを実行してみる

実際に chabot アプリを作成してみましょう。

$ chabot create -d ~/ my_first_chabot
  copying files.
  completed!
   > /Users/astronaughts/chabot/my_first_chabot

設定

設定ファイルを開いてみましょう。

$ cd ~/chabot/my_first_chabot
$ vi config.json

初期値として github というボットの設定があります。

{
    "port": 5000,
    "bots": {
        "github": {
            "hostname": "github.com",
            "token": "YOUR_TOKEN",
            "route": "/github/hooks/:roomid"
        }
    }
}
port

ポートを指定します。

bots

ボットの設定を指定します。複数定義可能です。

bot: token

ボットの発言としたいアカウントの ChatoWork API のトークンを指定します。

bot: route

外部サービスの WebHook の送信先として URL を指定します。 URL には :roomid を必ず含めるようにします。 :roomid はボットに発言させたいチャットの ID を指定します。

アプリの構造

  • my_first_chabot/
    • bots/
    • templates/
    • node_modules/
    • app.js
    • config.json
    • package.json
bots ディレクトリ

bot の実装ファイルをここに配置します。

templates ディレクトリ

bot が送信するメッセージのテンプレートファイルをここに配置します。

ボットの実装

chabot create で最初にサンプルとして bots/github.js が配置されています。

module.exports = function (chabot) {

    // WebHook で受けたデータをセット
    var payload = JSON.parse(chabot.data.payload);
    // ChatWork API の endpoint をセット
    var endpoint = '/rooms/' + chabot.roomid + '/messages';
    // templats/ 内のメッセージテンプレートを読み込む
    var template = chabot.readTemplate('github.ejs');
    // WebHook で受けたデータでメッセージテンプレートを描画
    var message_body = chabot.render(template, payload);

    // ChatWork API でメッセージ送信
    chabot.client
        .post(endpoint, {
            body: message_body
        })
        .done(function (res) {
            chabot.log('done');
        })
        .fail(function (err) {
            chabot.error(err);
        });
};

chabot オブジェクト

chabot.roomid

URL に指定されたチャットルームの ID です。

chabot.client

ChatWork API を操作するクライアントです。詳細は以下を参照。

astronaughts/simple-cw-node - github

chabot.data

WebHook から受け取った response です。

chabot.readTemplate

templates で配置したテンプレートファイルを読み込みます。

chabot.render

読み込んだテンプレートファイルを描画します。

テンプレートの作成

テンプレートは ejs が利用できます。

visionmedia/ejs - github

github ボットのテンプレートは以下のように定義してあります。

プッシュのお知らせ♪
[info]<%= head_commit.message %>
[hr]変更のあったファイル:
<% if (head_commit.added.length) { %>【追加】
<% head_commit.added.forEach(function (file) { %> <%= file %>
<% }) %><% } else { %>【追加】
 なし
<% } %><% if (head_commit.removed.length) { %>【削除】
<% head_commit.removed.forEach(function (file) { %> <%= file %>
<% }) %><% } else { %>【削除】
 なし
<% } %><% if (head_commit.modified.length) { %>【修正】
<% head_commit.modified.forEach(function (file) { %> <%= file %>
<% }) %><% } else { %>【修正】
 なし
<% } %>[hr]リポジトリ:<%= repository.name %>
コミット :<%= head_commit.url %>
コミッター:<%= head_commit.committer.username %>
[/info]

実行

以下のコマンドで簡単に実行可能です。

$ node app
loaded bot: github

curl などで、github の WebHook のデータを試しに送信してみてください。

github WebHook sample JSON

chabot's People

Contributors

astronaughts avatar ledsun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

chabot's Issues

Cannot GET /

何度やっても Cannot GET / になるのですが、
Cannot GET / のなって正常なのでしょうか?

Use template-literal instead of EJS

Template Literal is fastest, smallest and simplest template engine, because it use JS's literal template feature.

It's 55 times faster than EJS, and it also use less CPU and RAM ressources, so it may be a good idea to use it instead of EJS 😀

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.