Experimentation's with a TelegramBot polling an ESP32.

These Bots can be found or created via the Telegram App Messaging service...

(in our case I have called it "MRLbot" )

MRLbot & ESP32 Marriage possibilities :-

  1. Send Text Messages remotely (both ways)
  2. Send Pictures .... (both ways, using Spiffs or SD card)
  3. Send Controls and Status messages (both ways)
  4. Control GPIO pins both Input&output

Creation of a Bot is achieved Via an entity/user called "Botfather" who you can find by searching persons in the search bar of the Telegram App (its simple to set up and process/guidance is well documented).

Once the TelegramBot is created the ESP32 needs the Telegram Library (I recommend Brian Lough's)

During the Bot registration process you will be sent a BOTtoken which is unique to your Bot and should be kept safe .... unless you would want multiple persons to use it !!!

Here is some code for controlling 3 Led's and sending Hall_Effect Sensor values back

// An example of bot that receives commands and turns on and off RGB LED
// Also sends back Hall_Effect Sensor Values of ESP32 
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>

// Initialize Wifi connection to the router
char ssid[] = "YOUR WIFI NETWORK";     // your network SSID (name)
char password[] = "YOUR NETWORK PASSWORD"; // your network key

// Initialize Telegram BOT
#define BOTtoken "THIS IS OBTAINED WHEN YOU REGISTER YOUR BOT - DO NOT SHARE PUBLICLY"  // your Bot Token (Get from Botfather)

WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);

int Bot_mtbs = 1000; //mean time between scan messages
long Bot_lasttime;   //last time messages' scan has been done
bool Start = false;

const int RedledPin = 18;
const int GreenledPin = 19;
const int BlueledPin = 21;
int val=0;

int RedledStatus = 0;
int GreenledStatus = 0;
int BlueledStatus = 0;

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);
    String text = bot.messages[i].text;

    String from_name = bot.messages[i].from_name;
    if (from_name == "") from_name = "Guest";

    if (text == "/Redledon") {digitalWrite(RedledPin, HIGH);  RedledStatus = 1;   bot.sendMessage(chat_id, "Red Led is ON", "");  }
    if (text == "/Redledoff"){ RedledStatus = 0; digitalWrite(RedledPin, LOW);       bot.sendMessage(chat_id, "Red Led is OFF", "");  }
    if (text == "/Redstatus"){ if(RedledStatus){ bot.sendMessage(chat_id, "Red Led is ON", ""); }  else { bot.sendMessage(chat_id, "Red Led is OFF", ""); } }
    if (text == "/Greenledon") {digitalWrite(GreenledPin, HIGH);  GreenledStatus = 1;   bot.sendMessage(chat_id, "Green Led is ON", "");  }
    if (text == "/Greenledoff"){ GreenledStatus = 0; digitalWrite(GreenledPin, LOW);       bot.sendMessage(chat_id, "Green Led is OFF", "");  }
    if (text == "/Greenstatus"){ if(GreenledStatus){ bot.sendMessage(chat_id, "Green Led is ON", ""); }  else { bot.sendMessage(chat_id, "Green Led is OFF", ""); } }
    if (text == "/Blueledon") {digitalWrite(BlueledPin, HIGH);  BlueledStatus = 1;   bot.sendMessage(chat_id, "Blue Led is ON", "");  }
    if (text == "/Blueledoff"){ BlueledStatus = 0; digitalWrite(BlueledPin, LOW);       bot.sendMessage(chat_id, "Blue Led is OFF", "");  }
    if (text == "/Bluestatus"){ if(BlueledStatus){ bot.sendMessage(chat_id, "Green Led is ON", ""); }  else { bot.sendMessage(chat_id, "Blue Led is OFF", ""); } }

    if (text == "/start") {
      String welcome = "Welcome to Universal Arduino Telegram Bot library.\n";
      welcome += "This is Flash Led Bot example.\n\n";
      welcome += "/Redledon    :switch Red Led ON\n";
      welcome += "/Redledoff   :switch Red Led OFF\n";
      welcome += "/Redstatus   :status Red LED\n";
      welcome += "/Greenledon  :switch Green Led ON\n";
      welcome += "/Greenledoff :switch Green Led OFF\n";
      welcome += "/Greenstatus :status Green LED\n";  
      welcome += "/Blueledon   :switch Blue Led ON\n";
      welcome += "/Blueledoff  :switch Blue Led OFF\n";
      welcome += "/Bluestatus  :status Blue LED\n";
   
      bot.sendMessage(chat_id, welcome, "Markdown");
    }
  }
}

void setup() {
  Serial.begin(115200);

  // Attempt to connect to Wifi network:
  Serial.print("Connecting Wifi: ");
  Serial.println(ssid);

  // Set WiFi to station mode and disconnect from an AP if it was Previously
  // connected
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  pinMode(RedledPin, OUTPUT);  delay(10); digitalWrite(RedledPin, LOW);
  pinMode(GreenledPin, OUTPUT);  delay(10); digitalWrite(GreenledPin, LOW);
  pinMode(BlueledPin, OUTPUT);  delay(10); digitalWrite(BlueledPin, LOW);
}

void loop() {
  if (millis() > Bot_lasttime + Bot_mtbs)  {
    int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
    int val = hallRead(); Serial.println(val);
    String chat_id = String(bot.messages[0].chat_id);
if (val>10){bot.sendMessage(chat_id, String(val), ""); RedledStatus = 0;GreenledStatus = 0;BlueledStatus = 0;digitalWrite(RedledPin, LOW); digitalWrite(GreenledPin, LOW); digitalWrite(BlueledPin, LOW);}
    while(numNewMessages) {
      Serial.println("got response");
      handleNewMessages(numNewMessages);

      numNewMessages = bot.getUpdates(bot.last_message_received + 1);
    }

    Bot_lasttime = millis();
  }
}

Once uploaded the ESP32 will poll the Telegram server at intervals (in our case every second (Circa))

Then on your Phone/PC or Tablet via the Telegram App you can bring up the MRLbot and interact with it.

I have extended the Command list to be able to control a RGB led.

Typing "/start" brings up the list of commands


A while back I was using the ESP32's inbuilt Magnetic Hall Effect Sensor... this can be read and also the analoge values sent back to your Telegram Chat.

(Could easily be used as a simple intruder alert (Magnet on door)  ;-)


Another neat parameter is the ability to include some json formating for custom keyboard buttons :-


In this example you can store a list of URL pictures on your ESP and recall them via the Telegram App

String test_photo_url = "http://myrobotlab.org/sites/default/files/logo.png&quot;;

if (text == "/get_test_photo") { bot.sendPhoto(chat_id, test_photo_url, "");  }


I am awaiting some more ESP32_Cams to be able to send Polled camera picture back to my Chat.,, Stay Tuned.

(N.B. Telegram gives you a LOT of control over what happens to your messages, tons more control than others that will remain nameless).