line-bot-api 1.30.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: af4ad962794c7d219f9acb71cbc6e4af3c0b9749087daba28d73d54d2075e0d5
4
- data.tar.gz: c219bdefdc560590bddc4f5887e1f219f498867efd0adbcf0e7f051d5dec35ef
3
+ metadata.gz: b046b49fcf0cca9aa1a7d59650aaf5265e71ddd27099bc876b64d4e73cd812b3
4
+ data.tar.gz: 81fb1a37c8946e4e015abcf1daa255ef5459100f939179dc0f9c418e4371a637
5
5
  SHA512:
6
- metadata.gz: de3b983cab0150be06eb0470679f9a737d039fca49c29c1b7984a0a1648b3576c839835fa2b085cb75b3cbe966ab8c2acf93048519a51b4cac16e82856e9cc2b
7
- data.tar.gz: e47210b21072b98f04ba084c90ac80eb66e32b8193ffa64abd85014718c29f2fdaf014de07146ca89a2fd8614be547b28f7a00cb45fc743a16e5e6cc64d1672a
6
+ metadata.gz: 6855934b0a92dbe458414a7ebc3ad894b79f2288e17a2e13105fc5505764008823d04bc9606f53c2f69f5fec09d0b84ad3bf2b83f906a3b6f32640f45b414d78
7
+ data.tar.gz: 7fbfaf4bbd423ed0beb872b97797886827a0c5a629f2dabf7f410f01883d8f06288be9236736ba7cf7d2f0c30484fda340701f1ce118172eabe373fef5f98c42
data/README.md CHANGED
@@ -41,45 +41,72 @@ gem install line-bot-api
41
41
  ```
42
42
 
43
43
  ## 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.
44
47
 
45
- Usage:
48
+ ### Basic Usage
46
49
 
47
50
  ```ruby
48
51
  # app.rb
49
52
  require 'sinatra'
50
- require 'line/bot'
53
+ require 'line-bot-api'
54
+
55
+ set :environment, :production
51
56
 
52
57
  def 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"))
58
71
  end
59
72
 
60
73
  post '/callback' do
61
74
  body = request.body.read
62
-
63
75
  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'
66
81
  end
67
82
 
68
- events = client.parse_events_from(body)
69
83
  events.each do |event|
70
84
  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}\nStatus 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)
81
108
  tf = Tempfile.open("content")
82
- tf.write(response.body)
109
+ tf.write(response)
83
110
  end
84
111
  end
85
112
  end
@@ -89,9 +116,176 @@ post '/callback' do
89
116
  end
90
117
  ```
91
118
 
92
- ## Help and media
93
- FAQ: https://developers.line.biz/en/faq/
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
94
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
+ ```
284
+
285
+ ### More examples
286
+ See the [examples](examples/v2) directory for more examples.
287
+
288
+ ## Media
95
289
  News: https://developers.line.biz/en/news/
96
290
 
97
291
  ## Versioning
@@ -99,6 +293,18 @@ This project respects semantic versioning.
99
293
 
100
294
  See https://semver.org/
101
295
 
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
+
102
308
  ## Contributing
103
309
  Please check [CONTRIBUTING](CONTRIBUTING.md) before making a contribution.
104
310
 
@@ -2,6 +2,6 @@ module Line
2
2
  module Bot
3
3
  # This version is updated before releasing a new version in the release process.
4
4
  # You don't have to update this version manually.
5
- VERSION = "1.30.0"
5
+ VERSION = "2.0.0"
6
6
  end
7
7
  end
data/lib/line/bot.rb CHANGED
@@ -12,15 +12,6 @@
12
12
  # License for the specific language governing permissions and limitations
13
13
  # under the License.
14
14
 
15
- # V1
16
- require 'line/bot/v1/util'
17
- require 'line/bot/v1/client'
18
- require 'line/bot/v1/event'
19
- require 'line/bot/v1/api/errors'
20
- require 'line/bot/v1/api'
21
- require 'line/bot/v1/httpclient'
22
- require 'line/bot/v1/api/version'
23
-
24
15
  # V2
25
16
  require 'line/bot/v2/webhook_parser'
26
17
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: line-bot-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.30.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - LINE Corporation
@@ -49,27 +49,6 @@ files:
49
49
  - README.md
50
50
  - lib/line-bot-api.rb
51
51
  - lib/line/bot.rb
52
- - lib/line/bot/v1/api.rb
53
- - lib/line/bot/v1/api/errors.rb
54
- - lib/line/bot/v1/api/version.rb
55
- - lib/line/bot/v1/client.rb
56
- - lib/line/bot/v1/event.rb
57
- - lib/line/bot/v1/event/account_link.rb
58
- - lib/line/bot/v1/event/base.rb
59
- - lib/line/bot/v1/event/beacon.rb
60
- - lib/line/bot/v1/event/follow.rb
61
- - lib/line/bot/v1/event/join.rb
62
- - lib/line/bot/v1/event/leave.rb
63
- - lib/line/bot/v1/event/member_joined.rb
64
- - lib/line/bot/v1/event/member_left.rb
65
- - lib/line/bot/v1/event/message.rb
66
- - lib/line/bot/v1/event/postback.rb
67
- - lib/line/bot/v1/event/things.rb
68
- - lib/line/bot/v1/event/unfollow.rb
69
- - lib/line/bot/v1/event/unsend.rb
70
- - lib/line/bot/v1/event/video_play_complete.rb
71
- - lib/line/bot/v1/httpclient.rb
72
- - lib/line/bot/v1/util.rb
73
52
  - lib/line/bot/v2/channel_access_token/api/channel_access_token_client.rb
74
53
  - lib/line/bot/v2/channel_access_token/core.rb
75
54
  - lib/line/bot/v2/channel_access_token/model/channel_access_token_key_ids_response.rb
@@ -700,7 +679,7 @@ licenses:
700
679
  metadata:
701
680
  bug_tracker_uri: https://github.com/line/line-bot-sdk-ruby/issues
702
681
  changelog_uri: https://github.com/line/line-bot-sdk-ruby/releases
703
- documentation_uri: https://rubydoc.info/gems/line-bot-api/1.30.0
682
+ documentation_uri: https://rubydoc.info/gems/line-bot-api/2.0.0
704
683
  homepage_uri: https://github.com/line/line-bot-sdk-ruby
705
684
  source_code_uri: https://github.com/line/line-bot-sdk-ruby
706
685
  post_install_message:
@@ -1,23 +0,0 @@
1
- # Copyright 2016 LINE
2
- #
3
- # LINE Corporation licenses this file to you under the Apache License,
4
- # version 2.0 (the "License"); you may not use this file except in compliance
5
- # with the License. You may obtain a copy of the License at:
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
- # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
- # License for the specific language governing permissions and limitations
13
- # under the License.
14
-
15
- module Line
16
- module Bot
17
- module API
18
- # @deprecated
19
- # This is obsolete. (and there is no caller...)
20
- class Error < StandardError; end
21
- end
22
- end
23
- end
@@ -1,23 +0,0 @@
1
- # Copyright 2016 LINE
2
- #
3
- # LINE Corporation licenses this file to you under the Apache License,
4
- # version 2.0 (the "License"); you may not use this file except in compliance
5
- # with the License. You may obtain a copy of the License at:
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
- # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
- # License for the specific language governing permissions and limitations
13
- # under the License.
14
-
15
- module Line
16
- module Bot
17
- module API
18
- # @deprecated
19
- # Use {Line::Bot::VERSION} instead.
20
- VERSION = "1.30.0-deprecated"
21
- end
22
- end
23
- end
@@ -1,33 +0,0 @@
1
- # Copyright 2016 LINE
2
- #
3
- # LINE Corporation licenses this file to you under the Apache License,
4
- # version 2.0 (the "License"); you may not use this file except in compliance
5
- # with the License. You may obtain a copy of the License at:
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
- # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
- # License for the specific language governing permissions and limitations
13
- # under the License.
14
-
15
- require 'line/bot/v1/api/version'
16
-
17
- module Line
18
- module Bot
19
- # @deprecated
20
- # This is obsolete.
21
- module API
22
- DEFAULT_OAUTH_ENDPOINT = "https://api.line.me"
23
- DEFAULT_ENDPOINT = "https://api.line.me/v2"
24
- DEFAULT_BLOB_ENDPOINT = "https://api-data.line.me/v2"
25
- DEFAULT_LIFF_ENDPOINT = "https://api.line.me/liff/v1"
26
-
27
- DEFAULT_HEADERS = {
28
- 'Content-Type' => 'application/json; charset=UTF-8',
29
- 'User-Agent' => "LINE-BotSDK-Ruby/#{VERSION}"
30
- }.freeze
31
- end
32
- end
33
- end