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:



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

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

Instance Attribute Details

#channel_secretObject

@return [String]



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

def channel_secret
  @channel_secret
end

#channel_tokenObject

@return [String]



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

def channel_token
  @channel_token
end

#endpointObject

@return [String]



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

def endpoint
  @endpoint
end

#http_optionsHash

Returns:

  • (Hash)


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

def http_options
  @http_options
end

#httpclientObject

Returns:

  • (Object)


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

def httpclient
  @httpclient
end

Instance Method Details

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)


372
373
374
# File 'lib/line/bot/client.rb', line 372

def bulk_link_rich_menus(user_ids, rich_menu_id)
  post("/bot/richmenu/bulk/link", {richMenuId: rich_menu_id, userIds: user_ids}.to_json)
end

To unlink a rich menu from multiple users at a time

Parameters:

  • user_ids (Array)

    ID of the user

Returns:

  • (Net::HTTPResponse)


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

def bulk_unlink_rich_menus(user_ids)
  post("/bot/richmenu/bulk/unlink", {userIds: user_ids}.to_json)
end

Issue a link token to a user

Parameters:

  • user_id (String)

    ID of the user

Returns:

  • (Net::HTTPResponse)


418
419
420
421
# File 'lib/line/bot/client.rb', line 418

def create_link_token(user_id)
  endpoint_path = "/bot/user/#{user_id}/linkToken"
  post(endpoint_path)
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)


281
282
283
284
285
286
287
288
289
290
291
# File 'lib/line/bot/client.rb', line 281

def create_rich_menu(rich_menu)
  request = Request.new do |config|
    config.httpclient     = httpclient
    config.endpoint       = endpoint
    config.endpoint_path  = '/bot/richmenu'
    config.credentials    = credentials
    config.payload        = rich_menu.to_json
  end

  request.post
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)


401
402
403
404
405
406
407
408
409
410
411
# File 'lib/line/bot/client.rb', line 401

def create_rich_menu_image(rich_menu_id, file)
  request = Request.new do |config|
    config.httpclient     = httpclient
    config.endpoint       = endpoint
    config.endpoint_path  = "/bot/richmenu/#{rich_menu_id}/content"
    config.credentials = credentials
    config.file = file
  end

  request.post
end

#credentialsHash

Returns:

  • (Hash)


54
55
56
57
58
# File 'lib/line/bot/client.rb', line 54

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

#credentials?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/line/bot/client.rb', line 60

def credentials?
  credentials.values.all?
end

#delete(endpoint_path) ⇒ Net::HTTPResponse

Delete content of specified URL.

Parameters:

  • endpoint_path (String)

Returns:

  • (Net::HTTPResponse)

Raises:



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

def delete(endpoint_path)
  raise Line::Bot::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials?

  request = Request.new do |config|
    config.httpclient     = httpclient
    config.endpoint       = endpoint
    config.endpoint_path  = endpoint_path
    config.credentials    = credentials
  end

  request.delete
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)


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

def delete_rich_menu(rich_menu_id)
  endpoint_path = "/bot/richmenu/#{rich_menu_id}"
  delete(endpoint_path)
end

#get(endpoint_path) ⇒ Net::HTTPResponse

Fetch data, get content of specified URL.

Parameters:

  • endpoint_path (String)

Returns:

  • (Net::HTTPResponse)

Raises:



428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/line/bot/client.rb', line 428

def get(endpoint_path)
  raise Line::Bot::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials?

  request = Request.new do |config|
    config.httpclient     = httpclient
    config.endpoint       = endpoint
    config.endpoint_path  = endpoint_path
    config.credentials    = credentials
  end

  request.get
end

#get_default_rich_menuNet::HTTPResponse

Get default rich menu

Returns:

  • (Net::HTTPResponse)


316
317
318
319
# File 'lib/line/bot/client.rb', line 316

def get_default_rich_menu
  endpoint_path = '/bot/user/all/richmenu'
  get(endpoint_path)
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)


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

def get_group_member_ids(group_id, continuation_token = nil)
  endpoint_path  = "/bot/group/#{group_id}/members/ids"
  endpoint_path += "?start=#{continuation_token}" if continuation_token
  get(endpoint_path)
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)


186
187
188
189
# File 'lib/line/bot/client.rb', line 186

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

#get_message_content(identifier) ⇒ Net::HTTPResponse

Get message content.

Parameters:

  • identifier (String)

    Message’s identifier

Returns:

  • (Net::HTTPResponse)


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

def get_message_content(identifier)
  endpoint_path  = "/bot/message/#{identifier}/content"
  get(endpoint_path)
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)


271
272
273
274
# File 'lib/line/bot/client.rb', line 271

def get_message_delivery_multicast(date)
  endpoint_path = "/bot/message/delivery/multicast?date=#{date}"
  get(endpoint_path)
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)


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

def get_message_delivery_push(date)
  endpoint_path = "/bot/message/delivery/push?date=#{date}"
  get(endpoint_path)
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)


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

def get_message_delivery_reply(date)
  endpoint_path = "/bot/message/delivery/reply?date=#{date}"
  get(endpoint_path)
end

#get_profile(user_id) ⇒ Net::HTTPResponse

Get an user’s profile.

Parameters:

  • user_id (String)

    User’s identifier

Returns:

  • (Net::HTTPResponse)


175
176
177
178
# File 'lib/line/bot/client.rb', line 175

def get_profile(user_id)
  endpoint_path  = "/bot/profile/#{user_id}"
  get(endpoint_path)
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)


241
242
243
244
# File 'lib/line/bot/client.rb', line 241

def get_rich_menu(rich_menu_id)
  endpoint_path = "/bot/richmenu/#{rich_menu_id}"
  get(endpoint_path)
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)


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

def get_rich_menu_image(rich_menu_id)
  endpoint_path = "/bot/richmenu/#{rich_menu_id}/content"
  get(endpoint_path)
end

#get_rich_menusNet::HTTPResponse

Get a list of all uploaded rich menus

Returns:

  • (Net::HTTPResponse)


231
232
233
234
# File 'lib/line/bot/client.rb', line 231

def get_rich_menus
  endpoint_path = '/bot/richmenu/list'
  get(endpoint_path)
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)


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

def get_room_member_ids(room_id, continuation_token = nil)
  endpoint_path  = "/bot/room/#{room_id}/members/ids"
  endpoint_path += "?start=#{continuation_token}" if continuation_token
  get(endpoint_path)
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)


197
198
199
200
# File 'lib/line/bot/client.rb', line 197

def get_room_member_profile(room_id, user_id)
  endpoint_path = "/bot/room/#{room_id}/member/#{user_id}"
  get(endpoint_path)
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)


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

def get_user_rich_menu(user_id)
  endpoint_path = "/bot/user/#{user_id}/richmenu"
  get(endpoint_path)
end

#leave_group(group_id) ⇒ Object



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

def leave_group(group_id)
  raise Line::Bot::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials?

  request = Request.new do |config|
    config.httpclient     = httpclient
    config.endpoint       = endpoint
    config.endpoint_path  = "/bot/group/#{group_id}/leave"
    config.credentials    = credentials
  end

  request.post
end

#leave_room(room_id) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/line/bot/client.rb', line 147

def leave_room(room_id)
  raise Line::Bot::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials?

  request = Request.new do |config|
    config.httpclient     = httpclient
    config.endpoint       = endpoint
    config.endpoint_path  = "/bot/room/#{room_id}/leave"
    config.credentials    = credentials
  end

  request.post
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)


345
346
347
348
349
350
351
352
353
354
# File 'lib/line/bot/client.rb', line 345

def link_user_rich_menu(user_id, rich_menu_id)
  request = Request.new do |config|
    config.httpclient = httpclient
    config.endpoint = endpoint
    config.endpoint_path = "/bot/user/#{user_id}/richmenu/#{rich_menu_id}"
    config.credentials = credentials
  end

  request.post
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)

Raises:



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/line/bot/client.rb', line 116

def multicast(to, messages)
  raise Line::Bot::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials?

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

  request = Request.new do |config|
    config.httpclient     = httpclient
    config.endpoint       = endpoint
    config.endpoint_path  = '/bot/message/multicast'
    config.credentials    = credentials
    config.to             = to
    config.messages       = messages
  end

  request.post
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>)


483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/line/bot/client.rb', line 483

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

  json['events'].map { |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

#post(endpoint_path, payload = nil) ⇒ Net::HTTPResponse

Post data, get content of specified URL.

Parameters:

  • endpoint_path (String)

Returns:

  • (Net::HTTPResponse)

Raises:



446
447
448
449
450
451
452
453
454
455
456
457
458
# File 'lib/line/bot/client.rb', line 446

def post(endpoint_path, payload = nil)
  raise Line::Bot::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials?

  request = Request.new do |config|
    config.httpclient     = httpclient
    config.endpoint       = endpoint
    config.endpoint_path  = endpoint_path
    config.credentials    = credentials
    config.payload        = payload if payload
  end

  request.post
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)

Raises:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/line/bot/client.rb', line 70

def push_message(user_id, messages)
  raise Line::Bot::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials?

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

  request = Request.new do |config|
    config.httpclient     = httpclient
    config.endpoint       = endpoint
    config.endpoint_path  = '/bot/message/push'
    config.credentials    = credentials
    config.to             = user_id
    config.messages       = messages
  end

  request.post
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)

Raises:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/line/bot/client.rb', line 93

def reply_message(token, messages)
  raise Line::Bot::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials?

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

  request = Request.new do |config|
    config.httpclient     = httpclient
    config.endpoint       = endpoint
    config.endpoint_path  = '/bot/message/reply'
    config.credentials    = credentials
    config.reply_token    = token
    config.messages       = messages
  end

  request.post
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)


326
327
328
329
# File 'lib/line/bot/client.rb', line 326

def set_default_rich_menu(rich_menu_id)
  endpoint_path = "/bot/user/all/richmenu/#{rich_menu_id}"
  post(endpoint_path)
end

Unlink a rich menu from a user

Parameters:

  • user_id (String)

    ID of the user

Returns:

  • (Net::HTTPResponse)


361
362
363
364
# File 'lib/line/bot/client.rb', line 361

def unlink_user_rich_menu(user_id)
  endpoint_path  = "/bot/user/#{user_id}/richmenu"
  delete(endpoint_path)
end

#unset_default_rich_menuNet::HTTPResponse

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

Returns:

  • (Net::HTTPResponse)


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

def unset_default_rich_menu
  endpoint_path = "/bot/user/all/richmenu"
  delete(endpoint_path)
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)


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

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