TLDR
Try the bot here https://t.me/covid19liveupdate
Coronavirus COVID-19
As all you've already known, the novel coronavirus COVID-19 is affecting 37 countries and territories around the world and 1 international conveyance (the "Diamond Princess" cruise ship harbored in Yokohama, Japan). None of us want to panic, but informations are very helpful for every people.
How I created the bot?
With NodeJS
and telegraf
framework, it's pretty easy to create a Telegram bot for youself.
1) Create a bot
Before doing everything, you need to register a new bot with Telegram. Just need to chat with @BotFather, he will help you through steps (start with /newbot
)
In the end, the @BotFather
will register your bot, and give you an API Token
. Write it down, we'll need it for next steps.
2) Write bot's logic
It's the heart of your new bot, it will listen for messages, commands from users, and replies them as you prepared.
- Install
telegraf
yarn add telegraf
- Init the bot
# bot.js
const Telegraf = require('telegraf')
const BOT_TOKEN = '*** The API Token you got from previous step ***'
const bot = new Telegraf(BOT_TOKEN)
bot.start((context) => context.reply('Welcome!'))
bot.help((context) => context.reply('You can say hi'))
bot.hears('hi', (context) => context.reply('Hey there!'))
bot.launch()
- There it is! Very simple right? Your bot will say
"Welcome!"
for new user, or says"You can hi"
if user asks for help, and replies"Hey there!"
if user says"hi"
How my bot can send message?
There are 2 cases which the bot can send a message:
- Send message / Reply to a chat (bot <-> user)
- Broadcast to a channel (admin -> users)
1) Send messages in a chat
To send a message in a chat, you need to know the current chat. You can take the chat's information in the context
variable in the previous example.
When you're able to take the Chat ID from context.chat
, you're ready to send a message:
- Reply to current chat:
context.reply('*** Your message ***')
- Send a message to current chat (without user's message):
bot.telegram.sendMessage('*** Chat ID ***', '*** Your message ***')
2) Broadcast messages to a channel
To boardcast a message in a chat, you need to add the bot into your channel and make it becomes an admin.
Then you can simply send messages using:
bot.telegram.sendMessage('@YourChannelName', '*** Your message ***')
That's all! In next tutorials, we'll work together to deploy our bots to cloud, and keep it running to handle user's requests.
Top comments (2)
Hi! Really cool (and helpful) project! Where do you get the source data for covid-19 cases? Thank you
Good job, thanks.