Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a small bridge program between Telegram and an MQTT broker: the bridge receives bot messages over HTTPS, checks who sent them, and publishes approved commands; the ESP32 subscribes to those MQTT topics and can publish status replies. Telegram bots use HTTPS, while Espressif’s ESP-MQTT client supports publish/subscribe connections from ESP32 projects. This split keeps bot credentials off the microcontroller and gives you one place to authorize commands.

How the Telegram–MQTT–ESP32 setup works

Telegram does not send bot messages directly to an MQTT topic. A bridge process connects the two services:

  1. A user sends a command to a Telegram bot.
  2. A bridge running on a computer, server, or other reachable host receives the update using Telegram’s Bot API.
  3. The bridge verifies the sender and command, then publishes an allowed message to an MQTT broker.
  4. The ESP32 connects to the broker, subscribes to its command topic, and handles supported commands.
  5. If confirmation is needed, the ESP32 publishes a status message that the bridge can relay to Telegram.

This architecture follows Telegram’s description of bots as interfaces to code running on a server and Espressif’s documentation of the ESP32 MQTT client. It is a practical default, not the only possible implementation. See Telegram’s bot introduction and Espressif’s ESP-MQTT documentation.

What you need before building it

  • An ESP32 development board, with its exact model and pinout chosen to suit the project.
  • A Telegram bot created through @BotFather.
  • A bridge host that can reach Telegram over outbound HTTPS and the MQTT broker over the network.
  • An MQTT broker and credentials for both the bridge and ESP32.
  • A defined command and status vocabulary, plus any project-specific sensor or actuator hardware.

The title does not specify a sensor, relay, or other device to control, so there is no universal wiring diagram or GPIO assignment. Confirm the board’s pinout, voltage levels, power needs, and peripheral requirements before connecting hardware.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
ESP-WROOM-32 ESP32 ESP-32S Development Board 2.4GHz Dual-Mode WiFi + Bluetooth Dual Cores Microcontroller Processor Integrated with Antenna RF AMP Filter AP STA Compatible with Arduino IDE (3PCS)
  • 2.4GHz Dual Mode WiFi + Bluetooth Development Board
  • Support LWIP protocol, Freertos
  • SupportThree Modes: AP, STA, and AP+STA
  • Ultra-Low power consumption, Compatible with Arduino IDE
  • ESP32 is a safe, reliable, and scalable to a variety of applications

Choose how the bridge receives Telegram updates

Telegram offers two mutually exclusive ways to receive bot updates. Choose one for the bot; do not configure both at once. Telegram’s Bot API reference documents both methods and notes that updates are retained for no more than 24 hours: Bot API reference.

Method Network requirement Operational considerations
Long polling with getUpdates The bridge makes outbound HTTPS requests; it does not need a public inbound endpoint. Track the update offset and advance it after responses to avoid processing old updates again. Simpler for a small bridge that can run a polling loop.
Webhook Telegram must be able to POST updates to a reachable HTTPS endpoint. Validate Telegram’s configured secret_token in the X-Telegram-Bot-Api-Secret-Token header. Requires a working public endpoint and its ongoing availability.

If the bridge is offline longer than Telegram’s update-retention window, some updates may no longer be available. Monitor the bridge and define recovery behavior—for example, report that the service restarted and require the user to resend a command rather than implying that every missed action will be replayed.

Rank #2
Sale
ELEGOO 3PCS ESP-32 Dev Boards, ESP-WROOM-32, USB-C, WiFi Bluetooth 4.2
  • Dual-Core Performance Up to 240 MHz: Run sensor processing, wireless communication, automation logic and connected-device tasks on a 32-bit dual-core ESP32 platform designed for responsive embedded and IoT projects
  • Built-in Wi-Fi and Bluetooth 4.2: Connect to 2.4 GHz Wi-Fi networks or use Bluetooth Classic and BLE for wireless sensors, smart devices, remote controls, home automation and other connected projects
  • Flexible Power-Saving Modes: ESP32 power-management features support dynamic clock scaling and low-power operating modes, helping developers reduce energy use in compatible sensing, monitoring and connected-device applications, suitable for battery-powered Internet of Things (IoT) devices.
  • USB-C Programming with CP2102: Connect through USB-C for power, sketch uploads and serial monitoring, while GPIO, UART, SPI and I2C interfaces support sensors, displays, motor drivers and other modules (USB-C cable not included)
  • Over-the-Air Update Support: Configure OTA functionality through a compatible ESP-32 software framework to update deployed firmware over Wi-Fi without reconnecting the board by USB for every revision

Set up the bot and protect its token

  1. Create the bot using Telegram’s bot setup flow with @BotFather.
  2. Store the bot token in an environment variable or a secrets manager on the bridge host, not in published source code, firmware, or public logs.
  3. Configure the bridge to receive updates using either long polling or a webhook.
  4. Before publishing an MQTT command, allowlist the Telegram user or chat IDs authorized to control the device.
  5. Parse a deliberately small command vocabulary and reject unknown commands or out-of-range values.

Telegram warns: “Everyone who has your token will have full control over your bot.” Treat the token like a password: restrict access, redact it from logs, and replace it if exposed. The bot token is used in Bot API HTTPS requests, whose documented form is https://api.telegram.org/bot<token>/METHOD_NAME; see the Bot API reference.

Connect the ESP32 to MQTT securely

Configure the ESP32 as an MQTT client with a unique client identity and broker credentials. Use TLS for remote connections and configure certificate verification so the device can authenticate the broker; an encrypted connection without broker identity verification does not provide the same protection against an impostor broker. Espressif documents MQTT authentication and TCP, TLS, WebSocket, and secure WebSocket transports in its ESP-MQTT documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
ELEGOO ESP-32 Super Starter Kit with Tutorial Compatible with Arduino IDE
  • Powerful ESP-32 Board: Unlock the world of Internet of Things (IoT) and advanced electronics with the heart of this kit: the ESP-32 board. It features a powerful dual-core processor, integrated Wi-Fi and Bluetooth 4.2, making it perfect for building connected, smart devices that communicate with your phone or the cloud. It's fully compatible with the Arduino IDE for easy programming.
  • Super Starter Kit: This kit contains over 35 different modules and electronic components, including sensors, displays, motors, and input devices. From LEDs and buttons to an OLED screen, servo motor, and keypad, you have everything needed to explore a vast range of projects in one box.
  • Step by Step Online Tutorial: Jump right in with our detailed, beginner-friendly tutorial. Access 30+ projects with complete code, clear circuit diagrams, and step-by-step instructions. Learn the fundamentals of electronics, coding, and how to utilize the ESP-32's unique capabilities without any prior experience.
  • Hands-on Learning for All Skill Levels: Perfect for students, makers, engineers, and hobbyists. Start with basic circuits and coding, then progress to intermediate and advanced IoT applications. Build practical projects like weather stations, smart home controllers, remote-controlled devices, and interactive gadgets. The skills you learn are the foundation for real-world innovation.
  • Quality & Great Support: Elegoo is committed to quality. We provide a clear, detailed tutorial guide, refined code, and a well-organized component kit. All modules are carefully selected for reliability and ease of use. Our dedicated technical support team and active online community are ready to help you succeed in your learning journey.

Do not treat a public test broker as private or dependable production infrastructure. Choose a broker your bridge and board can reach, and configure matching credentials and trust settings on both clients.

Design command and status topics

Keep topics narrow and specific to the device or function, such as home/device-1/cmd and home/device-1/status. These are illustrative names, not required conventions. The bridge should publish only commands the firmware supports; the ESP32 should validate incoming payloads again rather than assuming that any broker message is safe.

Rank #4
ESP-WROOM-32 ESP32 ESP-32S Development Board 2.4GHz Dual-Mode WiFi + Bluetooth Dual Cores Microcontroller Processor Integrated with Antenna RF AMP Filter AP STA Compatible with Arduino IDE (1 PCS)
  • 2.4GHz Dual Mode WiFi + Bluetooth Development Board
  • Support LWIP protocol, Freertos;ESP32 is a safe, reliable, and scalable to a variety of applications
  • SupportThree Modes: AP, STA, and AP+STA
  • Ultra-Low power consumption, Compatible with Arduino IDE
  • 1PCS 30Pin ESP32 Development Board 2.4GHz WiFi Dual Cores Microcontroller Integrated with Antenna RF Low Noise Amplifiers Filters
  • Use a small, explicit command set, such as ON, OFF, or a bounded numeric setting relevant to the project.
  • Reject malformed payloads and values outside the hardware’s safe range.
  • Publish an acknowledgment or resulting state when the user needs confirmation.
  • Separate “command received” from “action completed”: MQTT message delivery alone cannot prove that a physical actuator actually moved or that a sensor reached a target.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Account for ESP-IDF and ESP-MQTT versions

The stable Espressif documentation states that ESP-MQTT moved out of ESP-IDF starting with ESP-IDF 6.0 and is added as the espressif/mqtt component using the Component Manager. Earlier ESP-IDF documentation included MQTT within the IDF distribution. Follow installation and API instructions for the exact ESP-IDF and component version used by your project; a tutorial that omits versions can leave readers with mismatched build steps.

Espressif also notes that esp_mqtt_client_publish can block for several seconds in some circumstances. Where the application must remain responsive, review the selected version’s API details and consider esp_mqtt_client_enqueue, which queues work for the MQTT task. Consult the ESP-MQTT API documentation for the version you install.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
HiLetgo ESP-WROOM-32 ESP32 ESP-32S Development Board 2.4GHz Dual-Mode WiFi + Bluetooth Dual Cores Microcontroller Processor Integrated with Antenna RF AMP Filter AP STA for Arduino IDE
  • 2.4GHz Dual Mode WiFi + Bluetooth Development Board
  • Ultra-Low power consumption, works perfectly with the Arduino IDE
  • Support LWIP protocol, Freertos
  • SupportThree Modes: AP, STA, and AP+STA
  • ESP32 is a safe, reliable, and scalable to a variety of applications

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.