Class: Capwatch::Telegram
- Inherits:
-
Object
- Object
- Capwatch::Telegram
- Defined in:
- lib/capwatch/telegram.rb
Instance Attribute Summary collapse
-
#bot ⇒ Object
readonly
Returns the value of attribute bot.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
- #format_body(body) ⇒ Object
-
#initialize(token) ⇒ Telegram
constructor
A new instance of Telegram.
- #new_fund ⇒ Object
- #reply_cap ⇒ Object
- #reply_watch ⇒ Object
- #start ⇒ Object
- #template(name) ⇒ Object
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
#bot ⇒ Object (readonly)
Returns the value of attribute bot.
10 11 12 |
# File 'lib/capwatch/telegram.rb', line 10 def bot @bot end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
10 11 12 |
# File 'lib/capwatch/telegram.rb', line 10 def config @config end |
#logger ⇒ Object (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_fund ⇒ Object
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_cap ⇒ Object
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_watch ⇒ Object
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 |
#start ⇒ Object
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 || logger.info "@#{message.from.username}: #{message.text}" command = .get_command_for(bot) .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.("#{__dir__}/../templates/#{name}")).read end |