~michalr/telegram-covid-bot

9e4f2e4f4159bd63beb46c7db30de223fac66c39 — MichaƂ Rudowicz 4 years ago f32dafc
ERB templates for messages
2 files changed, 8 insertions(+), 13 deletions(-)

M bot.rb
M config.example.yml
M bot.rb => bot.rb +3 -7
@@ 5,6 5,7 @@ require 'json'
require 'net/http'
require 'date'
require 'csv'
require 'erb'

unless ARGV.length == 2
  puts 'Usage: bot.rb [path-to-config.yml] [path-to-previous-data.json]'


@@ 17,15 18,10 @@ begin
rescue StandardError
  PREVIOUS_DATA = {}.freeze
end
Lang = Config['Translation'].freeze

def format_for_telegram(country)
  %{
<b>#{Lang['StatusFor']} #{country['CountryCode']} #{Lang['For']}:</b> <code>#{country['Date']}</code>
<b>#{Lang['TotalConfirmed']}:</b> <code>#{country['TotalConfirmed']}</code> <i>(#{country['totalConfirmedDiff']})</i>
<b>#{Lang['TotalRecovered']}:</b> <code>#{country['TotalRecovered']}</code> (<i>#{country['TotalRecovered'].to_f / country['TotalConfirmed'].to_f * 100}%, #{country['totalRecoveredDiff']}</i>)
<b>#{Lang['TotalDeaths']}:</b> <code>#{country['TotalDeaths']}</code> (<i>#{country['TotalDeaths'].to_f / country['TotalConfirmed'].to_f * 100}%, #{country['totalDeathsDiff']}</i>)
  }
  t = ERB.new(Config['Template'])
  t.result(binding)
end

def send_to_telegram(country)

M config.example.yml => config.example.yml +5 -6
@@ 1,9 1,8 @@
CountryCode: 'Poland'
TelegramToken: 'Your telegram token obtained from @BotFather'
TelegramChatId: '@your-channel-id'
Translation:
  StatusFor:        'Status for'
  For:              'for'
  TotalConfirmed:   'Total confirmed'
  TotalRecovered:   'Total recovered'
  TotalDeaths:      'Total deaths'
Template: |
  <b>Status for <%= country['CountryCode'] %> for:</b> <code><%= country['Date'] %></code>
  <b>Total confirmed:</b> <code><%= country['TotalConfirmed'] %></code> <i>(<%= country['totalConfirmedDiff'] %></i>)
  <b>Total recovered:</b> <code><%= country['TotalRecovered'] %></code> (<i><%= country['TotalRecovered'].to_f / country['TotalConfirmed'].to_f * 100 %>, <%= country['totalRecoveredDiff'] %></i>)
  <b>Total deaths:</b> <code><%= country['TotalDeaths'] %></code> (<i><%= country['TotalDeaths'].to_f / country['TotalConfirmed'].to_f * 100 %>, <%= country['totalDeathsDiff'] %></i>)