class TelegramMeetupBot::Client

Constants

TIMEOUT

Attributes

token[R]

Public Class Methods

new(token) click to toggle source
# File lib/telegram_meetup_bot/client.rb, line 7
def initialize(token)
  @token = token
end

Public Instance Methods

run() click to toggle source
# File lib/telegram_meetup_bot/client.rb, line 11
def run
  Telegram::Bot::Client.run(token) do |bot|
    bot.enable_botan!(botan_key) if botan_key
    bot.listen do |message|
      case message
      when Telegram::Bot::Types::Message
        process_message(bot, message) if message.text
      when Telegram::Bot::Types::CallbackQuery
        process_callback_query(bot, message) if message.data
      end
    end
  end
rescue Telegram::Bot::Exceptions::ResponseError => e
  write_log_and_sleep(e)
  retry # run again on telegram server error
end

Private Instance Methods

botan_key() click to toggle source
# File lib/telegram_meetup_bot/client.rb, line 52
def botan_key
  Initializers::ConfigLoader.botan_key
end
process_callback_query(bot, callback_query) click to toggle source
# File lib/telegram_meetup_bot/client.rb, line 41
def process_callback_query(bot, callback_query)
  messenger = Messenger.new(api: bot.api,
    chat_id: callback_query.message.chat.id,
    message_id: callback_query.message.message_id)

  CommandsHandler.new(
    callback_query: callback_query,
    messenger: messenger
  ).process
end
process_message(bot, message) click to toggle source
# File lib/telegram_meetup_bot/client.rb, line 30
def process_message(bot, message)
  messenger = Messenger.new(api: bot.api, chat_id: message.chat.id)
  botan = Botan.new(bot: bot, author_id: message.from.id) if botan_key

  CommandsHandler.new(
    message: message,
    messenger: messenger,
    botan: botan
  ).process
end
write_log_and_sleep(error) click to toggle source
# File lib/telegram_meetup_bot/client.rb, line 56
def write_log_and_sleep(error)
  puts "#{Time.now}: #{error}"
  sleep TIMEOUT
end