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
- #credentials ⇒ Hash
- #credentials? ⇒ Boolean
-
#get(endpoint_path) ⇒ Net::HTTPResponse
Fetch data, get content of specified URL.
-
#get_message_content(identifier) ⇒ Net::HTTPResponse
Get message content.
-
#get_profile(user_id) ⇒ Net::HTTPResponse
Get an user’s profile.
-
#initialize(options = {}) {|_self| ... } ⇒ Line::Bot::Client
constructor
Initialize a new Bot Client.
- #leave_group(group_id) ⇒ Object
- #leave_room(room_id) ⇒ Object
-
#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 users.
-
#reply_message(token, messages) ⇒ Net::HTTPResponse
Reply messages to line server and to users.
-
#validate_signature(content = "", channel_signature) ⇒ Boolean
Validate signature.
Constructor Details
#initialize(options = {}) {|_self| ... } ⇒ Line::Bot::Client
Initialize a new Bot Client.
36 37 38 39 40 41 |
# File 'lib/line/bot/client.rb', line 36 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]
26 27 28 |
# File 'lib/line/bot/client.rb', line 26 def channel_secret @channel_secret end |
#channel_token ⇒ Object
@return [String]
26 27 28 |
# File 'lib/line/bot/client.rb', line 26 def channel_token @channel_token end |
#endpoint ⇒ Object
@return [String]
26 27 28 |
# File 'lib/line/bot/client.rb', line 26 def endpoint @endpoint end |
#httpclient ⇒ Object
29 30 31 |
# File 'lib/line/bot/client.rb', line 29 def httpclient @httpclient end |
Instance Method Details
#credentials ⇒ Hash
52 53 54 55 56 |
# File 'lib/line/bot/client.rb', line 52 def credentials { "Authorization" => "Bearer #{channel_token}", } end |
#credentials? ⇒ Boolean
58 59 60 |
# File 'lib/line/bot/client.rb', line 58 def credentials? credentials.values.all? end |
#get(endpoint_path) ⇒ Net::HTTPResponse
Fetch data, get content of specified URL.
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/line/bot/client.rb', line 159 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_message_content(identifier) ⇒ Net::HTTPResponse
Get message content.
139 140 141 142 |
# File 'lib/line/bot/client.rb', line 139 def (identifier) endpoint_path = "/message/#{identifier}/content" get(endpoint_path) end |
#get_profile(user_id) ⇒ Net::HTTPResponse
Get an user’s profile.
149 150 151 152 |
# File 'lib/line/bot/client.rb', line 149 def get_profile(user_id) endpoint_path = "/profile/#{user_id}" get(endpoint_path) end |
#leave_group(group_id) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/line/bot/client.rb', line 108 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 = "/group/#{group_id}/leave" config.credentials = credentials end request.post end |
#leave_room(room_id) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/line/bot/client.rb', line 121 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 = "/room/#{room_id}/leave" config.credentials = credentials end request.post end |
#parse_events_from(request_body) ⇒ Array<Line::Bot::Event::Class>
Parse events from request.body
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/line/bot/client.rb', line 177 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 users.
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 (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 = '/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.
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 (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 = '/message/reply' config.credentials = credentials config.reply_token = token config. = end request.post end |
#validate_signature(content = "", channel_signature) ⇒ Boolean
Validate signature
196 197 198 199 200 201 202 203 |
# File 'lib/line/bot/client.rb', line 196 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 |