Giter Site home page Giter Site logo

line-bot-sdk-ruby's Introduction

LINE Messaging API SDK for Ruby

Gem-version

Introduction

The LINE Messaging API SDK for Ruby makes it easy to develop bots using LINE Messaging API, and you can create a sample bot within minutes.

Documentation

See the official API documentation for more information

Also, generated documentation by YARD is available.

Requirements

This library requires Ruby 3.1 or later.

Installation

Add this line to your application's Gemfile:

gem 'line-bot-api'

And then execute:

bundle

Or install it yourself as:

gem install line-bot-api

Synopsis

Usage:

# app.rb
require 'sinatra'
require 'line/bot'

def client
  @client ||= Line::Bot::Client.new { |config|
    config.channel_id = ENV["LINE_CHANNEL_ID"]
    config.channel_secret = ENV["LINE_CHANNEL_SECRET"]
    config.channel_token = ENV["LINE_CHANNEL_TOKEN"]
  }
end

post '/callback' do
  body = request.body.read

  signature = request.env['HTTP_X_LINE_SIGNATURE']
  unless client.validate_signature(body, signature)
    error 400 do 'Bad Request' end
  end

  events = client.parse_events_from(body)
  events.each do |event|
    case event
    when Line::Bot::Event::Message
      case event.type
      when Line::Bot::Event::MessageType::Text
        message = {
          type: 'text',
          text: event.message['text']
        }
        client.reply_message(event['replyToken'], message)
      when Line::Bot::Event::MessageType::Image, Line::Bot::Event::MessageType::Video
        response = client.get_message_content(event.message['id'])
        tf = Tempfile.open("content")
        tf.write(response.body)
      end
    end
  end

  # Don't forget to return a successful response
  "OK"
end

Help and media

FAQ: https://developers.line.biz/en/faq/

News: https://developers.line.biz/en/news/

Versioning

This project respects semantic versioning.

See https://semver.org/

Contributing

Please check CONTRIBUTING before making a contribution.

License

Copyright (C) 2016 LINE Corp.
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
   http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

line-bot-sdk-ruby's People

Contributors

4geru avatar a2ikm avatar chocoby avatar d-mato avatar dependabot-preview[bot] avatar dlackty avatar etaroid avatar etrex avatar gom avatar guitarsucks avatar hirohisa avatar kenta-takeuchi avatar kimoto avatar koheisg avatar mizor avatar nagamoto avatar nkjm avatar okuramasafumi avatar renovate[bot] avatar ryota-sakamoto avatar sean0628 avatar taish avatar tamanobi avatar tkgauri avatar toduq avatar tokuhirom avatar yang-33 avatar yskkin avatar yuki-inoue avatar zenizh 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  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  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  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

line-bot-sdk-ruby's Issues

Member Join/Leave Events

Do this before creating an issue

  • Check our developer documentation and FAQ page for more information on LINE bots and the Messaging API
  • Make sure your issue is related to the LINE Bot SDK. For general questions or issues about LINE bots, create an issue on the LINE Platform feedback repository. Note that we don't provide technical support.

When creating an issue

  • Provide detailed information about the issue you had with the SDK
  • Provide logs if possible

Suggestion: image message objects

From my testing, it appears image message objects need both originalContentUrl and previewImageUrl . When I don't include the previewImageUrl, no message is sent and no error is returned. We could:

  • clearly state they are both required in the docs
  • return an error from the server rather than failing silently if required params are missing

Webhook event objects support status check

I created this question in order to know which event types are already supported and which ones are yet to be implemented.
If a particular event is already supported by Ruby bot-sdk then please tick the checkbox against it.

https://developers.line.biz/en/reference/messaging-api/#webhook-event-objects

  • Common properties
    • Source user
    • Source group
    • Source room
  • Message event
    • Text
    • Image
    • Video
    • Audio
    • File
    • Location
    • Sticker
  • Follow event
  • Unfollow event
  • Join event
  • Leave event
  • Member join event
  • Member leave event
  • Postback event
  • Beacon event
  • Account link event
  • Device link event
  • Device unlink event

Issue with Push Message API

I setup a LINE Message API account with API access, i.e. I have my "Channel Access Token" already.

When I try the examples in the documentation I get an error message:

In Ruby: (https://devdocs.line.me/en/?ruby#push-message)

message = {
  type: 'text',
  text: 'hello'
}
client = Line::Bot::Client.new { |config|
    config.channel_secret = "<channel secret>"
    config.channel_token = "<channel access token>"
}
response = client.push_message("<to>", message)
p response

Response #<Net::HTTPBadRequest 400 Bad Request readbody=true>

In Shell: (https://devdocs.line.me/en/?shell#push-message)

curl -X POST \
-H 'Content-Type:application/json' \
-H 'Authorization: Bearer {ENTER_ACCESS_TOKEN}' \
-d '{
    "to": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "messages":[
        {
            "type":"text",
            "text":"Hello, world1"
        },
        {
            "type":"text",
            "text":"Hello, world2"
        }
    ]
}' https://api.line.me/v2/bot/message/push

Response {"message":"The property, 'to', in the request body is invalid (line: -, column: -)"}

For the to field I used the mid obtained from LINE Login.

Please help!

Is changelog maintained?

I've noticed that the last update of changelog is for 1.2.2, which is more than a year ago.
I check other SDKs' changelogs and they are maintained well. I think for Ruby SDK the changelog should be maintained.

I hope when the next version comes out, the changelog will be updated as well so that I can close this issue.

[Heroku] Receiving forbidden request when I don't set a fixie proxy and internal error if a set it as endpoint

I'm trying to make it work using heroku. The message is received correctly but when I try to send a message back:

  1. I get forbidden request if I don't set any endpoint
  2. I get HTTPInternalServerError if I set to fixie http endpoint, but I'm not sure if this is correct. Really appreciate any help. Maybe @hirohisa can give some lights?
require 'line/bot'

class CallbacksController < ApplicationController
  def event_handler
    signature = request.env['HTTP_X_LINE_CHANNELSIGNATURE']
    unless client.validate_signature(request.body.read, signature)
      error 400 do 'Bad Request' end
    end

    receive_request = Line::Bot::Receive::Request.new(request.env)

    receive_request.data.each { |message|
      puts message.to_json
      case message.content

      when Line::Bot::Message::Text
         # All the code above works correctly this call is the problematic
         res = client.send_text(
          to_mid: message.from_mid,
          text: "Hola",
        )
        puts "#{res}"
        puts "#{client.endpoint}"
      end
    }

    render :nothing => true, status: :ok
  end

  def client
    client ||= Line::Bot::Client.new do |config|
      config.channel_id = ENV.fetch('LINE_CHANNEL_ID')
      config.channel_secret = ENV.fetch('LINE_CHANNEL_SECRET')
      config.channel_mid = ENV.fetch('LINE_CHANNEL_MID')
      # I get either 500 if set and forbidden if i remove this line
      config.endpoint = ENV.fetch('LINE_OUTBOUND_PROXY')
    end
  end
end

Net::OpenTimeout when calling `get_message_content`

We seem to be getting timeouts every couple of calls to the get_message_content method.

Is there some outage going on on the Messaging API at the moment?

 TID-11lmcc WARN: Net::OpenTimeout: execution expired
2018-08-14T06:46:51.921442+00:00 app[sidekiq.1]: 4 TID-11lmcc WARN: /app/vendor/ruby-2.5.1/lib/ruby/2.5.0/net/http.rb:937:in `initialize'
2018-08-14T06:46:51.921444+00:00 app[sidekiq.1]: /app/vendor/ruby-2.5.1/lib/ruby/2.5.0/net/http.rb:937:in `open'
2018-08-14T06:46:51.921445+00:00 app[sidekiq.1]: /app/vendor/ruby-2.5.1/lib/ruby/2.5.0/net/http.rb:937:in `block in connect'
2018-08-14T06:46:51.921446+00:00 app[sidekiq.1]: /app/vendor/ruby-2.5.1/lib/ruby/2.5.0/timeout.rb:103:in `timeout'
2018-08-14T06:46:51.921447+00:00 app[sidekiq.1]: /app/vendor/ruby-2.5.1/lib/ruby/2.5.0/net/http.rb:935:in `connect'
2018-08-14T06:46:51.921449+00:00 app[sidekiq.1]: /app/vendor/ruby-2.5.1/lib/ruby/2.5.0/net/http.rb:920:in `do_start'
2018-08-14T06:46:51.921450+00:00 app[sidekiq.1]: /app/vendor/ruby-2.5.1/lib/ruby/2.5.0/net/http.rb:909:in `start'
2018-08-14T06:46:51.921451+00:00 app[sidekiq.1]: /app/vendor/ruby-2.5.1/lib/ruby/2.5.0/net/http.rb:1455:in `request'
2018-08-14T06:46:51.921452+00:00 app[sidekiq.1]: /app/vendor/bundle/ruby/2.5.0/gems/skylight-core-2.0.2/lib/skylight/core/probes/net_http.rb:27:in `request'
...
2018-08-14T06:46:51.921454+00:00 app[sidekiq.1]: /app/vendor/ruby-2.5.1/lib/ruby/2.5.0/net/http.rb:1213:in `get'
2018-08-14T06:46:51.921455+00:00 app[sidekiq.1]: /app/vendor/bundle/ruby/2.5.0/gems/line-bot-api-1.2.5/lib/line/bot/httpclient.rb:35:in `get'
2018-08-14T06:46:51.921456+00:00 app[sidekiq.1]: /app/vendor/bundle/ruby/2.5.0/gems/line-bot-api-1.2.5/lib/line/bot/request.rb:73:in `get'
2018-08-14T06:46:51.921457+00:00 app[sidekiq.1]: /app/vendor/bundle/ruby/2.5.0/gems/line-bot-api-1.2.5/lib/line/bot/client.rb:350:in `get'
2018-08-14T06:46:51.921458+00:00 app[sidekiq.1]: /app/vendor/bundle/ruby/2.5.0/gems/line-bot-api-1.2.5/lib/line/bot/client.rb:164:in `get_message_content'

Example code is wrong on API Reference

I tried to use Get Profile API according to Reference (https://devdocs.line.me/en/?ruby#bot-api-get-profile):

response = client.get_profile("<userId>")
if response.code == 200 {  # <= here
  ...
}

, but it failed even if userId is valid.

I find that Net::HTTPResponse#code is string, not integer: https://docs.ruby-lang.org/en/2.0.0/Net/HTTPResponse.html

The code should be:

response = client.get_profile("<userId>")
if Net::HTTPSuccess === response  # <= Fix.
  ...
end

Same fix should be applied to Get content API.

Also, Ruby's if syntax is

if condition [then]
  ...
else
  ...
end

, not

if condition {
  ...
} else {
  ...
}

Consider dropping support for Ruby 2.1 & 2.2

Currently, this gem supports Ruby >= 2.1 but the official Ruby support for both 2.1 & 2.2 has ended, and even Ruby 2.3 will be end-of-life soon in March this year.

By raising the minimum supported Ruby version, we can avoid issue like #96, adopt newer version of dependencies (e.g. Rubocop), and use newer Ruby syntax and built-in methods.

It seems that no API to retrieve the mid list of the friends of a bot account

Sorry I didn't find a good place to submit this question if it is not suitable to response here. It would be great if Line can add this API for the purpose of broadcasting messages from the Line Bot, otherwise a Line bot needs to remember the mid from each sender.

I have another question.
1.Besides using scanning QR-code to add a Line bot account as a friend, is there any other way to add this? Which means does/will a Line bot account have a searchable Line ID?

Thanks.

Get user ID from group chat message

When receiving a message from a room, only the room ID is given as the source of the message. How do I look up the user ID of the sender?

NoMethodError raised on Line::Bot::Event:Message

My bot actually works but on the Sinatra logging info there is an exception raised. Although my bot works by replying the message, I always retrieve 500 response code in the logging.

[2018-12-14 18:17:00] ERROR NoMethodError: undefined method `bytesize' for #<Line::Bot::Event::Message:0x000055f01290e568>
...
[14/Dec/2018:18:16:59 UTC] "POST /callback HTTP/1.1" 500 396

It seems the problem is within line-bot-sdk-ruby. Could you fix this?

Ruby used: v2.5.3
Sinatra used: v2.0.4
LINE bot SDK (line-bot-api) used: v1.3.0

message json object convert to class object

pro

  • validate unsend object. and decrease broken object requests.
  • decrease wrong type object.

con

  • need to rewrite document.

app.rb

  • before
message = {
  type: 'text',
  text: event.message['text']
}
client.reply_message(event['replyToken'], message)
  • after
message = MessageReply.new(
  text: event.message['text']
)
client.reply_message(event['replyToken'], message)

class object

  • MessageReply
class MessageReply
  def initialize(text)
    raise ArgumentError, 'Error text is over 2000 words' if text.length > 2000
    raise ArgumentError, 'Error text is 0 words' if text == ''
    @text = text
  end

  def reply
    expected = {
      type: 'text',
      text: @text
    }
  end
end

[Question] API returns 423 status with documented request

Hi. I'm trying to call the API from Postman and the request is going through but no matter which value i set for eventType is returning status code: 423 (evenType is invalid)

postman

@hirohisa are there any changes in the eventType codes that you could share with us? Really appreciate the help

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.