Class: Telegram::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/telegram/bot.rb,
lib/telegram/bot/railtie.rb,
lib/telegram/bot/version.rb,
lib/telegram/bot/middleware.rb,
lib/telegram/bot/routes_helper.rb,
lib/telegram/bot/updates_poller.rb,
lib/telegram/bot/updates_controller.rb,
lib/telegram/bot/updates_controller/log_subscriber.rb,
lib/telegram/bot/updates_controller/instrumentation.rb

Defined Under Namespace

Modules: RoutesHelper Classes: Error, Middleware, NotFound, Railtie, UpdatesController, UpdatesPoller

Constant Summary collapse

URL_TEMPLATE =
'https://api.telegram.org/bot%s/'.freeze
VERSION =
'0.3.0'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, username = nil) ⇒ Bot

Returns a new instance of Bot.



38
39
40
41
42
43
# File 'lib/telegram/bot.rb', line 38

def initialize(token, username = nil)
  @client = HTTPClient.new
  @token = token
  @username = username
  @base_uri = format URL_TEMPLATE, token
end

Instance Attribute Details

#base_uriObject (readonly)

Returns the value of attribute base_uri.



36
37
38
# File 'lib/telegram/bot.rb', line 36

def base_uri
  @base_uri
end

#clientObject (readonly)

Returns the value of attribute client.



36
37
38
# File 'lib/telegram/bot.rb', line 36

def client
  @client
end

#tokenObject (readonly)

Returns the value of attribute token.



36
37
38
# File 'lib/telegram/bot.rb', line 36

def token
  @token
end

#usernameObject (readonly)

Returns the value of attribute username.



36
37
38
# File 'lib/telegram/bot.rb', line 36

def username
  @username
end

Class Method Details

.gem_versionObject



5
6
7
# File 'lib/telegram/bot/version.rb', line 5

def self.gem_version
  Gem::Version.new VERSION
end

.wrap(input) ⇒ Object

Accepts different options to initialize bot.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/telegram/bot.rb', line 23

def wrap(input)
  case input
  when self then input
  when Array then input.map(&method(__callee__))
  when Hash then
    input = input.stringify_keys
    new input['token'], input['username']
  else
    new(input)
  end
end

Instance Method Details

#debug!(dev = STDOUT) ⇒ Object



45
46
47
# File 'lib/telegram/bot.rb', line 45

def debug!(dev = STDOUT)
  client.debug_dev = dev
end

#debug_off!Object



49
50
51
# File 'lib/telegram/bot.rb', line 49

def debug_off!
  client.debug_dev = nil
end

#http_request(uri, body) ⇒ Object

Endpoint for low-level request. For easy host highjacking & instrumentation. Params are not used directly but kept for instrumentation purpose. You probably don’t want to use this method directly.



88
89
90
# File 'lib/telegram/bot.rb', line 88

def http_request(uri, body)
  client.post(uri, body)
end

#inspectObject



92
93
94
# File 'lib/telegram/bot.rb', line 92

def inspect
  "#<Telegram::Bot##{object_id}(#{@username})>"
end

#request(action, data = {}) ⇒ Object

Raises:



53
54
55
56
57
58
59
60
61
62
# File 'lib/telegram/bot.rb', line 53

def request(action, data = {})
  res = http_request("#{base_uri}#{action}", data)
  status = res.status
  return JSON.parse(res.body) if 300 > status
  result = JSON.parse(res.body) rescue nil # rubocop:disable RescueModifier
  err_msg = "#{res.reason}: #{result && result['description'] || '-'}"
  # NotFound is raised only for valid responses from Telegram
  raise NotFound, err_msg if 404 == status && result
  raise Error, err_msg
end