Building bots is a whole lot of fun, although setting them up can be a bit tedious. Today I'm going to show you how to build and deploy a telegram bot in under four minutes.
Minute 1 - Create a bot profile
To build a bot on telegram, first, you need a bot profile. To do that,
open Telegram and search for
Go to BotFather and click
. It'll guide you through the process of
creating a bot profile.
Click “/newbot” and it'll ask you a series of questions. Choose a name and a unique username for you bot. Once that's done, hurray you've created a new telegram bot! But right now, it does nothing so let's not pat ourselves on the back just yet.
Once, the bot is created, it should send you a message with your API key. It'll look something like the image below. Save the API key somewhere, we'll need it later.
Minute 2 - Connecting the bot
Now for the bot to work, we need to run it somewhere. For that, we'll use a cloud function. If you don't know what a cloud function is, it's basically a bit of code that runs on a server somewhere and you don't have to manage anything. Cloud functions are really great for building automation and bots because they're easy and lightweight. We can use Napkin to quickly code one up. So head over to napkin.io and let's build that bot!
Once you've signed on to Napkin, it should take you to the dashboard. Once there, click on “Create a function”. For this tutorial, we'll choose NodeJS as the runtime and then name our bot and then click on create.
Napkin will set up all the infrastructure in the cloud to run your code, then it should take us to the editor where we can code and deploy our bot.
Minute 3 - Coding the bot
Ok, I know I said code the bot in the title but before that, we'll need to do a couple of tiny setups. Remember the API key you saved earlier? Grab that! We have to set-up some env variables.
For the telegram bot to work, we'll need 2 things
- Telegram API key
- Telegram Chat Id
You have the API key, now to get the telegram chat id,
open Telegram → go to search → type
on Telegram → click start.
It should give you your chat id. We'll use this to send messages to you.
Once you grab that, head over to the
tab > Environment variables and
click on “Add” to add the keys.
Once you've added them, it should look something like this. Adding env vars here will make them available in the editor while you code.
Now, one last thing before we start coding. We'll be using the
library
to interact with the telegram bot API. Napkin comes pre-installed with 300k+ packages
so just head over to the
Tab and click on the download symbol.
Note: To receive messages from your bot, you'll need to go to the bot's profile and click on “/start” to start a conversation. Otherwise you'll see a 404 error.
Alright, you're good to go now. Now let's begin coding.
Minute 4 - Actually coding the bot
Go to the
tab of the editor and copy this code. The code initialises
the bot with our API key from earlier and lets us send messages to anyone
if you have their
. We grabbed ours from earlier, we can use that.
const { Telegraf } = require("telegraf")
const bot = new Telegraf(process.env.telegram_key, { telegram: { webhookReply: true }})
const CHATID = process.env.chat_id
export default async (req, res) => { await bot.telegram.sendMessage(CHATID, "hello there!!")}
Once done, click on
and our little bot should send you a message on telegram.
Let's take it one step further, let's make the bot say
every minute.
To do that on Napkin, it's pretty simple. Just click on the “Schedule” button
on the top right and select “Every Minute” from the options tab and hit save.
That'll make your function trigger every minute and send you a message each time it runs.
The end
So is this it? Yes, for now, my friend. The end is just the beginning and that's how you build a bot in 4 minutes. Feel free to customize it more, I'll write another tutorial soon on how to build a more advanced bot for telegram. In the meantime, if you want to learn how to build a discord bot, you can read this post by my friend Nick - Build a Discord Bot to Track the ISS with Napkin.
If you've got questions regarding anything, feel free to stop by on our Discord.
Happy building!!