Class: Line::Bot::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/line/bot/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Line::Bot::Client

Initialize a new Bot Client.

Parameters:

  • options (Hash) (defaults to: {})

Yields:

  • (_self)

Yield Parameters:



37
38
39
40
41
42
# File 'lib/line/bot/client.rb', line 37

def initialize(options = {})
  options.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
  yield(self) if block_given?
end

Instance Attribute Details

#blob_endpointObject

@return [String]



24
25
26
# File 'lib/line/bot/client.rb', line 24

def blob_endpoint
  @blob_endpoint
end

#channel_idObject

@return [String]



24
25
26
# File 'lib/line/bot/client.rb', line 24

def channel_id
  @channel_id
end

#channel_secretObject

@return [String]



24
25
26
# File 'lib/line/bot/client.rb', line 24

def channel_secret
  @channel_secret
end

#channel_tokenObject

@return [String]



24
25
26
# File 'lib/line/bot/client.rb', line 24

def channel_token
  @channel_token
end

#endpointObject

@return [String]



24
25
26
# File 'lib/line/bot/client.rb', line 24

def endpoint
  @endpoint
end

#http_optionsHash

Returns:

  • (Hash)


30
31
32
# File 'lib/line/bot/client.rb', line 30

def http_options
  @http_options
end

#httpclientObject

Returns:

  • (Object)


27
28
29
# File 'lib/line/bot/client.rb', line 27

def httpclient
  @httpclient
end

Instance Method Details

#broadcast(messages) ⇒ Net::HTTPResponse

Broadcast messages to users

Parameters:

  • messages (Hash or Array)

Returns:

  • (Net::HTTPResponse)


153
154
155
156
157
158
159
160
161
# File 'lib/line/bot/client.rb', line 153

def broadcast(messages)
  channel_token_required

  messages = [messages] if messages.is_a?(Hash)

  endpoint_path = '/bot/message/broadcast'
  payload = { messages: messages }.to_json
  post(endpoint, endpoint_path, payload, credentials)
end

To link a rich menu to multiple users at a time

Parameters:

  • user_ids (Array)

    ID of the user

  • rich_menu_id (String)

    ID of the uploaded rich menu

Returns:

  • (Net::HTTPResponse)


426
427
428
429
430
431
# File 'lib/line/bot/client.rb', line 426

def bulk_link_rich_menus(user_ids, rich_menu_id)
  channel_token_required

  endpoint_path = "/bot/richmenu/bulk/link"
  post(endpoint, endpoint_path, { richMenuId: rich_menu_id, userIds: user_ids }.to_json, credentials)
end

To unlink a rich menu from multiple users at a time

Parameters:

  • user_ids (Array)

    ID of the user

Returns:

  • (Net::HTTPResponse)


438
439
440
441
442
443
# File 'lib/line/bot/client.rb', line 438

def bulk_unlink_rich_menus(user_ids)
  channel_token_required

  endpoint_path = "/bot/richmenu/bulk/unlink"
  post(endpoint, endpoint_path, { userIds: user_ids }.to_json, credentials)
end

Issue a link token to a user

Parameters:

  • user_id (String)

    ID of the user

Returns:

  • (Net::HTTPResponse)


483
484
485
486
487
488
# File 'lib/line/bot/client.rb', line 483

def create_link_token(user_id)
  channel_token_required

  endpoint_path = "/bot/user/#{user_id}/linkToken"
  post(endpoint, endpoint_path, nil, credentials)
end

#create_rich_menu(rich_menu) ⇒ Net::HTTPResponse

Create a rich menu

Parameters:

  • rich_menu (Hash)

    The rich menu represented as a rich menu object

Returns:

  • (Net::HTTPResponse)


332
333
334
335
336
337
# File 'lib/line/bot/client.rb', line 332

def create_rich_menu(rich_menu)
  channel_token_required

  endpoint_path = '/bot/richmenu'
  post(endpoint, endpoint_path, rich_menu.to_json, credentials)
end

#create_rich_menu_image(rich_menu_id, file) ⇒ Net::HTTPResponse

Upload and attaches an image to a rich menu

Parameters:

  • rich_menu_id (String)

    The ID of the rich menu to attach the image to

  • file (File)

    Image file to attach rich menu

Returns:

  • (Net::HTTPResponse)


463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/line/bot/client.rb', line 463

def create_rich_menu_image(rich_menu_id, file)
  channel_token_required

  content_type = case file.path
                 when /\.jpe?g\z/i then 'image/jpeg'
                 when /\.png\z/i   then 'image/png'
                 else
                   raise ArgumentError, "invalid file extension: #{file.path}"
                 end

  endpoint_path = "/bot/richmenu/#{rich_menu_id}/content"
  headers = credentials.merge('Content-Type' => content_type)
  post(blob_endpoint, endpoint_path, file.rewind && file.read, headers)
end

#credentialsHash

Returns:

  • (Hash)


64
65
66
67
68
# File 'lib/line/bot/client.rb', line 64

def credentials
  {
    "Authorization" => "Bearer #{channel_token}",
  }
end

#delete(endpoint_base, endpoint_path, headers = {}) ⇒ Net::HTTPResponse

Delete content of specified URL.

Parameters:

  • endpoint_base (String)
  • endpoint_path (String)
  • headers (Hash) (defaults to: {})

Returns:

  • (Net::HTTPResponse)


576
577
578
579
# File 'lib/line/bot/client.rb', line 576

def delete(endpoint_base, endpoint_path, headers = {})
  headers = Line::Bot::API::DEFAULT_HEADERS.merge(headers)
  httpclient.delete(endpoint_base + endpoint_path, headers)
end

#delete_rich_menu(rich_menu_id) ⇒ Net::HTTPResponse

Delete a rich menu

Parameters:

  • rich_menu_id (String)

    ID of an uploaded rich menu

Returns:

  • (Net::HTTPResponse)


344
345
346
347
348
349
# File 'lib/line/bot/client.rb', line 344

def delete_rich_menu(rich_menu_id)
  channel_token_required

  endpoint_path = "/bot/richmenu/#{rich_menu_id}"
  delete(endpoint, endpoint_path, credentials)
end

#get(endpoint_base, endpoint_path, headers = {}) ⇒ Net::HTTPResponse

Fetch data, get content of specified URL.

Parameters:

  • endpoint_base (String)
  • endpoint_path (String)
  • headers (Hash) (defaults to: {})

Returns:

  • (Net::HTTPResponse)


551
552
553
554
# File 'lib/line/bot/client.rb', line 551

def get(endpoint_base, endpoint_path, headers = {})
  headers = Line::Bot::API::DEFAULT_HEADERS.merge(headers)
  httpclient.get(endpoint_base + endpoint_path, headers)
end

#get_default_rich_menuNet::HTTPResponse

Get default rich menu

Returns:

  • (Net::HTTPResponse)


366
367
368
369
370
371
# File 'lib/line/bot/client.rb', line 366

def get_default_rich_menu
  channel_token_required

  endpoint_path = '/bot/user/all/richmenu'
  get(endpoint, endpoint_path, credentials)
end

#get_friend_demographicsNet::HTTPResponse

Retrieves the demographic attributes for a bot’s friends.

Returns:

  • (Net::HTTPResponse)


537
538
539
540
541
542
# File 'lib/line/bot/client.rb', line 537

def get_friend_demographics
  channel_token_required

  endpoint_path = '/bot/insight/demographic'
  get(endpoint, endpoint_path, credentials)
end

#get_group_member_ids(group_id, continuation_token = nil) ⇒ Net::HTTPResponse

Get user IDs of a group

Parameters:

  • group_id (String)

    Group’s identifier

  • continuation_token (String) (defaults to: nil)

    Identifier to return next page (next property to be included in the response)

Returns:

  • (Net::HTTPResponse)


234
235
236
237
238
239
240
# File 'lib/line/bot/client.rb', line 234

def get_group_member_ids(group_id, continuation_token = nil)
  channel_token_required

  endpoint_path = "/bot/group/#{group_id}/members/ids"
  endpoint_path += "?start=#{continuation_token}" if continuation_token
  get(endpoint, endpoint_path, credentials)
end

#get_group_member_profile(group_id, user_id) ⇒ Net::HTTPResponse

Get an user’s profile of a group.

Parameters:

  • group_id (String)

    Group’s identifier

  • user_id (String)

    User’s identifier

Returns:

  • (Net::HTTPResponse)


207
208
209
210
211
212
# File 'lib/line/bot/client.rb', line 207

def get_group_member_profile(group_id, user_id)
  channel_token_required

  endpoint_path = "/bot/group/#{group_id}/member/#{user_id}"
  get(endpoint, endpoint_path, credentials)
end

#get_message_content(identifier) ⇒ Net::HTTPResponse

Get message content.

Parameters:

  • identifier (String)

    Message’s identifier

Returns:

  • (Net::HTTPResponse)


182
183
184
185
186
187
# File 'lib/line/bot/client.rb', line 182

def get_message_content(identifier)
  channel_token_required

  endpoint_path = "/bot/message/#{identifier}/content"
  get(blob_endpoint, endpoint_path, credentials)
end

#get_message_delivery_broadcast(date) ⇒ Net::HTTPResponse

Gets the number of messages sent with the /bot/message/multicast endpoint.

Parameters:

  • date (String)

    Date the messages were sent (format: yyyyMMdd)

Returns:

  • (Net::HTTPResponse)


320
321
322
323
324
325
# File 'lib/line/bot/client.rb', line 320

def get_message_delivery_broadcast(date)
  channel_token_required

  endpoint_path = "/bot/message/delivery/broadcast?date=#{date}"
  get(endpoint, endpoint_path, credentials)
end

#get_message_delivery_multicast(date) ⇒ Net::HTTPResponse

Gets the number of messages sent with the /bot/message/multicast endpoint.

Parameters:

  • date (String)

    Date the messages were sent (format: yyyyMMdd)

Returns:

  • (Net::HTTPResponse)


308
309
310
311
312
313
# File 'lib/line/bot/client.rb', line 308

def get_message_delivery_multicast(date)
  channel_token_required

  endpoint_path = "/bot/message/delivery/multicast?date=#{date}"
  get(endpoint, endpoint_path, credentials)
end

#get_message_delivery_push(date) ⇒ Net::HTTPResponse

Gets the number of messages sent with the /bot/message/push endpoint.

Parameters:

  • date (String)

    Date the messages were sent (format: yyyyMMdd)

Returns:

  • (Net::HTTPResponse)


296
297
298
299
300
301
# File 'lib/line/bot/client.rb', line 296

def get_message_delivery_push(date)
  channel_token_required

  endpoint_path = "/bot/message/delivery/push?date=#{date}"
  get(endpoint, endpoint_path, credentials)
end

#get_message_delivery_reply(date) ⇒ Net::HTTPResponse

Gets the number of messages sent with the /bot/message/reply endpoint.

Parameters:

  • date (String)

    Date the messages were sent (format: yyyyMMdd)

Returns:

  • (Net::HTTPResponse)


284
285
286
287
288
289
# File 'lib/line/bot/client.rb', line 284

def get_message_delivery_reply(date)
  channel_token_required

  endpoint_path = "/bot/message/delivery/reply?date=#{date}"
  get(endpoint, endpoint_path, credentials)
end

#get_number_of_followers(date) ⇒ Net::HTTPResponse

Returns the number of followers

Parameters:

  • date (String)

    (Format:yyyyMMdd, Example:20191231)

Returns:

  • (Net::HTTPResponse)


527
528
529
530
531
532
# File 'lib/line/bot/client.rb', line 527

def get_number_of_followers(date)
  channel_token_required

  endpoint_path = "/bot/insight/followers?date=#{date}"
  get(endpoint, endpoint_path, credentials)
end

#get_number_of_message_deliveries(date) ⇒ Net::HTTPResponse

Returns the number of messages sent on a specified day

Parameters:

  • date (String)

    (Format:yyyyMMdd, Example:20191231)

Returns:

  • (Net::HTTPResponse)


515
516
517
518
519
520
# File 'lib/line/bot/client.rb', line 515

def get_number_of_message_deliveries(date)
  channel_token_required

  endpoint_path = "/bot/insight/message/delivery?date=#{date}"
  get(endpoint, endpoint_path, credentials)
end

#get_profile(user_id) ⇒ Net::HTTPResponse

Get an user’s profile.

Parameters:

  • user_id (String)

    User’s identifier

Returns:

  • (Net::HTTPResponse)


194
195
196
197
198
199
# File 'lib/line/bot/client.rb', line 194

def get_profile(user_id)
  channel_token_required

  endpoint_path = "/bot/profile/#{user_id}"
  get(endpoint, endpoint_path, credentials)
end

#get_quotaNet::HTTPResponse

Get the target limit for additional messages

Returns:

  • (Net::HTTPResponse)


493
494
495
496
497
498
# File 'lib/line/bot/client.rb', line 493

def get_quota
  channel_token_required

  endpoint_path = "/bot/message/quota"
  get(endpoint, endpoint_path, credentials)
end

#get_quota_consumptionNet::HTTPResponse

Get number of messages sent this month

Returns:

  • (Net::HTTPResponse)


503
504
505
506
507
508
# File 'lib/line/bot/client.rb', line 503

def get_quota_consumption
  channel_token_required

  endpoint_path = "/bot/message/quota/consumption"
  get(endpoint, endpoint_path, credentials)
end

#get_rich_menu(rich_menu_id) ⇒ Net::HTTPResponse

Get a rich menu via a rich menu ID

Parameters:

  • rich_menu_id (String)

    ID of an uploaded rich menu

Returns:

  • (Net::HTTPResponse)


272
273
274
275
276
277
# File 'lib/line/bot/client.rb', line 272

def get_rich_menu(rich_menu_id)
  channel_token_required

  endpoint_path = "/bot/richmenu/#{rich_menu_id}"
  get(endpoint, endpoint_path, credentials)
end

#get_rich_menu_image(rich_menu_id) ⇒ Net::HTTPResponse

Download an image associated with a rich menu

Parameters:

  • rich_menu_id (String)

    ID of an uploaded rich menu

Returns:

  • (Net::HTTPResponse)


450
451
452
453
454
455
# File 'lib/line/bot/client.rb', line 450

def get_rich_menu_image(rich_menu_id)
  channel_token_required

  endpoint_path = "/bot/richmenu/#{rich_menu_id}/content"
  get(blob_endpoint, endpoint_path, credentials)
end

#get_rich_menusNet::HTTPResponse

Get a list of all uploaded rich menus

Returns:

  • (Net::HTTPResponse)


260
261
262
263
264
265
# File 'lib/line/bot/client.rb', line 260

def get_rich_menus
  channel_token_required

  endpoint_path = '/bot/richmenu/list'
  get(endpoint, endpoint_path, credentials)
end

#get_room_member_ids(room_id, continuation_token = nil) ⇒ Net::HTTPResponse

Get user IDs of a room

Parameters:

  • group_id (String)

    Room’s identifier

  • continuation_token (String) (defaults to: nil)

    Identifier to return next page (next property to be included in the response)

Returns:

  • (Net::HTTPResponse)


249
250
251
252
253
254
255
# File 'lib/line/bot/client.rb', line 249

def get_room_member_ids(room_id, continuation_token = nil)
  channel_token_required

  endpoint_path = "/bot/room/#{room_id}/members/ids"
  endpoint_path += "?start=#{continuation_token}" if continuation_token
  get(endpoint, endpoint_path, credentials)
end

#get_room_member_profile(room_id, user_id) ⇒ Net::HTTPResponse

Get an user’s profile of a room.

Parameters:

  • room_id (String)

    Room’s identifier

  • user_id (String)

    User’s identifier

Returns:

  • (Net::HTTPResponse)


220
221
222
223
224
225
# File 'lib/line/bot/client.rb', line 220

def get_room_member_profile(room_id, user_id)
  channel_token_required

  endpoint_path = "/bot/room/#{room_id}/member/#{user_id}"
  get(endpoint, endpoint_path, credentials)
end

#get_user_rich_menu(user_id) ⇒ Net::HTTPResponse

Get the ID of the rich menu linked to a user

Parameters:

  • user_id (String)

    ID of the user

Returns:

  • (Net::HTTPResponse)


356
357
358
359
360
361
# File 'lib/line/bot/client.rb', line 356

def get_user_rich_menu(user_id)
  channel_token_required

  endpoint_path = "/bot/user/#{user_id}/richmenu"
  get(endpoint, endpoint_path, credentials)
end

#issue_channel_token(grant_type = 'client_credentials') ⇒ Net::HTTPResponse

Issue channel access token

Parameters:

  • grant_type (String) (defaults to: 'client_credentials')

    Grant type

Returns:

  • (Net::HTTPResponse)


75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/line/bot/client.rb', line 75

def issue_channel_token(grant_type = 'client_credentials')
  channel_id_required
  channel_secret_required

  endpoint_path = '/oauth/accessToken'
  payload = URI.encode_www_form(
    grant_type:    grant_type,
    client_id:     channel_id,
    client_secret: channel_secret
  )
  headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
  post(endpoint, endpoint_path, payload, headers)
end

#leave_group(group_id) ⇒ Object



163
164
165
166
167
168
# File 'lib/line/bot/client.rb', line 163

def leave_group(group_id)
  channel_token_required

  endpoint_path = "/bot/group/#{group_id}/leave"
  post(endpoint, endpoint_path, nil, credentials)
end

#leave_room(room_id) ⇒ Object



170
171
172
173
174
175
# File 'lib/line/bot/client.rb', line 170

def leave_room(room_id)
  channel_token_required

  endpoint_path = "/bot/room/#{room_id}/leave"
  post(endpoint, endpoint_path, nil, credentials)
end

Link a rich menu to a user

Parameters:

  • user_id (String)

    ID of the user

  • rich_menu_id (String)

    ID of an uploaded rich menu

Returns:

  • (Net::HTTPResponse)


401
402
403
404
405
406
# File 'lib/line/bot/client.rb', line 401

def link_user_rich_menu(user_id, rich_menu_id)
  channel_token_required

  endpoint_path = "/bot/user/#{user_id}/richmenu/#{rich_menu_id}"
  post(endpoint, endpoint_path, nil, credentials)
end

#multicast(to, messages) ⇒ Net::HTTPResponse

Multicast messages to line server and to users.

Parameters:

  • to (Array or String)
  • messages (Hash or Array)

Returns:

  • (Net::HTTPResponse)


137
138
139
140
141
142
143
144
145
146
# File 'lib/line/bot/client.rb', line 137

def multicast(to, messages)
  channel_token_required

  to = [to] if to.is_a?(String)
  messages = [messages] if messages.is_a?(Hash)

  endpoint_path = '/bot/message/multicast'
  payload = { to: to, messages: messages }.to_json
  post(endpoint, endpoint_path, payload, credentials)
end

#parse_events_from(request_body) ⇒ Array<Line::Bot::Event::Class>

Parse events from request.body

Parameters:

  • request_body (String)

Returns:

  • (Array<Line::Bot::Event::Class>)


586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/line/bot/client.rb', line 586

def parse_events_from(request_body)
  json = JSON.parse(request_body)

  json['events'].map do |item|
    begin
      klass = Line::Bot::Event.const_get(Line::Bot::Util.camelize(item['type']))
      klass.new(item)
    rescue NameError
      Line::Bot::Event::Base.new(item)
    end
  end
end

#post(endpoint_base, endpoint_path, payload = nil, headers = {}) ⇒ Net::HTTPResponse

Post data, get content of specified URL.

Parameters:

  • endpoint_base (String)
  • endpoint_path (String)
  • payload (String or NilClass) (defaults to: nil)
  • headers (Hash) (defaults to: {})

Returns:

  • (Net::HTTPResponse)


564
565
566
567
# File 'lib/line/bot/client.rb', line 564

def post(endpoint_base, endpoint_path, payload = nil, headers = {})
  headers = Line::Bot::API::DEFAULT_HEADERS.merge(headers)
  httpclient.post(endpoint_base + endpoint_path, payload, headers)
end

#push_message(user_id, messages) ⇒ Net::HTTPResponse

Push messages to line server and to user.

Parameters:

  • user_id (String)

    User’s identifiers

  • messages (Hash or Array)

Returns:

  • (Net::HTTPResponse)


105
106
107
108
109
110
111
112
113
# File 'lib/line/bot/client.rb', line 105

def push_message(user_id, messages)
  channel_token_required

  messages = [messages] if messages.is_a?(Hash)

  endpoint_path = '/bot/message/push'
  payload = { to: user_id, messages: messages }.to_json
  post(endpoint, endpoint_path, payload, credentials)
end

#reply_message(token, messages) ⇒ Net::HTTPResponse

Reply messages to line server and to users.

Parameters:

  • token (String)
  • messages (Hash or Array)

Returns:

  • (Net::HTTPResponse)


121
122
123
124
125
126
127
128
129
# File 'lib/line/bot/client.rb', line 121

def reply_message(token, messages)
  channel_token_required

  messages = [messages] if messages.is_a?(Hash)

  endpoint_path = '/bot/message/reply'
  payload = { replyToken: token, messages: messages }.to_json
  post(endpoint, endpoint_path, payload, credentials)
end

#revoke_channel_token(access_token) ⇒ Net::HTTPResponse

Revoke channel access token

Returns:

  • (Net::HTTPResponse)


92
93
94
95
96
97
# File 'lib/line/bot/client.rb', line 92

def revoke_channel_token(access_token)
  endpoint_path = '/oauth/revoke'
  payload = URI.encode_www_form(access_token: access_token)
  headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
  post(endpoint, endpoint_path, payload, headers)
end

#set_default_rich_menu(rich_menu_id) ⇒ Net::HTTPResponse

Set default rich menu (Link a rich menu to all user)

Parameters:

  • rich_menu_id (String)

    ID of an uploaded rich menu

Returns:

  • (Net::HTTPResponse)


378
379
380
381
382
383
# File 'lib/line/bot/client.rb', line 378

def set_default_rich_menu(rich_menu_id)
  channel_token_required

  endpoint_path = "/bot/user/all/richmenu/#{rich_menu_id}"
  post(endpoint, endpoint_path, nil, credentials)
end

Unlink a rich menu from a user

Parameters:

  • user_id (String)

    ID of the user

Returns:

  • (Net::HTTPResponse)


413
414
415
416
417
418
# File 'lib/line/bot/client.rb', line 413

def unlink_user_rich_menu(user_id)
  channel_token_required

  endpoint_path = "/bot/user/#{user_id}/richmenu"
  delete(endpoint, endpoint_path, credentials)
end

#unset_default_rich_menuNet::HTTPResponse

Unset default rich menu (Unlink a rich menu from all user)

Returns:

  • (Net::HTTPResponse)


388
389
390
391
392
393
# File 'lib/line/bot/client.rb', line 388

def unset_default_rich_menu
  channel_token_required

  endpoint_path = "/bot/user/all/richmenu"
  delete(endpoint, endpoint_path, credentials)
end

#validate_signature(content, channel_signature) ⇒ Boolean

Validate signature

Parameters:

  • content (String)

    Request’s body

  • channel_signature (String)

    Request’header ‘X-LINE-Signature’ # HTTP_X_LINE_SIGNATURE

Returns:

  • (Boolean)


605
606
607
608
609
610
611
612
# File 'lib/line/bot/client.rb', line 605

def validate_signature(content, channel_signature)
  return false if !channel_signature || !channel_secret

  hash = OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, channel_secret, content)
  signature = Base64.strict_encode64(hash)

  variable_secure_compare(channel_signature, signature)
end