#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
#define BOT_TOKEN "7044080444:AAF8YOFlyI6GKLxwW3n7beel8G71fyL48Eg"
#define ledPin 14
#define ledPin2 12
bool ledState = LOW;
bool ledState2 = LOW;
const unsigned long BOT_MTBS = 1000; // mean time between scan messages
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
unsigned long bot_lasttime; // last time messages' scan has been done
bool Start = false;
void handleNewMessages(int numNewMessages)
{
Serial.println("handleNewMessages");
Serial.println(String(numNewMessages));
for (int i = 0; i < numNewMessages; i++)
{
String chat_id = 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 == "/send_test_action")
{
bot.sendChatAction(chat_id, "typing");
delay(4000);
bot.sendMessage(chat_id, "Did you see the action message?");
}
if (text == "/start")
{
String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
welcome += "This is Chat Action Bot example.\n\n";
welcome += "/send_test_action : to send test chat action message\n";
welcome += "/led_on : to Lamp On\n";
welcome += "/led_off : to Lamp Off\n";
welcome += "/led2_on : to Lamp On\n";
welcome += "/led2_off : to Lamp Off\n";
welcome += "/state : to check Lamp\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 == "/led2_on") {
bot.sendMessage(chat_id, "LED state set to ON", "");
ledState2 = HIGH;
digitalWrite(ledPin2, ledState2);
}
if (text == "/led2_off") {
bot.sendMessage(chat_id, "LED state set to OFF", "");
ledState2 = LOW;
digitalWrite(ledPin2, ledState2);
}
if (text == "/state") {
if (digitalRead(ledPin)){
bot.sendMessage(chat_id, "LED is ON", "");
}
else{
bot.sendMessage(chat_id, "LED is OFF", "");
}
}
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
WiFi.begin("Wokwi-GUEST", "", 6);
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
while (WiFi.status() != WL_CONNECTED) {
delay(250);
}
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
if (millis() - bot_lasttime > BOT_MTBS)
{
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);
}
bot_lasttime = millis();
}
}