Line bot APIのラッパーGem作ったヨ〜

  • 51
    Like
  • 0
    Comment
More than 1 year has passed since last update.

あいさつ

流行りにはしっかり乗らさせて頂きたい!

line botちょこちょこ遊んでいた中で出来た成果物をAPIのruby言語ラッパーとしてgem化しときました^^

Github

Rubygems

即時性や並列性を考えるとガチBotには向いてないかもですがまあサクッと遊ぶときに使ってくださいな^^

ちなみにこのライブラリとheroku+Fixieで作ったbotをひけらかすとこんな感じ

参考: LINE BOT をとりあえずタダで Heroku で動かす

Slack for iOS Upload.png.jpeg

情報のフェッチだけじゃなくて予約とか何か副作用の持つアクションと絡めたり、グループチャットにbot入れてスケージュリングとかは夢が広がる〜!

使い方

Gem Version

Installation

$ gem install line-bot

Usage

require 'line/bot/client'

client = Line::Bot::Client.new do |config|
  config.channel_id     = "LINE_CHANNEL_ID"
  config.channel_secret = "LINE_CHANNEL_SECRET"
  config.channel_mid    = "LINE_CHANNEL_MID"
  config.proxy          = "http://proxy.example.com/" # Optional
end

client.get_profiles(["array_of_mids_of_user"])
# => {
# :status=>200,
# :body=>{"contacts"=>[{"displayName"=>"Aisaac", ...

client.send_text(["array_of_mids_of_target_recipient"], text: "Hello world")
#=> {
# :status=>200,
# :body=>{"failed"=>[], "messageId"=>"1460267791059", "timestamp"=>1460267791059...

client.get_message_content("1460267791059")
# => {
# :status=>200,
# :body=> {"result":[{"content":{...

雰囲気はこんな感じパラメーターのキーはなるべくline bot api公式と同じものをそのまま渡せる感じにしてる

All examples

client.send_text(["uxxxxxxxxxxxxx"],
  text: "Hi!"
)

client.send_image(["uxxxxxxxxxxxxx"],
  originalContentUrl: "http://example.com/original.jpg",
  previewImageUrl: "http://example.com/preview.jpg"
)

client.send_video(["uxxxxxxxxxxxxx"],
  originalContentUrl: "http://example.com/original.jpg",
  previewImageUrl: "http://example.com/preview.jpg"
)

client.send_audio(["uxxxxxxxxxxxxx"],
  originalContentUrl: "http://example.com/original.m4a",
  contentMetadata: {
    "AUDLEN" => "240000"
  }
)

client.send_location(["uxxxxxxxxxxxxx"],
  text: "Hi!",
  location: {
    title: "Convention center",
    latitude: 35.61823286112982,
    longitude: 139.72824096679688
  }
)

client.send_sticker(["uxxxxxxxxxxxxx"],
  contentMetadata: {
    "STKID" => "3",
    "STKPKGID" => "332",
    "STKVER" => "100" # Optional
  }
)

client.send_messages(["uxxxxxxxxxxxxx"], [
  {
    contentType: Line::Bot::Constants::ContentType::TEXT,
    text: "First message"
  },
  {
    contentType: Line::Bot::Constants::ContentType::IMAGE,
    originalContentUrl: "http://example.com/original.jpg",
    previewImageUrl: "http://example.com/original.jpg"
  }
])

client.get_profiles(["uxxxxxxxxxxxxx"])

client.get_message_content("1460267791059")

client.get_message_content_preview("1460267791059")

複数メッセージ送信(#send_messages)に使う定数一覧

module ContentType
  TEXT     = 1
  IMAGE    = 2
  VIDEO    = 3
  AUDIO    = 4
  LOCATION = 7
  STICKER  = 8
  CONTACT  = 10
end

開発

Setup


$ git clone https://github.com/aisaac-lab/line-bot

$ cd line-bot

$ bundle install --path vendor/bundle

# Option 1
$ cp .envrc.sample .envrc

( update .envrc as your configs )

$ direnv allow .

# Option 2
$ export LINE_CHANNEL_ID=xxxxxxx
$ export LINE_CHANNEL_SECRET=xxxxxxx
$ export LINE_CHANNEL_MID=xxxxxxx

run test

$ bundle exec test

Try it in console

$ bin/console

Contributing

どんなレポートやPR歓迎です^^ https://github.com/aisaac-lab/line-bot.

License

MIT License.

その内

callbackの受け手を作るrackアプリ機能も持たせようと思っていますー