Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
mokuzon committed Dec 18, 2024
1 parent c84e669 commit ca2bfcd
Showing 1 changed file with 117 additions and 22 deletions.
139 changes: 117 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,42 +42,66 @@ gem install line-bot-api

## Synopsis

Usage:
### Basic Usage

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

set :environment, :production

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"]
}
@client ||= Line::Bot::V2::MessagingApi::ApiClient.new(
channel_access_token: ENV.fetch("LINE_CHANNEL_ACCESS_TOKEN")
)
end

def blob_client
@blob_client ||= Line::Bot::V2::MessagingApi::ApiBlobClient.new(
channel_access_token: ENV.fetch("LINE_CHANNEL_ACCESS_TOKEN")
)
end

def parser
@parser ||= Line::Bot::V2::WebhookParser.new(channel_secret: ENV.fetch("LINE_CHANNEL_SECRET"))
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

begin
events = parser.parse(body, signature)
rescue Line::Bot::V2::WebhookParser::InvalidSignatureError
halt 400, { 'Content-Type' => 'text/plain' }, 'Bad Request'
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'])
when Line::Bot::V2::Webhook::MessageEvent
case event.message
when Line::Bot::V2::Webhook::TextMessageContent
case event.message.text
when 'profile'
if event.source.type == 'user'
profile_response = client.get_profile(user_id: event.source.user_id)
reply_text(event, "Display name: #{profile_response.display_name}\nStatus message: #{profile_response.status_message}")
else
reply_text(event, "Bot can't use profile API without user ID")
end
else
request = Line::Bot::V2::MessagingApi::ReplyMessageRequest.new(
reply_token: event.reply_token,
messages: [
Line::Bot::V2::MessagingApi::TextMessage.new(type: 'text', text: "[ECHO] #{event.message.text}")
]
)
client.reply_message(reply_message_request: request)
end

when Line::Bot::V2::Webhook::ImageMessageContent, Line::Bot::V2::Webhook::VideoMessageContent
response = blob_client.get_message_content(message_id: event.message.message_id)
tf = Tempfile.open("content")
tf.write(response.body)
end
Expand All @@ -89,16 +113,87 @@ post '/callback' do
end
```

## Help and media
FAQ: https://developers.line.biz/en/faq/
### Use HTTP Information
You may need to store the ```x-line-request-id``` header obtained as a response from several APIs.
In this case, please use ```*_with_http_info``` methods. You can get headers and status codes.
The `x-line-accepted-request-id` or `content-type` header can also be obtained in the same way.

```ruby
push_request = Line::Bot::V2::MessagingApi::PushMessageRequest.new(
to: event.source.user_id,
messages: [
Line::Bot::V2::MessagingApi::TextMessage.new(type: 'text', text: "[^Request ID] #{headers['x-line-request-id']}")
]
)
_body, _status_code, headers = client.push_message_with_http_info(push_message_request: push_request)

puts headers['x-line-request-id']
puts headers['x-line-accepted-request-id']
puts headers['content-type']
```

### Error handling
If an API call fails, the SDK does not generate an Error in Ruby; use the status code for proper error handling.
Error details are stored in body.

```ruby
require 'json'
require 'line/bot'

def client
@client ||= Line::Bot::V2::MessagingApi::ApiClient.new(
channel_access_token: ENV.fetch("LINE_CHANNEL_ACCESS_TOKEN"),
)
end

def main
dummy_message = Line::Bot::V2::MessagingApi::TextMessage.new(
type: "text",
text: "Hello, world!",
)

valid_request = Line::Bot::V2::MessagingApi::ValidateMessageRequest.new(
messages: [dummy_message, dummy_message, dummy_message, dummy_message, dummy_message],
)
body, status_code, _headers = client.validate_push_with_http_info(validate_message_request: valid_request)
handle_response(body, status_code)

invalid_request = Line::Bot::V2::MessagingApi::ValidateMessageRequest.new(
messages: [dummy_message, dummy_message, dummy_message, dummy_message, dummy_message, dummy_message],
)
body, status_code, _headers = client.validate_push_with_http_info(validate_message_request: invalid_request)
handle_response(body, status_code)
end

def handle_response(body, status_code)
case status_code
when 200
puts "Valid"
when 400..499
puts "Invalid: #{JSON.parse(body)}"
else
puts "Other Status: #{status_code}"
end
end

main
```

### More examples
See the [examples](examples/v2) directory for more examples.

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

## Versioning
This project respects semantic versioning.

See https://semver.org/

### v1 and v2
v1 and v2 are completely different implementations, v1 is all deprecated and will be removed in the future.
Migration to v2 is strongly recommended.

## Contributing
Please check [CONTRIBUTING](CONTRIBUTING.md) before making a contribution.

Expand Down

0 comments on commit ca2bfcd

Please sign in to comment.