Skip to content

Build a telegram bot with NodeJS in 4 minutes

Learn how to build a custom telegram bot in 4 minutes with Napkin

Post written by Joel Benjamin

Joel Benjamin

Build a telegram bot with NodeJS in 4 minutes

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


Search for @botfather bot on telegram

Go to BotFather and click

. It'll guide you through the process of creating a bot profile.

Click start on @botfather to begin a conversation. Botfather lists all commands

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.

click or type command /newbot, choose a name for the bot

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.

@botfather sends you a message with the API keys

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 and let's build that bot!

Create napkin function dialog

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.

napkin editor with example code

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.

/start command for @my_id_bot. Bot replies with telegram user id

Once you grab that, head over to the

tab > Environment variables and click on “Add” to add the keys.

Environment variables tab on napkin. Click on add

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.

Environment variables added

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.

Install modules on napkin by switching to the modules tab

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.

Telegram bot demo. Bot sends a message when run button is clicked

Let's take it one step further, let's make the bot say

hello there
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.

Napkin schedule modal. Choose every minute option

That'll make your function trigger every minute and send you a message each time it runs.

Napkin scheduling demo. Bot sends message every minute

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!!

Join our Serverless Community

Become part of our growing community of serverless developers on Discord. Got questions about serverless, bots or backend development in general? We got you covered!

Write and deploy cloud functions from your browser.

Napkin is the quickest way to build your backend