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:



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

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]



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

def channel_secret
  @channel_secret
end

#channel_tokenObject

@return [String]



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

def channel_token
  @channel_token
end

#endpointObject

@return [String]



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

def endpoint
  @endpoint
end

#httpclientObject

Returns:

  • (Object)


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

def httpclient
  @httpclient
end

Instance Method Details

#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)


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

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)


324
325
326
327
328
329
330
331
332
333
334
# File 'lib/line/bot/client.rb', line 324

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)


52
53
54
55
56
# File 'lib/line/bot/client.rb', line 52

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

#credentials?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/line/bot/client.rb', line 58

def credentials?
  credentials.values.all?
end

#delete(endpoint_path) ⇒ Net::HTTPResponse

Delete content of specified URL.

Parameters:

  • endpoint_path (String)

Returns:

  • (Net::HTTPResponse)

Raises:



359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/line/bot/client.rb', line 359

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)


266
267
268
269
# File 'lib/line/bot/client.rb', line 266

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:



341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/line/bot/client.rb', line 341

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_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)


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

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)


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

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)


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

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


173
174
175
176
# File 'lib/line/bot/client.rb', line 173

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)


239
240
241
242
# File 'lib/line/bot/client.rb', line 239

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)


313
314
315
316
# File 'lib/line/bot/client.rb', line 313

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)


229
230
231
232
# File 'lib/line/bot/client.rb', line 229

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)


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

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)


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

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)


276
277
278
279
# File 'lib/line/bot/client.rb', line 276

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

#leave_group(group_id) ⇒ Object



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

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



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

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)


287
288
289
290
291
292
293
294
295
296
# File 'lib/line/bot/client.rb', line 287

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:



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

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>)


377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/line/bot/client.rb', line 377

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

  json['events'].map { |item|
    begin
      klass = Line::Bot::Event.const_get(item['type'].capitalize)
      klass.new(item)
    rescue NameError => e
      Line::Bot::Event::Base.new(item)
    end
  }
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:



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

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:



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

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

Unlink a rich menu from a user

Parameters:

  • user_id (String)

    ID of the user

Returns:

  • (Net::HTTPResponse)


303
304
305
306
# File 'lib/line/bot/client.rb', line 303

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

#validate_signature(content = "", channel_signature) ⇒ Boolean

Validate signature

Parameters:

  • content (String) (defaults to: "")

    Request’s body

  • channel_signature (String)

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

Returns:

  • (Boolean)


396
397
398
399
400
401
402
403
# File 'lib/line/bot/client.rb', line 396

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