#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Telegram botu baslatma ayralari
#define BOTtoken "7660524252:AAG8awWcfDppx2aRDRLKPyC7K7bno4UxH1o"
#define CHAT_ID "1613990157"
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
unsigned long bot_lasttime;
bool Start = false;
int value1 = 0;
int value2 = 0;
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";
// Find position of ',' and '='
int posA = text.indexOf("a=");
int posB = text.indexOf("b=");
if (posA != -1 && posB != -1) {
// Extract values of a and b
String value1String = text.substring(posA + 2, posB - 1);
String value2String = text.substring(posB + 2);
// Convert string values to integers
value1 = value1String.toInt();
value2 = value2String.toInt();
}
// Print extracted values for debugging
Serial.print("Value of a: ");
Serial.println(value1);
Serial.print("Value of b: ");
Serial.println(value2);
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";
bot.sendMessage(chat_id, welcome);
}
}
}
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Wifi Connected..");
}
// Print ESP32 Local IP Address
Serial.println(WiFi.localIP());
Serial.print("Retrieving time: ");
configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
time_t now = time(nullptr);
while (now < 24 * 3600)
{
Serial.print(".");
delay(100);
now = time(nullptr);
}
Serial.println(now);
bot.sendMessage(CHAT_ID, "ESP32 Started WokWi ");
}
void loop() {
if (millis() - bot_lasttime > 1000)
{
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();
}
}