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

#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

#get(endpoint_path) ⇒ Net::HTTPResponse

Fetch data, get content of specified URL.

Parameters:

  • endpoint_path (String)

Returns:

  • (Net::HTTPResponse)

Raises:



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.

Parameters:

  • identifier (String)

    Message’s identifier

Returns:

  • (Net::HTTPResponse)


139
140
141
142
# File 'lib/line/bot/client.rb', line 139

def get_message_content(identifier)
  endpoint_path  = "/message/#{identifier}/content"
  get(endpoint_path)
end

#get_profile(user_id) ⇒ Net::HTTPResponse

Get an user’s profile.

Parameters:

  • user_id (String)

    User’s identifiers

Returns:

  • (Net::HTTPResponse)


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

Parameters:

  • request_body (String)

Returns:

  • (Array<Line::Bot::Event::Class>)


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.

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  = '/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  = '/message/reply'
    config.credentials    = credentials
    config.reply_token    = token
    config.messages       = messages
  end

  request.post
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)


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