Class: Diligence::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/diligence/api_client.rb

Constant Summary collapse

BASE_URL =
'https://api.telegram.org/bot'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ ApiClient

Returns a new instance of ApiClient.



9
10
11
12
13
# File 'lib/diligence/api_client.rb', line 9

def initialize(token)
  @token = token
  @bot_info = fetch_bot_info
  print_bot_info
end

Instance Attribute Details

#bot_infoObject (readonly)

Returns the value of attribute bot_info.



7
8
9
# File 'lib/diligence/api_client.rb', line 7

def bot_info
  @bot_info
end

Instance Method Details

#get_updates(offset: nil, timeout: 30) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/diligence/api_client.rb', line 15

def get_updates(offset: nil, timeout: 30)
  params = { timeout: timeout }
  params[:offset] = offset if offset

  response = HTTParty.get("#{BASE_URL}#{@token}/getUpdates", query: params)

  return [] unless response.success?

  response.parsed_response['result']
end

#send_message(chat_id:, text:, **options) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/diligence/api_client.rb', line 26

def send_message(chat_id:, text:, **options)
  params = {
    chat_id: chat_id,
    text: text
  }.merge(options)

  response = HTTParty.post("#{BASE_URL}#{@token}/sendMessage", body: params)

  unless response.success?
    raise "Failed to send message: #{response.code} - #{response.message}"
  end

  response.parsed_response['result']
end