~michalr/telegram-covid-bot

65ef3a0cb59d70cfff7ec32dd290a1e9a821e833 — Michał Rudowicz 4 years ago
Initial commit
4 files changed, 64 insertions(+), 0 deletions(-)

A COPYING
A README.md
A bot.rb
A config.example.yml
A  => COPYING +18 -0
@@ 1,18 @@
    DO WHAT THE FUCK YOU WANT TO BUT IT'S NOT MY FAULT PUBLIC LICENSE
                    Version 1, October 2013

 Copyright 2020 Michał Rudowicz <michal.rudowicz@fl9.eu>

 Everyone is permitted to copy and distribute verbatim or modified
 copies of this license document, and changing it is allowed as long
 as the name is changed.

   DO WHAT THE FUCK YOU WANT TO BUT IT'S NOT MY FAULT PUBLIC LICENSE
   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

  0. You just DO WHAT THE FUCK YOU WANT TO.

  1. Do not hold the author(s), creator(s), developer(s) or
     distributor(s) liable for anything that happens or goes wrong
     with your use of the work.
 

A  => README.md +19 -0
@@ 1,19 @@
# Coronavirus Telegram bot for covid19api.com

Simple Ruby bot for fetching the data from covid19api.com and pushing to a specified Telegram
group or channel.

## Configuration

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

What you'll need:

 * A country code of the country you're interested in,
 * Telegram bot API Key, to be obtained on Telegram using the `@BotFather` bot,
 * A group or a channel where the messages will be posted

## Running

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


A  => bot.rb +24 -0
@@ 1,24 @@
require 'yaml'
require 'json'
require 'open-uri'
require 'net/http'

Config = YAML.load_file(ARGV[0]).freeze

def sendToTelegram(country)
    textToSend = "<b>Stan dla #{country['CountryCode']} na:</b> <code>#{country['Date']}</code>\n"\
                 "<b>Łącznie potwierdzonych:</b> <code>#{country['TotalConfirmed']}</code>\n"\
                 "<b>Łącznie wyzdrowiało:</b> <code>#{country['TotalRecovered']}</code> (<i>#{country['TotalRecovered'].to_f/country['TotalConfirmed'].to_f*100}%</i>)\n"\
                 "<b>Łącznie zmarło:</b> <code>#{country['TotalDeaths']}</code> (<i>#{country['TotalDeaths'].to_f/country['TotalConfirmed'].to_f*100}%</i>)"

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

open("https://api.covid19api.com/summary") do |raw_json|
    JSON.parse(raw_json.read)['Countries']
      .select { |e| e['CountryCode'] == Config['CountryCode'] }
      .each { |country| sendToTelegram country }
end

A  => config.example.yml +3 -0
@@ 1,3 @@
CountryCode: 'PL'
TelegramToken: 'Your telegram token obtained from @BotFather'
TelegramChatId: '@your-channel-id'