~michalr/telegram-covid-bot

64f94a2b1e5ff01954f3e20158963e80c8c2f8a9 — MichaƂ Rudowicz 4 years ago 49c6077
Comparison of previous values
2 files changed, 39 insertions(+), 6 deletions(-)

M README.md
M bot.rb
M README.md => README.md +3 -1
@@ 3,6 3,8 @@
Simple Ruby bot for fetching the data from covid19api.com and pushing to a specified Telegram
group or channel.

Stores previous data in `previous.json` for comparison and to ignore the data that has not changed.

## Configuration

Check out the supplied `config.example.yml` for the configuration file format.


@@ 15,5 17,5 @@ What you'll need:

## Running

`ruby bot.rb path/to/config.yml`
`ruby bot.rb path/to/config.yml path/to/previous.json`


M bot.rb => bot.rb +36 -5
@@ 2,16 2,29 @@ require 'yaml'
require 'json'
require 'net/http'

unless ARGV.length == 2 then
    puts "Usage: bot.rb [path-to-config.yml] [path-to-previous-data.json]"
    exit 1
end

Config = YAML.load_file(ARGV[0]).freeze
begin
    PreviousData = JSON.parse(File.open(ARGV[1], 'r').read).freeze
rescue
    PreviousData = {}
end
Lang = Config['Translation'].freeze

def sendToTelegram(country)
    textToSend = "<b>#{Lang['StatusFor']} #{country['CountryCode']} #{Lang['For']}:</b> <code>#{country['Date']}</code>\n"\
                 "<b>#{Lang['TotalConfirmed']}:</b> <code>#{country['TotalConfirmed']}</code>\n"\
                 "<b>#{Lang['TotalConfirmed']}:</b> <code>#{country['TotalConfirmed']}</code> "\
                 "<i>(#{country['totalConfirmedDiff']})</i>\n"\
                 "<b>#{Lang['TotalRecovered']}:</b> <code>#{country['TotalRecovered']}</code> "\
                 "(<i>#{country['TotalRecovered'].to_f/country['TotalConfirmed'].to_f*100}%</i>)\n"\
                 "(<i>#{country['TotalRecovered'].to_f/country['TotalConfirmed'].to_f*100}%, "\
                 "#{country['totalRecoveredDiff']}</i>)\n"\
                 "<b>#{Lang['TotalDeaths']}:</b> <code>#{country['TotalDeaths']}</code> "\
                 "(<i>#{country['TotalDeaths'].to_f/country['TotalConfirmed'].to_f*100}%</i>)"
                 "(<i>#{country['TotalDeaths'].to_f/country['TotalConfirmed'].to_f*100}%, "\
                 "#{country['totalDeathsDiff']}</i>)"

    res = Net::HTTP.post_form(URI("https://api.telegram.org/bot#{Config['TelegramToken']}/sendMessage"),
                        'chat_id' => Config['TelegramChatId'],


@@ 22,8 35,26 @@ def sendToTelegram(country)
    end
end

def s(value)
    value_sign = '+' if value > 0
    "#{value_sign}#{value}"
end

raw_json = Net::HTTP.get(URI("https://api.covid19api.com/summary"))
JSON.parse(raw_json)['Countries']

current_data = JSON.parse(raw_json)['Countries']
    .select { |e| e['CountryCode'] == Config['CountryCode'] }
    .each { |country| sendToTelegram country }
    .first

unless current_data.reject { |k,_| k == 'Date' } == PreviousData.reject { |k,_| k == 'Date' } then
    previous_data = PreviousData unless PreviousData.empty?
    previous_data = current_data if PreviousData.empty?

    File.open(ARGV[1], 'w') { |f| f.write(JSON.dump(current_data)) }

    sendToTelegram(current_data.merge({
        'totalConfirmedDiff' => s(current_data['TotalConfirmed'] - previous_data['TotalConfirmed']),
        'totalRecoveredDiff' => s(current_data['TotalRecovered'] - previous_data['TotalRecovered']),
        'totalDeathsDiff' => s(current_data['TotalDeaths'] - previous_data['TotalDeaths']),
    }))
end