Class: Capwatch::Telegram

Inherits:
Object
  • Object
show all
Defined in:
lib/capwatch/telegram.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Telegram

Returns a new instance of Telegram.



12
13
14
15
16
17
18
# File 'lib/capwatch/telegram.rb', line 12

def initialize(token)
  @config = config
  @logger = Logger.new(STDOUT, Logger::DEBUG)
  @logger.debug "Starting telegram bot..."
  @bot = TelegramBot.new(token: token)
  @console_formatter = Capwatch::Console::Formatter
end

Instance Attribute Details

#botObject (readonly)

Returns the value of attribute bot.



10
11
12
# File 'lib/capwatch/telegram.rb', line 10

def bot
  @bot
end

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/capwatch/telegram.rb', line 10

def config
  @config
end

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/capwatch/telegram.rb', line 10

def logger
  @logger
end

Instance Method Details

#format_body(body) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/capwatch/telegram.rb', line 41

def format_body(body)
  JSON.parse(body).sort_by! { |e| -e["value_btc"].to_f }.map do |coin|
    [
      coin["name"],
      Console::Formatter.format_usd(coin["price_usd"]),
      coin["quantity"],
      Console::Formatter.format_percent(coin["distribution"].to_f * 100),
      Console::Formatter.format_btc(coin["value_btc"]),
      Console::Formatter.format_eth(coin["value_eth"]),
      Console::Formatter.format_percent(coin["percent_change_24h"]),
      Console::Formatter.format_percent(coin["percent_change_7d"]),
      Console::Formatter.format_usd(coin["value_usd"])
    ]
  end
end

#new_fundObject



20
21
22
23
24
# File 'lib/capwatch/telegram.rb', line 20

def new_fund
  config = Fund::Config.new
  provider = Providers::CoinMarketCap.new
  Fund.new(provider: provider, config: config)
end

#reply_capObject



30
31
32
33
# File 'lib/capwatch/telegram.rb', line 30

def reply_cap
  fund = new_fund
  ERB.new(template("cap.erb")).result(binding)
end

#reply_watchObject



35
36
37
38
39
# File 'lib/capwatch/telegram.rb', line 35

def reply_watch
  fund = new_fund
  body = format_body(fund.serialize)
  ERB.new(template("watch.erb")).result(binding)
end

#startObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/capwatch/telegram.rb', line 57

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
          reply.text = reply_cap
        when /\/watch/i
          reply.text = reply_watch
        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

#template(name) ⇒ Object



26
27
28
# File 'lib/capwatch/telegram.rb', line 26

def template(name)
  File.open(File.expand_path("#{__dir__}/../templates/#{name}")).read
end