#include "CTBot.h"
CTBot myBot;
String ssid = "Wokwi-GUEST"
String pass = ""
String token = "7249668273:AAHe0xpo-RDpyfSH_Yd2GXl11egmbLz5Qbk";
uint8_t led = 2;
void setup() {
// initialize the Serial
Serial.begin(115200);
Serial.println("Starting TelegramBot...");
// connect the ESP32 to the desired access point
myBot.wifiConnect(ssid, pass);
// set the telegram bot token
myBot.setTelegramToken(token);
// check if all things are ok
if (myBot.testConnection())
Serial.println("\ntestConnection OK");
else
Serial.println("\ntestConnection NOK");
// set the pin connected to the LED to act as output pin
pinMode(led, OUTPUT);
digitalWrite(led, HIGH); // turn off the led (inverted logic!)
}
void loop() {
// a variable to store telegram message data
TBMessage msg;
// if there is an incoming message...
if (CTBotMessageText == myBot.getNewMessage(msg)) {
if (msg.text.equalsIgnoreCase("LIGHT ON")) { // if the received message is "LIGHT ON"...
digitalWrite(led, LOW); // turn on the LED (inverted logic!)
myBot.sendMessage(msg.sender.id, "Light is now ON"); // notify the sender
}
else if (msg.text.equalsIgnoreCase("LIGHT OFF")) { // if the received message is "LIGHT OFF"...
digitalWrite(led, HIGH); // turn off the led (inverted logic!)
myBot.sendMessage(msg.sender.id, "Light is now OFF"); // notify the sender
}
else { // otherwise...
// generate the message for the sender
String reply;
reply = (String)"Welcome " + msg.sender.username + (String)". Try LIGHT ON or LIGHT OFF.";
myBot.sendMessage(msg.sender.id, reply); // and send it
}
}
// wait 500 milliseconds
delay(500);
}