Let’s Make A Telegram Bot With Node.js – Telegram Group

Telegram

Let’s Make A Telegram Bot With Node.js

Photo By Telegram Advisor

I apologize for not posting lately I’ve been busy with other projects and trying to apply to remote jobs abroad so i didn’t have much time to come up with anything interesting to share but hopefully you guys will like this 😅

In this blog we’ll discover how easy it is to make a telegram bot with node.js and when i say easy, i really mean REALLY easy

I’ve always felt a bit intimidated by making a Telegram bot specifically for no real reason honestly, i just imagined it to be something complex because of obtaining a token and stuff but recently i found myself once again in a terrible sleeping disaster so i opened up my laptop, fired up vscode and decided to try and see if it’s really that difficult ????

What Is A Telegram Bot ❓

A telegram bot is essentially a program that interacts with the user based on events and conditions, basically think about it in this scenario

You get to send the bot a message say “ping” and the bot responds with an image, link, video, audio, map or anything else as per described in the program

So the idea isn’t different from any other bot you’ve heard of so let’s see a super simple example of a telegram bot

Example Code Of A Telegram Bot

Down below is a minimum bot written in Javascript that’ll check every message if it’s equal to “ping” then respond with a greeting

And the result of this code is the following

Apologies for the giant image 😂

With that being said, let’s look into more details regarding making your first telegram bot

Prerequisites

Firstly, we need to install few packages with npm to help us with the project, needless to say initialize the project with npm init -y

npm i node-telegram-bot-api // the telegram bot api wrapper
npm i dotenv // to work with

As of the time of writing this, I’m running node version 17.4.0 which you can check with node --version

Setting Up A Telegram Bot

Ok so before we get to the coding part we need to “register” and authenticate our application, luckily for us that’s made thanks to the @BotFather which is a bot made to make bots, i know it’s wild 😂

So open up your telegram app on phone or pc and look up @BotFather and the instructions will be pretty clear, after obtaining your token, create a .env file to store your token safely so no one will see it in case you push the project to a public repo, then create a .gitignore file and add the .env file to it so it won’t be committed in case you’re using git

Making Our Bot Say Hello

This next example is quite similar to the one earlier but we’re gonna explore the api even more

We firstly import what we need, which are the api wrapper and dotenv

Then create a telegrambot instance which will represent our bot that’ll interact with the telegram bot api

The way this api wrapper works is events, events are emitted and your bot responds accordingly

So in this case we have a message events fired up which means a message has been sent to the bot in the chat, it takes a callback as an argument and this callback has one interesting argument, let’s console.log() that and see what useful properties it has

As you can see, it has many interesting properties we can use in our program

Make Our Bot Roll A Dice

Another interesting thing we can do is playing rolling dice with the bot, yep you’re gonna be playing with yourself basically 😏 , let’s see how to implement that

See ? pretty simple, now if you look on your phone you should see an animated dice has been sent after you sent “dice” in the chat

Making Polls

It’s often common to enable users to participate in a poll and to implement that with node.js we simply use the same bot object to prompt users to pick an option we specify as the following example

Now let’s test it out and see the result

What’s Next ?

Ok now that you’ve seen how to make your own telegram bot, the sky is the limit now, you may implement any idea you want and i can’t even begin to list what can be done with this knowledge, whatever that you can do with Node.js you can now do with this bot, an express.js server to handle some processing and communicate with the bot via HTTP using axios ? Yes it can be done !

Web scrape some content and respond back ? Easy peasy, matter of fact, 20 CLAPS ON THIS BLOG AND I’LL DO IT

But you might be wondering how to wrap your head around the api ? Well I’d highly highly highly recommend visiting the repo and especially the usage part here

Thanks a lot for reading and i hope you’ve learned a thing or two here today and pls let me know in the comments below what you’re building ♥️

Ten articles before and after

Developing a Money-Making Telegram Bot on Python — Pt 2. – Telegram Group

Best Autoresponder for Telegram to set auto reply messages – Telegram Group

Day 4,5,6 – Telegram Group

data-rh=”true”>Ixinium (XXA) Official Telegram Channel – Ixinium (XXA) – Medium – Telegram Group

Build an e-commerce telegram web app bot? – Telegram Group

1097+ Best Telegram Group Join Links List Updated – Telegram Group

Create your beautiful sticker pack in Telegram using Figma – Telegram Group

Pro-Kremlin Telegram channels promote narrative that Poland will annex western Ukraine – Telegram Group

Выходные с книгой. Рассказываем. Вып. 253 – Telegram Group

How to improve the monitoring of your Lambda functions with Telegram. Telegram bots AWS Lambda logging – Telegram Group

About Me

Pretium lorem primis senectus habitasse lectus donec ultricies tortor adipiscing fusce morbi volutpat pellentesque consectetur risus curae malesuada dignissim lacus convallis massa mauris.