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 {|_self| ... } ⇒ LINE::Bot::Client

Initializes a new Bot Client

Parameters:

  • options (Hash)

Yields:

  • (_self)

Yield Parameters:



18
19
20
# File 'lib/line/bot/client.rb', line 18

def initialize
  yield(self) if block_given?
end

Instance Attribute Details

#channel_idObject

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_midObject

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_secretObject

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

#credentialsHash

Returns:

  • (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

Returns:

  • (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

Parameters:

  • endpoint_path (String)

Returns:

  • (Net::HTTPResponse)

Raises:



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

Parameters:

  • identifer (String)

    Message’s identifier

Returns:

  • (Net::HTTPResponse)

Raises:

  • (ArgumentError)

    Error raised when supplied argument are missing message.



168
169
170
171
# File 'lib/line/bot/client.rb', line 168

def get_message_content(identifer)
  endpoint_path  = "/v1/bot/message/#{identifer}/content"
  get(endpoint_path)
end

#get_message_content_preview(identifer) ⇒ Net::HTTPResponse

Gets preview of message content

Parameters:

  • identifer (String)

    Message’s identifier

Returns:

  • (Net::HTTPResponse)

Raises:

  • (ArgumentError)

    Error raised when supplied argument are missing message.



180
181
182
183
# File 'lib/line/bot/client.rb', line 180

def get_message_content_preview(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

Parameters:

  • attrs (:to_mid)
    String or Array

    User’s indentifiers

Returns:

Raises:

  • (ArgumentError)

    Error raised when supplied argument are missing message.

  • (HTTPError)


193
194
195
196
197
198
199
200
# File 'lib/line/bot/client.rb', line 193

def (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_messageLine::Bot::Builder::MultipleMessage

Creates multiple message to line server and to users



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

def multiple_message
  Line::Bot::Builder::MultipleMessage.new(self)
end

#rich_messageLine::Bot::Builder::RichMessage

Creates rich message to line server and to users



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

def rich_message
  Line::Bot::Builder::RichMessage.new(self)
end

#send_audio(attrs = {}) ⇒ Net::HTTPResponse

Sends audio to users

Parameters:

  • attrs (Hash) (defaults to: {})
  • attrs (:to_mid) (defaults to: {})
    String or Array

    User’s indentifiers

  • attrs (:audio_url) (defaults to: {})
    String

    Audio file’s url

  • attrs (:duration) (defaults to: {})
    String or Integer

    Voice message’s length, milliseconds

Returns:

  • (Net::HTTPResponse)

Raises:

  • (ArgumentError)

    Error raised when supplied argument are missing :to_mid, :audio_url, :duration keys.



93
94
95
96
97
98
99
# File 'lib/line/bot/client.rb', line 93

def send_audio(attrs = {})
  message = Message::Audio.new(
    audio_url: attrs[:audio_url],
    duration: attrs[:duration],
  )
  send_message(attrs[:to_mid], message)
end

#send_image(attrs = {}) ⇒ Net::HTTPResponse

Sends image to users

Parameters:

  • attrs (Hash) (defaults to: {})
  • attrs (:to_mid) (defaults to: {})
    String or Array

    User’s indentifiers

  • attrs (:image_url) (defaults to: {})
    String

    Image file’s url

  • attrs (:preview_url) (defaults to: {})
    String

    Preview image file’s url

Returns:

  • (Net::HTTPResponse)

Raises:

  • (ArgumentError)

    Error raised when supplied argument are missing :to_mid, :image_url, :preview_url keys.



59
60
61
62
63
64
65
# File 'lib/line/bot/client.rb', line 59

def send_image(attrs = {})
  message = Message::Image.new(
    image_url: attrs[:image_url],
    preview_url: attrs[:preview_url],
  )
  send_message(attrs[:to_mid], message)
end

#send_location(attrs = {}) ⇒ Net::HTTPResponse

Sends location to users

Parameters:

  • attrs (Hash) (defaults to: {})
  • attrs (:to_mid) (defaults to: {})
    String or Array

    User’s indentifiers

  • attrs (:title) (defaults to: {})
    String

    Location’s title

  • attrs (:address) (defaults to: {})
    String

    Location’s address

  • attrs (:latitude) (defaults to: {})
    Float

    Location’s latitude

  • attrs (:longitude) (defaults to: {})
    Float

    Location’s longitude

Returns:

  • (Net::HTTPResponse)

Raises:

  • (ArgumentError)

    Error raised when supplied argument are missing :to_mid, :title, :latitude, :longitude keys.



112
113
114
115
116
117
118
119
120
# File 'lib/line/bot/client.rb', line 112

def send_location(attrs = {})
  message = Message::Location.new(
    title: attrs[:title],
    address: attrs[:address],
    latitude: attrs[:latitude],
    longitude: attrs[:longitude],
  )
  send_message(attrs[:to_mid], message)
end

#send_message(to_mid, message) ⇒ Net::HTTPResponse

Sends message to line server and to users

Parameters:

Returns:

  • (Net::HTTPResponse)

Raises:

  • (ArgumentError)

    Error raised when supplied argument are missing message.



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

def send_message(to_mid, message)
  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.message        = message
  end

  request.post
end

#send_sticker(attrs = {}) ⇒ Net::HTTPResponse

Sends sticker to users

Parameters:

  • attrs (Hash) (defaults to: {})
  • attrs (:to_mid) (defaults to: {})
    String or Array

    User’s indentifiers

  • attrs (:stkpkgid) (defaults to: {})
    String or Integer

    Sticker’s package identifier

  • attrs (:stkid) (defaults to: {})
    String or Integer

    Sticker’s identifier

  • attrs (:stkver) (defaults to: {})
    String or Integer

    Sticker’s version number

Returns:

  • (Net::HTTPResponse)

Raises:

  • (ArgumentError)

    Error raised when supplied argument are missing :to_mid, :stkpkgid, :stkid, :stkver keys.



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

def send_sticker(attrs = {})
  message = Message::Sticker.new(
    stkpkgid: attrs[:stkpkgid],
    stkid: attrs[:stkid],
    stkver: attrs[:stkver],
  )
  send_message(attrs[:to_mid], message)
end

#send_text(attrs = {}) ⇒ Net::HTTPResponse

Sends text to users

Parameters:

  • attrs (Hash) (defaults to: {})
  • attrs (:to_mid) (defaults to: {})
    String or Array

    User’s indentifiers

  • attrs (:text) (defaults to: {})
    String

Returns:

  • (Net::HTTPResponse)

Raises:

  • (ArgumentError)

    Error raised when supplied argument are missing :to_mid, :text keys.



43
44
45
46
47
48
# File 'lib/line/bot/client.rb', line 43

def send_text(attrs = {})
  message = Message::Text.new(
    text: attrs[:text],
  )
  send_message(attrs[:to_mid], message)
end

#send_video(attrs = {}) ⇒ Net::HTTPResponse

Sends video to users

Parameters:

  • attrs (Hash) (defaults to: {})
  • attrs (:to_mid) (defaults to: {})
    String or Array

    User’s indentifiers

  • attrs (:video_url) (defaults to: {})
    String

    Video file’s url

  • attrs (:preview_url) (defaults to: {})
    String

    Preview image file’s url

Returns:

  • (Net::HTTPResponse)

Raises:

  • (ArgumentError)

    Error raised when supplied argument are missing :to_mid, :video_url, :preview_url keys.



76
77
78
79
80
81
82
# File 'lib/line/bot/client.rb', line 76

def send_video(attrs = {})
  message = Message::Video.new(
    video_url: attrs[:video_url],
    preview_url: attrs[:preview_url],
  )
  send_message(attrs[:to_mid], message)
end

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

Validates signature

Parameters:

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

    Request’s body

  • channel_signature (String)

    Request’header ‘X-LINE-ChannelSignature’ # HTTP_X_LINE_CHANNELSIGNATURE

Returns:

  • (Boolean)


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