Try the bot here https://t.me/covid19liveupdate
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.
telegraf framework, it's pretty easy to create a Telegram bot for youself.
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
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.
It's the heart of your new bot, it will listen for messages, commands from users, and replies them as you prepared.
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
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)
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 ***')
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.