Telegram bot made easy, a covid-19 statistics bot. 
Tags: telegram, node, javascript, machinelearning
Published: 2020-05-05 21:33:38


Hello there, i've been struggling for days to make a telegram bot, and after i learn all the basics i've found that it was so easy to do, you can try it here [@ElBaronBot](https://telegram.me/elbaronbot) .
So let's start making our bot.

### Getting the API key from bot father
 1. We start by communicating with bot father via this url [**@BotFather**](https://telegram.me/BotFather).
 2. Send it `/start`
 3. Then we send the new bot command `/newbot`
 4. The bot father asks us for a name for our bot, that name should end with bot, and i'll name mine **Elbaronbot**, and send it to it.
 5. Last step the [**@BotFather**](https://telegram.me/BotFather) give us our **Access token** to control the **bot**. 

###  Learning how to send and receive messages.
After getting the **API key** Lets start doing our favorite thing, which is coding.

 Open CMD and type : `npm init` then give your project a name and a discripting.

Now les install **the node telegram bot api** via this command :
 ```sh
npm i node-telegram-bot-api
```

Create a new file lets call it **index.js** and add this line of code to the header for calling this telegram api library 
```js
var TelegramBot = require('node-telegram-bot-api');
```

Then lets make a connection to telegram, **polling: true** means that our script will keep fetching the telegram conversations for updates.
```js
// Connecting to telegram
telegram = new TelegramBot("1*******:**************************************", {
    polling: true
});
``` 

Now lt's listen to new messages :
```js
// Triggers when new message arrive
telegram.on("text", (message) => {
// Sending message to user.
telegram.sendMessage(message.chat.id, "Hi " + message.chat.first_name + " Its working");
}
```

###  Making the bot.
Now since we know how to receive and send messages lets make a covid-19 Bot that help us find covid statistica for every country around the world, 

I want to trigger this action only when a user start the message with `/covid` then a country, like this `/covid morocco` or  `/covid usa` then so some thing with the country name.

Let's make a function that get a country and scrap for data then send a message to the user.
```js
function GetRes(country, telegram, chat_id) {
    let url = "https://api.coronastatistics.live/countries/" + country;
    let settings = {
        method: "Get"
    };
    fetch(url, settings).then(
		res => res.json()
	).then((json) => {
		resStr = '
		\n Ressaults for : ' + json.country + '
		\n Total cases : ' + json.cases + '
		\n Total active : ' + json.active + '
		\n Total recovered : ' + json.recovered + '
		\n Total deaths : ' + json.deaths + '
		\n Today cases : ' + json.todayCases + '
		\n Today Deaths : ' + json.todayDeaths + '
		';
            if (typeof json.country !== 'undefined' && json.country) {
                telegram.sendMessage(chat_id,resStr);
            } else {
                telegram.sendMessage(chat_id,"no such country...");
            }
        });
}
```

Now let's call the function when someone send us a message starting with `/covid`

```js
var TelegramBot = require('node-telegram-bot-api');
// Connecting to telegram
telegram = new TelegramBot("1*******:**************************************", {
    polling: true
});
// Triggers when new message arrive
telegram.on("text", (message) => {
// Check if the message start with /covid
    if (message.text.toLowerCase().indexOf("/covid") === 0) {
	    // remouve that /covid to get only the country 
        country = message.text.replace("/covid ", "");
        //give it to the function so it take care of the rest
        GetRes(country, telegram, message.chat.id);
    }
}
```

### Conclusions 
So this is was a basic telegram bot hope you like it you can try it here [@ElBaronBot](https://telegram.me/elbaronbot), leave me your messages, and you can follow me on twitter here [𝙹𝚒𝚋𝚛āʾī𝚕 ⚡](https://twitter.com/Elbaron_V3I)