@@ -41,45 +41,72 @@ gem install line-bot-api
4141```
4242
4343## Synopsis
44+ ### RBS
45+ This library provides [ RBS] ( https://github.com/ruby/rbs ) files for type checking.\
46+ You can code with type support in the corresponding IDE or editor.
4447
45- Usage:
48+ ### Basic Usage
4649
4750``` ruby
4851# app.rb
4952require ' sinatra'
50- require ' line/bot'
53+ require ' line-bot-api'
54+
55+ set :environment , :production
5156
5257def client
53- @client ||= Line ::Bot ::Client .new { |config |
54- config.channel_id = ENV [" LINE_CHANNEL_ID" ]
55- config.channel_secret = ENV [" LINE_CHANNEL_SECRET" ]
56- config.channel_token = ENV [" LINE_CHANNEL_TOKEN" ]
57- }
58+ @client ||= Line ::Bot ::V2 ::MessagingApi ::ApiClient .new (
59+ channel_access_token: ENV .fetch(" LINE_CHANNEL_ACCESS_TOKEN" )
60+ )
61+ end
62+
63+ def blob_client
64+ @blob_client ||= Line ::Bot ::V2 ::MessagingApi ::ApiBlobClient .new (
65+ channel_access_token: ENV .fetch(" LINE_CHANNEL_ACCESS_TOKEN" )
66+ )
67+ end
68+
69+ def parser
70+ @parser ||= Line ::Bot ::V2 ::WebhookParser .new (channel_secret: ENV .fetch(" LINE_CHANNEL_SECRET" ))
5871end
5972
6073post ' /callback' do
6174 body = request.body.read
62-
6375 signature = request.env[' HTTP_X_LINE_SIGNATURE' ]
64- unless client.validate_signature(body, signature)
65- error 400 do ' Bad Request' end
76+
77+ begin
78+ events = parser.parse(body: body, signature: signature)
79+ rescue Line ::Bot ::V2 ::WebhookParser ::InvalidSignatureError
80+ halt 400 , { ' Content-Type' => ' text/plain' }, ' Bad Request'
6681 end
6782
68- events = client.parse_events_from(body)
6983 events.each do |event |
7084 case event
71- when Line ::Bot ::Event ::Message
72- case event.type
73- when Line ::Bot ::Event ::MessageType ::Text
74- message = {
75- type: ' text' ,
76- text: event.message[' text' ]
77- }
78- client.reply_message(event[' replyToken' ], message)
79- when Line ::Bot ::Event ::MessageType ::Image , Line ::Bot ::Event ::MessageType ::Video
80- response = client.get_message_content(event.message[' id' ])
85+ when Line ::Bot ::V2 ::Webhook ::MessageEvent
86+ case event.message
87+ when Line ::Bot ::V2 ::Webhook ::TextMessageContent
88+ case event.message.text
89+ when ' profile'
90+ if event.source.type == ' user'
91+ profile_response = client.get_profile(user_id: event.source.user_id)
92+ reply_text(event, " Display name: #{ profile_response.display_name } \n Status message: #{ profile_response.status_message } " )
93+ else
94+ reply_text(event, " Bot can't use profile API without user ID" )
95+ end
96+ else
97+ request = Line ::Bot ::V2 ::MessagingApi ::ReplyMessageRequest .new (
98+ reply_token: event.reply_token,
99+ messages: [
100+ Line ::Bot ::V2 ::MessagingApi ::TextMessage .new (text: " [ECHO] #{ event.message.text } " )
101+ ]
102+ )
103+ client.reply_message(reply_message_request: request)
104+ end
105+
106+ when Line ::Bot ::V2 ::Webhook ::ImageMessageContent , Line ::Bot ::V2 ::Webhook ::VideoMessageContent
107+ response = blob_client.get_message_content(message_id: event.message.message_id)
81108 tf = Tempfile .open (" content" )
82- tf.write(response.body )
109+ tf.write(response)
83110 end
84111 end
85112 end
@@ -89,19 +116,195 @@ post '/callback' do
89116end
90117```
91118
92- ## Migration way from v1 to v2
93- Migration guide: https://github.com/line/line-bot-sdk-ruby/releases/tag/v1.30.0
119+ ### Use HTTP Information
120+ You may need to store the ``` x-line-request-id ``` header obtained as a response from several APIs.\
121+ In this case, please use ``` *_with_http_info ``` methods. You can get headers and status codes.\
122+ The ` x-line-accepted-request-id ` or ` content-type ` header can also be obtained in the same way. Note header name must be lowercase.
123+
124+ ``` ruby
125+ push_request = Line ::Bot ::V2 ::MessagingApi ::PushMessageRequest .new (
126+ to: event.source.user_id,
127+ messages: [
128+ Line ::Bot ::V2 ::MessagingApi ::TextMessage .new (text: " [^Request ID] #{ headers[' x-line-request-id' ] } " )
129+ ]
130+ )
131+ _body , _status_code , headers = client.push_message_with_http_info(push_message_request: push_request)
132+
133+ puts headers[' x-line-request-id' ]
134+ puts headers[' x-line-accepted-request-id' ]
135+ puts headers[' content-type' ]
136+ ```
137+
138+ ### Error handling
139+ If an API call fails, the SDK does not generate an Error in Ruby; use the status code for proper error handling.
140+ Error details are stored in body.
141+
142+ ``` ruby
143+ require ' json'
144+ require ' line-bot-api'
145+
146+ def client
147+ @client ||= Line ::Bot ::V2 ::MessagingApi ::ApiClient .new (
148+ channel_access_token: ENV .fetch(" LINE_CHANNEL_ACCESS_TOKEN" ),
149+ )
150+ end
151+
152+ def main
153+ dummy_message = Line ::Bot ::V2 ::MessagingApi ::TextMessage .new (
154+ text: " Hello, world!" ,
155+ )
156+
157+ valid_request = Line ::Bot ::V2 ::MessagingApi ::ValidateMessageRequest .new (
158+ messages: [dummy_message, dummy_message, dummy_message, dummy_message, dummy_message],
159+ )
160+ body, status_code, _headers = client.validate_push_with_http_info(validate_message_request: valid_request)
161+ handle_response(body, status_code)
162+
163+ invalid_request = Line ::Bot ::V2 ::MessagingApi ::ValidateMessageRequest .new (
164+ messages: [dummy_message, dummy_message, dummy_message, dummy_message, dummy_message, dummy_message],
165+ )
166+ body, status_code, _headers = client.validate_push_with_http_info(validate_message_request: invalid_request)
167+ handle_response(body, status_code)
168+ end
169+
170+ def handle_response (body , status_code )
171+ case status_code
172+ when 200
173+ puts " Valid"
174+ when 400 ..499
175+ puts " Invalid: #{ JSON .parse(body)} "
176+ else
177+ puts " Other Status: #{ status_code } "
178+ end
179+ end
180+
181+ main
182+ ```
183+
184+ ### Use with Hash / JSON
185+ You can use Hash instead of the SDK classes.
186+ So you can also use Hash parsed from JSON as a parameter.
187+
188+ This is useful, for example, in migrating from v1 or building Flex Message.
189+
190+ ** But this is not recommended because you lose type checking by RBS.**
191+
192+ ``` ruby
193+ client = Line ::Bot ::V2 ::MessagingApi ::ApiClient .new (
194+ channel_access_token: ENV .fetch(" LINE_CHANNEL_ACCESS_TOKEN" ),
195+ )
196+
197+ request = {
198+ to: " U4af4980629..." ,
199+ messages: [
200+ {
201+ type: " flex" ,
202+ alt_text: " This is a Flex Message" ,
203+ contents: {
204+ type: " bubble" ,
205+ body: {
206+ type: " box" ,
207+ layout: " horizontal" ,
208+ contents: [
209+ {
210+ type: " text" ,
211+ text: " Hello"
212+ }
213+ ]
214+ }
215+ }
216+ }
217+ ]
218+ }
219+ client.push_message(push_message_request: request)
220+
221+ # or
222+
223+ request = JSON .parse(
224+ <<~JSON
225+ {
226+ "to": "U4af4980629...",
227+ "messages": [
228+ {
229+ "type": "flex",
230+ "alt_text": "This is a Flex Message",
231+ "contents": {
232+ "type": "bubble",
233+ "body": {
234+ "type": "box",
235+ "layout": "horizontal",
236+ "contents": [
237+ {
238+ "type": "text",
239+ "text": "Hello"
240+ }
241+ ]
242+ }
243+ }
244+ }
245+ ]
246+ }
247+ JSON
248+ )
249+ client.push_message(push_message_request: request)
250+ ```
251+
252+ #### Convert to SDK classes
253+ You can convert Hash / JSON to SDK classes using ` #create ` method.
254+
255+ ``` ruby
256+ json = <<~JSON
257+ {
258+ "to": "U4af4980629...",
259+ "messages": [
260+ {
261+ "type": "flex",
262+ "alt_text": "This is a Flex Message",
263+ "contents": {
264+ "type": "bubble",
265+ "body": {
266+ "type": "box",
267+ "layout": "horizontal",
268+ "contents": [
269+ {
270+ "type": "text",
271+ "text": "Hello"
272+ }
273+ ]
274+ }
275+ }
276+ }
277+ ]
278+ }
279+ JSON
280+ request = Line ::Bot ::V2 ::MessagingApi ::PushMessageRequest .create(
281+ JSON .parse(json)
282+ )
283+ ```
94284
95- ## Help and media
96- FAQ: https://developers.line.biz/en/faq/
285+ ### More examples
286+ See the [ examples ] ( examples/v2 ) directory for more examples.
97287
288+ ## Media
98289News: https://developers.line.biz/en/news/
99290
100291## Versioning
101292This project respects semantic versioning.
102293
103294See https://semver.org/
104295
296+ ### v1 and v2
297+ v1 and v2 are completely different implementations, v1 is all deprecated and will be removed in the future.
298+ Migration to v2 is strongly recommended.
299+
300+ Migration guide: https://github.com/line/line-bot-sdk-ruby/releases/tag/v1.30.0
301+
302+
303+ #### Deprecation warnings
304+ When calling the API endpoint with v1, a deprecation warning will occur.\
305+ If you want to suppress this, please set any value to the environment variable ` SUPRESS_V1_DEPRECATION_WARNINGS ` .
306+
307+
105308## Contributing
106309Please check [ CONTRIBUTING] ( CONTRIBUTING.md ) before making a contribution.
107310
0 commit comments