ESP32 Based Telegram Bot

Introduction: ESP32 Based Telegram Bot

About: IoT projects, DIY projects, Arduino projects, Raspberry pi projects, NodeMCU based projects, Articles, Electronics projects.

Telegram is all about freedom and open-sources, it announced the new Telegram bot API in 2015, which allowed third parties to create telegram bots for ESP32 that utilize the messaging application as their main communication interface. This means we can control our smart home applications and other smart devices with it. So, in this article, we will be discussing a new way of controlling your smart appliances and knowing the status. Yeah, you’re right, we will be controlling them with a social media app, “Telegram”.

What is Telegram?
Telegram is a cross-platform cloud-based instant messaging, videotelephony, and VoIP service with end-to-end-encrypted chat for secret chat only, whereas server-client / client-server encryption is used in cloud chats.

But what differentiates it from other such messaging platforms is the ability to create bots.

Telegram bots are AI-based apps that can be configured to serve many different functions, some examples would be like, send relevant information about the weather or useful news articles, some are preconfigured to send reminders, also there are some which can play tunes or create to-do lists, and so much more.

Today we’ll also be creating one such bot which will communicate with our ESP32.

In this tutorial, we will be controlling a LED with the Telegram bot, the LED is connected to the ESP32 board. Instead of the LED, you can control any pin connected to any other appliance or component.

Step 1: ​Requirements for Building the ESP32 Based Telegram Bot

As this project is a software heavy project, it doesn’t require that much hardware, but there are quite a few steps that need to be followed in the software side of things, we will discuss those steps as we move forward in the article.

Hardware Requirements:

ESP32 Development board

Software Requirements:

Arduino IDE

Specific Arduino Libraries

Telegram App

Step 2: Making a Telegram Bot

As we discussed earlier, Telegram allows us to create several bots with different functionalities. For our project, we will be creating a simple bot using Telegram. The rest of the command declarations and replies will be coded in the ESP board itself, which will communicate to our bot using the chat ID. We will discuss those as we proceed further in the article. As that out of the way, we can turn our focus to build the bot in Telegram.

  • Install Telegram from Play Store.
  • After installing, make an account if you don’t have one already and follow the next steps to get your TG bot working.
  • First, search for “botfather” and click the BotFather as shown below. Or open this link t.me/botfather on your smartphone.
  • Botfather is a pre-built Telegram bot that lets you create, manage, and delete your bots.
  • Click on the start button and choose /newbot Give your bot a name and username.
  • If your bot is successfully created, you’ll receive a message with a link to access your newly created bot and the bot token.
  • Bot token is a unique id that we will be using later to communicate with the bot.

Step 3: Get Your Telegram Chat Id

A telegram user id is a unique number for every chat, group, and user which helps Telegram to identify users and chats. In this project of ours, anyone with the link to the bot may interact with the bot. To prevent any unauthorized access, we can encrypt it using the unique user id.

By doing so, every time ESP receives a message from the bot, it checks whether the id corresponds with the id stored in it and then only executes the commands.

Steps for getting your Telegram User ID:

  • In your Telegram account, search for “IDBot” or open this link t.me/myidbot on your smartphone.
  • Start a conversation with that bot and type /getid. You will get a reply back with your user ID.
  • Note the user id as we’ll need it later.

Step 4: Installing the Telegram Bot Library

We will be using Arduino IDE for programming the ESP32 board. So, make sure you have the IDE as well as the board package installed on your PC. We need to install two libraries in the Arduino IDE, using these libraries will make the coding process a lot simpler and convenient.

To establish communication with the Telegram bot, we’ll be using the Universal Telegram Bot Library created by Brian Lough that provides an easy interface for Telegram Bot API.

Follow the next steps to install the latest release of the library.

  • Go to Sketch > Include Library > Add.ZIP Library...
  • Add the library you’ve just downloaded. And that’s it.

The library is installed.

For details about the library, you can check out the Universal Arduino Telegram Bot Library GitHub page.

ArduinoJson Library:

You also have to install the ArduinoJson library. Follow the next steps to install the library.

  • Go to Sketch > Include Library > Manage Libraries.
  • Search for “ArduinoJson”.
  • Select the latest version available.
  • Install the library.

As we have done installing all the required libraries we can.

Step 5: Programming the ESP32 Based Telegram Bot

We have to flash our ESP32 in such a way that it receives any message being sent from the bot, compares the user id, and turn the LED on or off according to the received message.

  • In the very first of the code, we start with importing the required libraries.
  • After that, we initialize the variables to store the SSID and Password of your Wi-Fi.
  • Similarly, we define the variables for holding the bot token and chat id. You need to put your credentials in these variables.

Here you can find the complete code with an explanation.

Step 6:

Now that we have completed setting up everything. Upload the above-mentioned code to your ESP32 board through Arduino IDE. Don’t forget to choose the correct board and port while uploading the sketch.

After uploading the sketch, press the EN/Reset button on the board, so that it starts executing the code. Open the serial monitor, and wait for the board to get connected with your Wi-Fi router. Now, open Telegram and start a conversation with your bot by going to the link provided by Botfather and typing /start.

Now you can control the LED or know the state by typing the corresponding commands.

Step 7: ESP32 Based Telegram Bot - Working

You can also check out the video which demonstrates the working of this tutorial. If you have any questions, you can also leave them in the comment section below.

I hope you enjoyed the article and learned something useful. For more such interesting tutorials, please follow us on Instructables.

Be the First to Share

    Recommendations

    • Reclaimed Materials Contest

      Reclaimed Materials Contest
    • Hide It Challenge

      Hide It Challenge
    • Made with Math Contest

      Made with Math Contest

    Comments