Class: Line::Bot::Client
- Inherits:
-
Object
- Object
- Line::Bot::Client
- Defined in:
- lib/line/bot/client.rb
Instance Attribute Summary collapse
-
#channel_id ⇒ Object
Returns the value of attribute channel_id.
-
#channel_mid ⇒ Object
Returns the value of attribute channel_mid.
-
#channel_secret ⇒ Object
Returns the value of attribute channel_secret.
Instance Method Summary collapse
- #credentials ⇒ Hash
- #credentials? ⇒ Boolean
-
#get(endpoint_path) ⇒ Net::HTTPResponse
Fetches data.
-
#get_message_content(identifer) ⇒ Net::HTTPResponse
Gets message content.
-
#get_message_content_preview(identifer) ⇒ Net::HTTPResponse
Gets preview of message content.
-
#get_user_profile(mids) ⇒ Line::Bot::Response::User::Profile
Gets user profile.
-
#initialize {|_self| ... } ⇒ LINE::Bot::Client
constructor
Initializes a new Bot Client.
-
#multiple_message ⇒ Line::Bot::Builder::MultipleMessage
Creates multiple message to line server and to users.
-
#rich_message ⇒ Line::Bot::Builder::RichMessage
Creates rich message to line server and to users.
-
#send_audio(attrs = {}) ⇒ Net::HTTPResponse
Sends audio to users.
-
#send_image(attrs = {}) ⇒ Net::HTTPResponse
Sends image to users.
-
#send_location(attrs = {}) ⇒ Net::HTTPResponse
Sends location to users.
-
#send_message(to_mid, message) ⇒ Net::HTTPResponse
Sends message to line server and to users.
-
#send_sticker(attrs = {}) ⇒ Net::HTTPResponse
Sends sticker to users.
-
#send_text(attrs = {}) ⇒ Net::HTTPResponse
Sends text to users.
-
#send_video(attrs = {}) ⇒ Net::HTTPResponse
Sends video to users.
-
#validate_signature(content = "", channel_signature) ⇒ Boolean
Validates signature.
Constructor Details
#initialize {|_self| ... } ⇒ LINE::Bot::Client
Initializes a new Bot Client
18 19 20 |
# File 'lib/line/bot/client.rb', line 18 def initialize yield(self) if block_given? end |
Instance Attribute Details
#channel_id ⇒ Object
Returns the value of attribute channel_id.
12 13 14 |
# File 'lib/line/bot/client.rb', line 12 def channel_id @channel_id end |
#channel_mid ⇒ Object
Returns the value of attribute channel_mid.
12 13 14 |
# File 'lib/line/bot/client.rb', line 12 def channel_mid @channel_mid end |
#channel_secret ⇒ Object
Returns the value of attribute channel_secret.
12 13 14 |
# File 'lib/line/bot/client.rb', line 12 def channel_secret @channel_secret end |
Instance Method Details
#credentials ⇒ Hash
23 24 25 26 27 28 29 |
# File 'lib/line/bot/client.rb', line 23 def credentials { 'X-Line-ChannelID' => channel_id, 'X-Line-ChannelSecret' => channel_secret, 'X-Line-Trusted-User-With-ACL' => channel_mid, } end |
#credentials? ⇒ Boolean
31 32 33 |
# File 'lib/line/bot/client.rb', line 31 def credentials? credentials.values.all? end |
#get(endpoint_path) ⇒ Net::HTTPResponse
Fetches data
207 208 209 210 211 212 213 214 215 216 |
# File 'lib/line/bot/client.rb', line 207 def get(endpoint_path) raise Line::Bot::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials? request = Request.new do |config| config.endpoint_path = endpoint_path config.credentials = credentials end request.get end |
#get_message_content(identifer) ⇒ Net::HTTPResponse
Gets message content
168 169 170 171 |
# File 'lib/line/bot/client.rb', line 168 def (identifer) endpoint_path = "/v1/bot/message/#{identifer}/content" get(endpoint_path) end |
#get_message_content_preview(identifer) ⇒ Net::HTTPResponse
Gets preview of message content
180 181 182 183 |
# File 'lib/line/bot/client.rb', line 180 def (identifer) endpoint_path = "/v1/bot/message/#{identifer}/content/preview" get(endpoint_path) end |
#get_user_profile(mids) ⇒ Line::Bot::Response::User::Profile
Gets user profile
193 194 195 196 197 198 199 200 |
# File 'lib/line/bot/client.rb', line 193 def get_user_profile(mids) mids = mids.instance_of?(String) ? [mids] : mids endpoint_path = "/v1/profiles?mids=#{mids.join(',')}" response = get(endpoint_path) Line::Bot::Response::User::Profile.new(response) if !response.value end |
#multiple_message ⇒ Line::Bot::Builder::MultipleMessage
Creates multiple message to line server and to users
228 229 230 |
# File 'lib/line/bot/client.rb', line 228 def Line::Bot::Builder::MultipleMessage.new(self) end |
#rich_message ⇒ Line::Bot::Builder::RichMessage
Creates rich message to line server and to users
221 222 223 |
# File 'lib/line/bot/client.rb', line 221 def Line::Bot::Builder::RichMessage.new(self) end |
#send_audio(attrs = {}) ⇒ Net::HTTPResponse
Sends audio to users
93 94 95 96 97 98 99 |
# File 'lib/line/bot/client.rb', line 93 def send_audio(attrs = {}) = Message::Audio.new( audio_url: attrs[:audio_url], duration: attrs[:duration], ) (attrs[:to_mid], ) end |
#send_image(attrs = {}) ⇒ Net::HTTPResponse
Sends image to users
59 60 61 62 63 64 65 |
# File 'lib/line/bot/client.rb', line 59 def send_image(attrs = {}) = Message::Image.new( image_url: attrs[:image_url], preview_url: attrs[:preview_url], ) (attrs[:to_mid], ) end |
#send_location(attrs = {}) ⇒ Net::HTTPResponse
Sends location to users
112 113 114 115 116 117 118 119 120 |
# File 'lib/line/bot/client.rb', line 112 def send_location(attrs = {}) = Message::Location.new( title: attrs[:title], address: attrs[:address], latitude: attrs[:latitude], longitude: attrs[:longitude], ) (attrs[:to_mid], ) end |
#send_message(to_mid, message) ⇒ Net::HTTPResponse
Sends message to line server and to users
148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/line/bot/client.rb', line 148 def (to_mid, ) raise Line::Bot::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials? request = Request.new do |config| config.endpoint_path = '/v1/events' config.credentials = credentials config.to_mid = to_mid config. = end request.post end |
#send_sticker(attrs = {}) ⇒ Net::HTTPResponse
Sends sticker to users
132 133 134 135 136 137 138 139 |
# File 'lib/line/bot/client.rb', line 132 def send_sticker(attrs = {}) = Message::Sticker.new( stkpkgid: attrs[:stkpkgid], stkid: attrs[:stkid], stkver: attrs[:stkver], ) (attrs[:to_mid], ) end |
#send_text(attrs = {}) ⇒ Net::HTTPResponse
Sends text to users
43 44 45 46 47 48 |
# File 'lib/line/bot/client.rb', line 43 def send_text(attrs = {}) = Message::Text.new( text: attrs[:text], ) (attrs[:to_mid], ) end |
#send_video(attrs = {}) ⇒ Net::HTTPResponse
Sends video to users
76 77 78 79 80 81 82 |
# File 'lib/line/bot/client.rb', line 76 def send_video(attrs = {}) = Message::Video.new( video_url: attrs[:video_url], preview_url: attrs[:preview_url], ) (attrs[:to_mid], ) end |
#validate_signature(content = "", channel_signature) ⇒ Boolean
Validates signature
238 239 240 241 242 243 244 245 |
# File 'lib/line/bot/client.rb', line 238 def validate_signature(content = "", channel_signature) return false unless !channel_signature.nil? && credentials? hash = OpenSSL::HMAC::digest(OpenSSL::Digest::SHA256.new, channel_secret, content) signature = Base64.strict_encode64(hash) channel_signature == signature end |