ESP32 BOT

--Resource --

1. Create a bot


# Open this bot in telegramhttps://telegram.me/botfather
# give the commands

/start

/newbot

# give a name to your bot
# you will get a token . SAVE IT!

# Get your Telegram chat idhttps://t.me/myidbot

# give the commands
# you will get an id . SAVE IT!

/getid

2. ESP32 Code


#include < WiFi.h >
    #include < WiFiClientSecure.h>
    #include < UniversalTelegramBot.h>
    #include < ArduinoJson.h>
    
    // Replace with your network credentials
    const char* ssid = "Your WiFi Name";
    const char* password = "Your WiFi Password";
    
    // your Bot Token (Get from Botfather)
    #define BOTtoken "BOT Token got from BotFather"
    #define CHAT_ID "Your chat id from IdBot"
    
    WiFiClientSecure client;
    UniversalTelegramBot bot(BOTtoken, client);
    
    int botRequestDelay = 1000;
    unsigned long lastTimeBotRan;
    const int ledPin = 2;
    bool ledState = LOW;
    
    void handleNewMessages(int numNewMessages) {
      Serial.println("handleNewMessages");
      Serial.println(String(numNewMessages));
      for (int i = 0; i < numNewMessages; i++) {
        String chat_id = String(bot.messages[i].chat_id);
        if (chat_id != CHAT_ID) {
          bot.sendMessage(chat_id, "Unauthorized user", "");
          continue;
        }
        // Print the received message
        String text = bot.messages[i].text;
        Serial.println(text);
        String from_name = bot.messages[i].from_name;
        if (text == "/start") {
          String welcome = "Welcome, " + from_name + ".\n";
          welcome += "Use the following commands to control your outputs.\n\n";
          welcome += "/led_on to turn GPIO ON \n";
          welcome += "/led_off to turn GPIO OFF \n";
          welcome += "/state to request current GPIO state \n";
          bot.sendMessage(chat_id, welcome, "");
        }
        if (text == "/led_on") {
          bot.sendMessage(chat_id, "LED state set to ON", "");
          ledState = HIGH;
          digitalWrite(ledPin, ledState);
        }
        if (text == "/led_off") {
          bot.sendMessage(chat_id, "LED state set to OFF", "");
          ledState = LOW;
          digitalWrite(ledPin, ledState);
        }
        if (text == "/state") {
          if (digitalRead(ledPin)) {
            bot.sendMessage(chat_id, "LED is ON", "");
          }
          else {
            bot.sendMessage(chat_id, "LED is OFF", "");
          }
        }
      }
    }
    
    void setup() {
      Serial.begin(115200);
      pinMode(ledPin, OUTPUT);
      digitalWrite(ledPin, ledState);
      WiFi.mode(WIFI_STA);
      WiFi.begin(ssid, password);
      WiFi.setSleep(false);
      while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.println("Connecting to WiFi..");
      }
      Serial.println(WiFi.localIP());
      client.setInsecure();
    }
    
    void loop() {
      if (millis() > lastTimeBotRan + botRequestDelay)  {
        int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
        while (numNewMessages) {
          Serial.println("got response");
          handleNewMessages(numNewMessages);
          numNewMessages = bot.getUpdates(bot.last_message_received + 1);
        }
        lastTimeBotRan = millis();
      }
    }


3. Use this commands in your Bot


/start

/led_on

/led_off

/state