In this article I will show you how to make an ESP32 send a Telegram Message to the Telegram app on your mobile phone or computer. We also will add a Passive Infrared Sensor (PIR) to the ESP32, which then can be used to send an intruder alarm notification, if motion is detected.
Required Parts
Below you will find the components required. For this project, I used an older ESP32 board, which has been deprecated but you can still get it for a very low price. However, any other ESP32 will work just fine as well.
I listed two different types of motion sensors. If you want a small size go with the AM312. But if you want to detect motion only at night, go with the HC-SR501, since it can easily be equipped with a light sensor and also has a larger detection distance.

ESP32 lite

USB Data Cable

Dupont Wire Set

Breadboard

Motion Sensor

HC-SR501 PIR Motion Sensor
Makerguides is a participant in affiliate advertising programs designed to provide a means for sites to earn advertising fees by linking to Amazon, AliExpress, Elecrow, and other sites. As an Affiliate we may earn from qualifying purchases.
Telegram Messenger
Telegram Messenger is a cloud-based messaging app. You can easily install it in your smartphone (Android and iPhone) or computer (PC, Mac and Linux). It is completely free and also has no annoying ads.
It allows you to send messages, photos, videos, voice notes, files, and make voice or video calls. A standout feature is its cloud storage system, which enables you to access their chats from multiple devices seamlessly. Telegram also supports large group chats, public broadcast channels, and bots, making it popular for both personal and community-based communication.
When compared to WhatsApp or Signal, Telegram is generally more flexible and developer-friendly. WhatsApp is end-to-end encrypted by default and widely used but has limited bot support and fewer automation options. Signal is the most secure in terms of privacy but lacks the APIs and bot systems that make Telegram suitable for hardware integration. For receiving messages from an ESP32, Telegram is the easiest platform to use because of its well-documented Bot API, which allows the ESP32 to send messages via HTTPS with minimal setup.
Install Telegram
Installing of Telegram is simple. Just go to your Google Play or App Store and download and install the Telegram app. You can also install it on Windows as a desktop application.

Create Telegram Bot
To receive messages sent from an ESP32 on your Telegram app, we first need to create a Bot. Bots are third-party applications that run inside Telegram. Users can interact with bots by sending them messages, commands and inline requests.
We will use the BotFather bot to create a new bot that will receive our messages. Open Telegram on your mobile phone and search for “botfather”. Then click the BotFather bot as shown below or open the link t.me/botfather.

Make sure that you pick the correct bot. There are similar named bots you want to avoid. Look for this image to be sure or use the provided link:

If you select and click on the BotFather Bot the following message should appear. Press the /start button (marked red):

Next type /newbot and follow the instructions to create your bot. You have to provide a name, e.g. “My_ESP32_receiver” and a username that must end in ‘bot’, e.g. “My_ESP32_receiver_bot”.

Many names are already taken and you probably will have to try several times until you find a name that works. Once your bot is created you will see a message with a link to access the bot and the bot token.
The bot token is a string that looks like this: 1234567811:fakeIodw9023sjjoj982qe23dSSDCDDDSSxx but will be different for your bot. Make sure to save the bot token – we will need it later. Best copy it on your phone and share it with yourself via email.

Get Telegram User ID
Next we need to get our Telegram User ID and we will use another bot for this. Open Telegram and search for “IDBot” or open this link t.me/myidbot:

You will get a message explaining what this bot does. Reply to this message by typing /getid. You will get a reply with your user ID, e.g. ‘7221435846’. As before make sure that you safe the user ID, since we will need it later.

Now we have all the information we need. In the next section we write the code that sends a message from the ESP32 to your bot.
Code to send Telegram Messages
We will use the UniversalTelegramBot Library to simply sending Telegram messages from the ESP32. To install this library open the LIBRARY MANAGER of the Arduino IDE, search for ‘UniversalTelegramBot’ and then click the INSTALL button. After a successful installation you should see the installed library:

Send simple Message
In this first code example we send a simple, fixed message to our Telegram bot as test. Below is the complete code. Have a quick look first and then we discuss the details.
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
const char* SSID = "xxxx";
const char* PASSWORD = "xxxx";
const String BOT_TOKEN = "xxxxxxxx:xxxxxxxxxxxxxxxxxxxx";
const String CHAT_ID = "xxxx";
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);
void setup() {
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) delay(100);
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
bot.sendMessage(CHAT_ID, "Message-1", "");
}
void loop() {}
The code starts by including the required libraries for WiFi and Telegram communication:
#include <WiFi.h> #include <WiFiClientSecure.h> #include <UniversalTelegramBot.h>
Next we define the constants SSID and PASSWORD for the WiFi authentication and the constants BOT_TOKEN and CHAT_ID for the Telegram app. For CHAT_ID you have to use the User ID you’ve got from the IDBot and the BOT_TOKEN you got from the GodFather Bot for your bot.
const char* SSID = "xxxx"; const char* PASSWORD = "xxxx"; const String BOT_TOKEN = "xxxxxxxx:xxxxxxxxxxxxxxxxxxxx"; const String CHAT_ID = "xxxx";
With the constants defined, we then create the objects for WiFi and Telegram communication:
WiFiClientSecure client; UniversalTelegramBot bot(BOT_TOKEN, client);
Finally, we implement the setup function, where we first establish the WiFi communication. We then set the certificate for a secureTelegram communication and send the message ‘Message-1’ by calling bot.sendMessage():
void setup() {
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) delay(100);
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
bot.sendMessage(CHAT_ID, "Message-1", "");
}
With this code, every time you reset your ESP32 the same message ‘Message-1’ will get transmitted to your Telegram bot.
The loop function remains empty. Here you could send messages repeatedly but make sure to have a delay in sending otherwise your Telegram bot will get flooded with messages!
Send Message with Timestamp
Often you want to have your message timestamped, so that you know when it was send. The following code is a small extension of the previous code that adds a timestamp to the message:
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <time.h>
const char* SSID = "xxxx";
const char* PASSWORD = "xxxx";
const String BOT_TOKEN = "xxxxxxxx:xxxxxxxxxxxxxxxxxxxx";
const String CHAT_ID = "xxxx";
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);
void sendMessage() {
struct tm t;
if (getLocalTime(&t)) {
char msg[64];
strftime(msg, sizeof(msg), "Message@%H:%M:%S", &t);
bot.sendMessage(CHAT_ID, msg, "");
}
}
void setup() {
configTime(0, 0, "pool.ntp.org"); // UTC
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) delay(100);
sendMessage();
}
void loop() { }
The main differences are the sendMessage() function and in the setup. The sendMessage() function gets the local time, formats a string with timestamp and sends the message:
void sendMessage() {
struct tm t;
if (getLocalTime(&t)) {
char msg[64];
strftime(msg, sizeof(msg), "Message@%H:%M:%S", &t);
bot.sendMessage(CHAT_ID, msg, "");
}
}
The message will look like ‘Message@12:31:00’, for instance. You can change the message text or the timestamp format to your liking. Have a look at the documentation for the strftime function.
In the setup function we first configure the clock of the ESP32 to receive the time from an internet time service provider by calling configTime(). In this example the clock is set to UTC time but you can set your own time zone.
void setup() {
configTime(0, 0, "pool.ntp.org"); // UTC
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) delay(100);
sendMessage();
}
If you want to learn more about clock synchronization and time string formats, have a look at the How to synchronize ESP32 clock with SNTP server and maybe the Digital Clock on e-Paper Display tutorials.
Next we set the certificate for secure Telegram communication and establish the WiFi connection. Finally, we send the message by calling sendMessage().
With this code, you get a new, timestamped message every time you reset your ESP32.
Send Motion Alert Message
In the final code example we are going send an alert message to our Telegram bot if our ESP32 detects motion. This will require us to connect a motion sensor to the ESP32. The following picture shows you, how to connect the AM312 PIR sensor to the ESP32 lite:

Start by connecting the minus pin (-) of the AM312 to the ground pin (G) of the ESP32 and the plus pin (+) to the 3V pin. I connected the signal pin (s) of the AM312 to pin 17 but you can use pretty much any other pin as well. Just make sure to adjust the code below accordingly.
If you want to learn more about motion activation have a look at our How to Build a Motion Activated Night Light and the How to use HC-SR501 PIR Motion Sensor with Arduino tutorials.
The following code sends a timestamped alert message when the PIR sensor connected to the ESP32 detects and motion and then waits for one hour before the next alert can be triggered:
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <time.h>
const char* SSID = "xxxx";
const char* PASSWORD = "xxxx";
const String BOT_TOKEN = "xxxxxxxx:xxxxxxxxxxxxxxxxxxxx";
const String CHAT_ID = "xxxx";
const int PIR_PIN = 17;
const int WAIT_TIME = 60 * 60 * 1000; // wait one hour
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);
void sendMessage() {
struct tm t;
if (getLocalTime(&t)) {
char msg[128];
strftime(msg, sizeof(msg), "Alert @ %Y-%m-%d %H:%M:%S", &t);
bot.sendMessage(CHAT_ID, msg, "");
}
}
void setup() {
pinMode(PIR_PIN, INPUT);
configTime(0, 0, "pool.ntp.org"); // UTC
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) delay(100);
}
void loop() {
if (digitalRead(PIR_PIN) == HIGH) {
sendMessage();
delay(WAIT_TIME);
}
}
The code is very similar to the previous examples. The only real difference is in the loop function, where we now check if the PIR sensor has any motion detected. If this is the case, we send the alert message and then wait for the WAIT_TIME to avoid frequent retriggering of the alert.
You can change the PIR_PIN constant to connect the sensor to a different pin, and the WAIT_TIME constant to change the minimum wait time between alerts. Don’t make the WAIT_TIME too small, however, otherwise your phone might get swamped with messages.
Conclusion
In this article you learned how to send a message to the Telegram app on your mobile phone from an ESP32.
We also connected a PIR sensor to the ESP32 to send alert messages when motion is detected. You could combine this function with a camera system to get notified when to check the surveillance camera stream or recordings. See the Surveillance Camera with ESP32-CAM, Take Photos with ESP32-CAM, and the Motion Activated ESP32-CAM tutorials on how to build such as system.
If you not only want to receive notifications but control the ESP32, e.g. switching on lights, by sending Telegram messages have a look at the Control ESP32 from Telegram tutorial.
If you have any question, feel free to leave them in the comment section.
Happy Tinkering ; )
Stefan is a professional software developer and researcher. He has worked in robotics, bioinformatics, image/audio processing and education at Siemens, IBM and Google. He specializes in AI and machine learning and has a keen interest in DIY projects involving Arduino and 3D printing.

