hubot-line-message-apiをつくった


BOT API Trial Account廃止

新しいMessaging APIが登場してしばらくたちましたが、とうとう既存のBOT API Trial Accountが廃止されます。
Messaging API のリリースとBOT API Trial Account廃止のご案内

旧API トライアルアカウント「BOT API Trial Account」をお使いの皆さまには、新しいトライアルアカウント「Developer Trial」のご利用をお願い致します。

だそうです。
新しいトライアルアカウントはこちらからどうぞ。

今までとは違うところ

LINE Messaging API が BOT API から変わったところ。 - Qiita
を見ていただければ分かる通り、送受信のJSONがかわります。

hubotでつくっとったbotはどうなるんや・・・

hubot-line-trialを使ってhubotからlinebotに通知させてみた - Qiitaの記事をかいた@tochi_ondyさんが作っていただいたアダプターなどが使えなくなっているので、せっかくなので自分で作ってみました。

hubot-line-message-api

  • 基本的にherokuで公開するコトを想定して作っています。これらを設定してあげてください。
    • LINE_CHANNEL_ACCESS_TOKEN
      • LINEアカウントのBasic InfomationにあるChannel Access Tokenの値を設定してください。
    • HUBOT_ENDPOINT
      • defaultで/hubot/incomingになってます。
      • 自由に設定していただいて大丈夫です。
    • FIXIE_URL
      • herokuで走らす場合は必須かなと思います。
      • Fixieというアドオンを使います。
      • $ heroku addons:create fixie:tricycleを叩くと自動で設定されています。
      • 出力されるIPアドレスをServer IP Whitelistに設定してあげてください。
  • Yeomanでつくると楽です
    • adapterにline-message-apiを指定してください

ここらへんはググるとすぐ出てくると思います。

つかいかた

  • まずダウンロード
    $ git clone https://github.com/pyonk/hubot-line-message-api.git

  • yoで作ってください

  • package.jsonにローカルのものを登録します

package.json
"dependencies": {
    "line-message-api": "file:./hubot-line-message-api"
}

できること

今のところpushで送信は対応できておらず、返信しかできません。

  • 返信

    • テキスト

    https://devdocs.line.me/ja/#text

    module.exports = (robot) ->
        robot.hear /^テキスト$/, (res) ->
            res.reply
                type: 'text'
                contents: ['nyaa']
    
    • 画像と動画

    https://devdocs.line.me/ja/#image
    https://devdocs.line.me/ja/#video

    module.exports = (robot) ->
        robot.hear /^画像$/, (res) ->
            res.reply
                type:'image'# 'video'
                content: [
                    original: 'https://example.com/images/image.jpg'
                    preview: 'https://example.com/images/image.jpg'
                ]
    
    • ボタン

    https://devdocs.line.me/ja/#buttons

    module.exports = (robot) ->
        robot.hear /^テキスト$/, (res) ->
            res.reply
                type: 'buttons'
                altText: 'hogehoge'
                contents: [
                    image: 'https://example.com/images/image.jpg'
                    title: 'this is Buttons'
                    text: 'buttons description'
                    actions:[
                        type: 'uri'
                        label: 'Open in Browser'
                        uri: 'http://example.com/'
                    ]
                ]
    
    • カルーセル

    https://devdocs.line.me/ja/#carousel

    module.exports = (robot) ->
        robot.hear /^カルーセル$/, (res) ->
            res.reply
                type: 'carousel'
                altText: 'hogehoge'
                contents: [
                    image: 'https://example.com/images/image.jpg'
                    title: 'this is Buttons'
                    text: 'buttons description'
                    actions:[
                        type: 'uri'
                        label: 'Open in Browser'
                        uri: 'http://example.com/'
                    ],
                    image: 'https://example.com/images/image.jpg'
                    title: 'this is Buttons'
                    text: 'buttons description'
                    actions:[
                        type: 'uri'
                        label: 'Open in Browser'
                        uri: 'http://example.com/'
                    ]...
                ]
    
    • くみあわせ
    module.exports = (robot) ->
        robot.hear /^くみあわせ$/, (res) ->
            res.reply {
                type: 'text'
                contents: ['nyaa']
            },
            {
                type: 'buttons'
                contents: [
                    image: 'https://example.com/images/image.jpg'
                    title: 'this is Buttons'
                    text: 'buttons description'
                    actions: [
                        type: 'uri'
                        label: 'Open in Browser'
                        uri: 'http://example.com/'
                    ]
                ]
            }
    

注意点

  • contents.length <= 5にしないとLINEに怒られます。
    • くみあわせて使う場合はcontents.lengthを足し算した値が5を超えないようにしないと怒られます。
  • 画像のURLなどはhttpsでないと怒られます。

最後に

APIのリファレンス読んでおけば問題ないとおもいます。
初めてcoffeescriptをちゃんと触って、アダプターも初めて書きました。
至らない点もあるかと思いますので、教えていただくと幸いです。

まだ機能的に十分ではないので、暇を見て機能追加していきます。
ソースコードはこちらです。