Universal Telegram Bot Library

repository·master·Indexed 22 days ago

https://github.com/witnessmenow/universal-arduino-telegram-bot

An architecture-agnostic Arduino IDE library for interfacing with the Telegram Bot API. It supports ESP32 and ESP8266 boards, enabling features such as sending and receiving messages, implementing Reply and Inline Keyboards, managing chat actions, and handling long polling. The library requires the ArduinoJson library by Benoît Blanchon as a dependency.

Tokens
3.1K
Snippets
8
Records
33
Agent score
78%

What's inside UniversalTelegramBot

  1. Update inline keyboard messages and buttons

    master
    You can update existing messages in Telegram by using the message_id of the specific message received. This allows you to dynamically change the text content of a message or update the buttons within an inline_keyboard. This pattern is useful for building interactive menus (similar to BotFather) where a single message evolves based on user interaction, such as toggling the state of a device (e.g., an LED).
  2. Initialize the UniversalTelegramBot in Arduino

    master

    To start using the library, include the header file and instantiate the UniversalTelegramBot object. The constructor requires your Bot Token and an SSL Client (such as WiFiClientSecure) to handle the HTTPS requests required by the Telegram Bot API.

    #include <UniversalTelegramBot.h>
    
    // Telegram BOT Token (Get from Botfather)
    #define BOT_TOKEN "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    
    WiFiClientSecure secured_client;
    UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  3. Install the Universal Telegram Bot Library

    master

    To install the library in the Arduino IDE, download the source code and use the following menu path:

    Sketch / include Library / Add .Zip library

    Dependency Requirement: You must also install the ArduinoJson library by Benoît Blanchon. You can find this in the Arduino Library Manager or download it from its official repository.

    Sketch / include Library / Add .Zip library
  4. Handle incoming updates and messages

    master

    To receive messages from users, call getUpdates(long offset). This method populates the messages array with telegramMessage structs.

    Each telegramMessage contains:

    • text: The message content.
    • chat_id: The ID of the chat.
    • from_id: The ID of the sender.
    • from_name: The name of the sender.
    • message_id: The unique ID of the message.
    • reply_to_message_id: The ID of the message this is replying to (if applicable).
  5. Configure Long Polling

    master

    By default, the bot checks for messages immediately. You can use longPoll to set how many seconds the bot should wait for a new message before returning. This reduces the number of requests and data usage, but it will block the Arduino while waiting.

    Example: bot.longPoll = 60; sets a 60-second wait time.

    bot.longPoll = 60;
  6. Send bulk messages to all subscribed users on ESP8266

    master

    This example demonstrates how to use the UniversalTelegramBot library on ESP8266-based boards to broadcast a message to every user who has previously interacted with your bot (e.g., by sending /start).

    Important Considerations:

    • Scalability: This method is suitable for small communities (e.g., friends or family). For large-scale deployments (e.g., 10,000+ users), it is recommended to use an external database to manage user IDs rather than relying on local storage or simple iteration.
    • Configuration: You must provide your WiFi credentials (SSID, password) and your Telegram Bot token for the example to function.