Sha256: aec0f5c5e281617762d95f1d7b53bd649287dd36d70029eab58efff770356a50

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

require 'logger'

module Capwatch
  class Telegram

    attr_reader :logger, :bot, :fund

    def initialize(fund: FundParser.new.fund, token:)
      @fund = fund
      @logger = Logger.new(STDOUT, Logger::DEBUG)
      @logger.debug 'Starting telegram bot...'
      @bot = TelegramBot.new(token: token)
    end

    def start
      bot.get_updates(fail_silently: true) do |message|
        logger.info "@#{message.from.username}: #{message.text}"
        command = message.get_command_for(bot)

        message.reply do |reply|
          begin
            case command
            when /\/cap/i
              table = Console.format_table(Calculator.fund_hash(fund, CoinMarketCap.fetch))
              reply.text = table[:footer].reject(&:empty?).join("\n")
            when /\/watch/i
              table = Console.format_table(Calculator.fund_hash(fund, CoinMarketCap.fetch))
              text = [
                "*#{table[:title]}*",
                "\n",
                table[:table].map{|x| x.join("   |   ") }.join("\n"),
                "\n",
                table[:footer].reject(&:empty?).join("   |   ")
              ].join("\n")
              reply.text = text
            else
              reply.text = "#{message.from.first_name}, have no idea what _#{command}_ means."
            end
            logger.info "sending #{reply.text.inspect} to @#{message.from.username}"
            reply.parse_mode = 'Markdown'
            reply.send_with(bot)
          rescue => e
            logger.error e
          end
        end
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
capwatch-0.1.13 lib/capwatch/telegram.rb