👨🏼‍💻 Introduction to Telegram Bot

Samet Zengin
Huawei Developers
Published in
5 min readAug 1, 2022

Hello Everyone,
In this article we will learn how to create bots with Telegram API.

Telegram Bots

Introduction

Telegram is a free, cross-platform and cloud-based instant messaging application. Being an open source software, it has more advanced features than WhatsApp. One of them is developer bots. As of June 2015, Telegram has allowed third-party developers to develop bots.

What can be done with Telegram Bots?

· It can reply and react to your messages.
· It can be added to groups.
· Integration with other programs can be provided.
· Online payments can be made (Paymentwall, Yandex.Money, Stripe, Ravepay, Razorpay, QiWi, Apple Pay, Google Pay)
· It can be integrated with IOT services. (IFTTT)
· It can be used to manage your Telegram channel.

Telegram Bot API

https://core.telegram.org/bots/api

Telegram Bot API Documentation

Here you can see the various methods you will use in your bot application.

Let’s Start!

To create the bot first, find the account named Botfather in Telegram and select /newbot from the list of commands.

In the next step, it will ask you to give a name to the bot you will create.

In this step, telegram allows you to use upper and lower case letters.

5470xxxxxxxx:XXXXXXXXXXXXXXXXXXXXCeZ8U
A token has been created for HTTP API usage. Copy the code.

Let’s learn how to receive messages through our bot. There are 2 methods Telegram offers us in this regard.

First method is polling.

Our server sends frequent requests to Telegram constantly. It returns http request when it receives any update. The disadvantage of this method is that when you get too much traffic, you will send too many requests to Telegram servers in parallel.

Another method is Webhooks.

When you send a request to your bot, it forwards this request to the Telegram server. Then telegram forwards it to your server as an http request via your public endpoint (webhook).

Let’s Start to Project
You can use ngrok tool.

It returns all requests as a bridge between Ngrok telegram server and your local server as http request over the subdomain it defines. Shortly, it defines a domain address for you to open up to the outside world.

We will develop our bot with Node.js. If it is not installed, you can install it from this link.

Let’s go to the directory we want to create our project and initialize it with the ‘npm init’ command.

· Let’s add the necessary dependencies for our project with the npm i -save depencyname command.

· The dependencies we will use are express, body-parser, axios, dotenv and nodemon which is the development dependency.

· Attention, you can install development dependencies with the commandnpm -i –save-dev’.

· After installing, open the package.json file in your project directory.

You can see the versions of the dependencies on the side. It may be different with your depency version.

· Now let’s define the environment variables needed for our project to work. For this, create an .env file in the project directory and enter the token value given to you.

· Let’s create a variable named SERVER_URL. Enter your public url in this field. For Ngrok, you can write your server address that you started with the ngrok http 5000 command.

· Enter the index.js file. First, include all required defences. Include .env variables in the class with the code require(‘dotenv’).config. For other depency, you can use const depencyname = require(‘depency’) code.

· Let’s define the Telegram API endpoint.

· Run the express module and decode the returned json code with bodyparser.

· At this stage, define the port where the project will run and follow it by pressing the log on the console.

https://core.telegram.org/bots/api#setwebhook

· Make a get request with axious and press the console to see the result. Add the function you wrote to the listen function.

· Call init function in app.listen function. In order to better see the bug reports in your project, you can start it with the ‘npm run dev’ command.

· On this screen, we can see that our webhook has been set up successfully.

· Pass the result as a post request to show that we have successfully received respond from the Telegram server.

Logs in Ngrok Panel

· Let’s use the “send message endpoint” for our bot to send the same message as ours and post request with axios.

· And our awesome bot sends us the same message again.

· Our code will look like the following.

Conclusion

This is how you can easily create your own Telegram Bot with Node.js. By reviewing the documentation, you can learn about different parameters and further develop your project.

I hope it was useful article for you. See you in the next article👋

--

--