#include <WiFi.h>
#include <UniversalTelegramBot.h>
#include <WiFiClientSecure.h>
const unsigned long BOT_MTBS = 1000; // mean time between scan messages
#define BOT_TOKEN "6176846745:AAHKysQFnpcCHlAv84oNtreNDVlDd46wlac"
#define CHAT_ID "1265427106"
//X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN ,secured_client);
unsigned long bot_lasttime; // last time messages' scan has been done
void handleNewMessages(int numNewMessages) {
Serial.print("Handle New Messages: ");
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 interact with the ESP32-CAM \n";
welcome += "/photo : takes a new photo\n";
welcome += "/send : send msg \n";
bot.sendMessage(CHAT_ID, welcome, "");
}
}
}
void setup(){
Serial.begin(115200);
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
configInitCamera();
WiFi.mode(WIFI_STA);
clientTCP.setCACert(TELEGRAM_CERTIFICATE_ROOT);
UniversalTelegramBot bot(BOT_TOKEN ,secured_client);
WiFi.begin("Wokwi-GUEST", "", 6);
Serial.print("connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print(".");
}
Serial.println("starting");
bot.sendMessage(CHAT_ID, "welcome", "");
bot_lasttime = millis();
}
void loop(){
if (millis() - bot_lasttime > BOT_MTBS)
{
Serial.println("-----------");
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
Serial.print(numNewMessages);
while (numNewMessages)
{
Serial.println("got response");
handleNewMessages(numNewMessages);
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
bot_lasttime = millis();
}
delay(100);
}