Class: Line::Bot::Client
- Inherits:
-
Object
- Object
- Line::Bot::Client
- Defined in:
- lib/line/bot/client.rb
Instance Attribute Summary collapse
-
#channel_secret ⇒ Object
@return [String].
-
#channel_token ⇒ Object
@return [String].
-
#endpoint ⇒ Object
@return [String].
- #httpclient ⇒ Object
Instance Method Summary collapse
-
#create_rich_menu(rich_menu) ⇒ Net::HTTPResponse
Create a rich menu.
-
#create_rich_menu_image(rich_menu_id, file) ⇒ Net::HTTPResponse
Upload and attaches an image to a rich menu.
- #credentials ⇒ Hash
- #credentials? ⇒ Boolean
-
#delete(endpoint_path) ⇒ Net::HTTPResponse
Delete content of specified URL.
-
#delete_rich_menu(rich_menu_id) ⇒ Net::HTTPResponse
Delete a rich menu.
-
#get(endpoint_path) ⇒ Net::HTTPResponse
Fetch data, get content of specified URL.
-
#get_group_member_ids(group_id, continuation_token = nil) ⇒ Net::HTTPResponse
Get user IDs of a group.
-
#get_group_member_profile(group_id, user_id) ⇒ Net::HTTPResponse
Get an user’s profile of a group.
-
#get_message_content(identifier) ⇒ Net::HTTPResponse
Get message content.
-
#get_profile(user_id) ⇒ Net::HTTPResponse
Get an user’s profile.
-
#get_rich_menu(rich_menu_id) ⇒ Net::HTTPResponse
Get a rich menu via a rich menu ID.
-
#get_rich_menu_image(rich_menu_id) ⇒ Net::HTTPResponse
Download an image associated with a rich menu.
-
#get_rich_menus ⇒ Net::HTTPResponse
Get a list of all uploaded rich menus.
-
#get_room_member_ids(room_id, continuation_token = nil) ⇒ Net::HTTPResponse
Get user IDs of a room.
-
#get_room_member_profile(room_id, user_id) ⇒ Net::HTTPResponse
Get an user’s profile of a room.
-
#get_user_rich_menu(user_id) ⇒ Net::HTTPResponse
Get the ID of the rich menu linked to a user.
-
#initialize(options = {}) {|_self| ... } ⇒ Line::Bot::Client
constructor
Initialize a new Bot Client.
- #leave_group(group_id) ⇒ Object
- #leave_room(room_id) ⇒ Object
-
#link_user_rich_menu(user_id, rich_menu_id) ⇒ Net::HTTPResponse
Link a rich menu to a user.
-
#multicast(to, messages) ⇒ Net::HTTPResponse
Multicast messages to line server and to users.
-
#parse_events_from(request_body) ⇒ Array<Line::Bot::Event::Class>
Parse events from request.body.
-
#push_message(user_id, messages) ⇒ Net::HTTPResponse
Push messages to line server and to user.
-
#reply_message(token, messages) ⇒ Net::HTTPResponse
Reply messages to line server and to users.
-
#unlink_user_rich_menu(user_id) ⇒ Net::HTTPResponse
Unlink a rich menu from a user.
-
#validate_signature(content, channel_signature) ⇒ Boolean
Validate signature.
Constructor Details
#initialize(options = {}) {|_self| ... } ⇒ Line::Bot::Client
Initialize a new Bot Client.
35 36 37 38 39 40 |
# File 'lib/line/bot/client.rb', line 35 def initialize( = {}) .each do |key, value| instance_variable_set("@#{key}", value) end yield(self) if block_given? end |
Instance Attribute Details
#channel_secret ⇒ Object
@return [String]
25 26 27 |
# File 'lib/line/bot/client.rb', line 25 def channel_secret @channel_secret end |
#channel_token ⇒ Object
@return [String]
25 26 27 |
# File 'lib/line/bot/client.rb', line 25 def channel_token @channel_token end |
#endpoint ⇒ Object
@return [String]
25 26 27 |
# File 'lib/line/bot/client.rb', line 25 def endpoint @endpoint end |
#httpclient ⇒ Object
28 29 30 |
# File 'lib/line/bot/client.rb', line 28 def httpclient @httpclient end |
Instance Method Details
#create_rich_menu(rich_menu) ⇒ Net::HTTPResponse
Create a rich menu
248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/line/bot/client.rb', line 248 def () request = Request.new do |config| config.httpclient = httpclient config.endpoint = endpoint config.endpoint_path = '/bot/richmenu' config.credentials = credentials config.payload = .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
323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/line/bot/client.rb', line 323 def (, file) request = Request.new do |config| config.httpclient = httpclient config.endpoint = endpoint config.endpoint_path = "/bot/richmenu/#{}/content" config.credentials = credentials config.file = file end request.post end |
#credentials ⇒ Hash
51 52 53 54 55 |
# File 'lib/line/bot/client.rb', line 51 def credentials { "Authorization" => "Bearer #{channel_token}", } end |
#credentials? ⇒ Boolean
57 58 59 |
# File 'lib/line/bot/client.rb', line 57 def credentials? credentials.values.all? end |
#delete(endpoint_path) ⇒ Net::HTTPResponse
Delete content of specified URL.
358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/line/bot/client.rb', line 358 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
265 266 267 268 |
# File 'lib/line/bot/client.rb', line 265 def () endpoint_path = "/bot/richmenu/#{}" delete(endpoint_path) end |
#get(endpoint_path) ⇒ Net::HTTPResponse
Fetch data, get content of specified URL.
340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/line/bot/client.rb', line 340 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
206 207 208 209 210 |
# File 'lib/line/bot/client.rb', line 206 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.
183 184 185 186 |
# File 'lib/line/bot/client.rb', line 183 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.
162 163 164 165 |
# File 'lib/line/bot/client.rb', line 162 def (identifier) endpoint_path = "/bot/message/#{identifier}/content" get(endpoint_path) end |
#get_profile(user_id) ⇒ Net::HTTPResponse
Get an user’s profile.
172 173 174 175 |
# File 'lib/line/bot/client.rb', line 172 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
238 239 240 241 |
# File 'lib/line/bot/client.rb', line 238 def () endpoint_path = "/bot/richmenu/#{}" get(endpoint_path) end |
#get_rich_menu_image(rich_menu_id) ⇒ Net::HTTPResponse
Download an image associated with a rich menu
312 313 314 315 |
# File 'lib/line/bot/client.rb', line 312 def () endpoint_path = "/bot/richmenu/#{}/content" get(endpoint_path) end |
#get_rich_menus ⇒ Net::HTTPResponse
Get a list of all uploaded rich menus
228 229 230 231 |
# File 'lib/line/bot/client.rb', line 228 def 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
219 220 221 222 223 |
# File 'lib/line/bot/client.rb', line 219 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.
194 195 196 197 |
# File 'lib/line/bot/client.rb', line 194 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
275 276 277 278 |
# File 'lib/line/bot/client.rb', line 275 def (user_id) endpoint_path = "/bot/user/#{user_id}/richmenu" get(endpoint_path) end |
#leave_group(group_id) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/line/bot/client.rb', line 131 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
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/line/bot/client.rb', line 144 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_user_rich_menu(user_id, rich_menu_id) ⇒ Net::HTTPResponse
Link a rich menu to a user
286 287 288 289 290 291 292 293 294 295 |
# File 'lib/line/bot/client.rb', line 286 def (user_id, ) request = Request.new do |config| config.httpclient = httpclient config.endpoint = endpoint config.endpoint_path = "/bot/user/#{user_id}/richmenu/#{}" config.credentials = credentials end request.post end |
#multicast(to, messages) ⇒ Net::HTTPResponse
Multicast messages to line server and to users.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/line/bot/client.rb', line 113 def multicast(to, ) raise Line::Bot::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials? to = [to] if to.is_a?(String) = [] if .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. = end request.post end |
#parse_events_from(request_body) ⇒ Array<Line::Bot::Event::Class>
Parse events from request.body
376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/line/bot/client.rb', line 376 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.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/line/bot/client.rb', line 67 def (user_id, ) raise Line::Bot::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials? = [] if .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. = end request.post end |
#reply_message(token, messages) ⇒ Net::HTTPResponse
Reply messages to line server and to users.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/line/bot/client.rb', line 90 def (token, ) raise Line::Bot::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials? = [] if .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. = end request.post end |
#unlink_user_rich_menu(user_id) ⇒ Net::HTTPResponse
Unlink a rich menu from a user
302 303 304 305 |
# File 'lib/line/bot/client.rb', line 302 def (user_id) endpoint_path = "/bot/user/#{user_id}/richmenu" delete(endpoint_path) end |
#validate_signature(content, channel_signature) ⇒ Boolean
Validate signature
395 396 397 398 399 400 401 402 |
# File 'lib/line/bot/client.rb', line 395 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 |