#include <WiFi.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
#include <WiFiClientSecure.h>
#include <FS.h>
#include <SPIFFS.h>
#define BOT_TOKEN "5711693392:AAEUASm_bSmfnqUdoef3hN8ZjUnp4dWHV6A"
const char* ssid = "Wokwi-GUEST";
const char* password = "";
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);
StaticJsonDocument<1024> doc;
void handleNewMessages(int numNewMessages);
void addUser(String id, String username, String flat, String chat_id);
void deleteUser(String id);
String listUsers();
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
if (!SPIFFS.begin(true)) {
Serial.println("An error occurred while mounting SPIFFS");
return;
}
pinMode(12, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
pinMode(2, OUTPUT);
//bot.begin();
File file = SPIFFS.open("/users.json", "r");
deserializeJson(doc, file);
file.close();
}
void loop() {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
if (numNewMessages) {
handleNewMessages(numNewMessages);
}
if (digitalRead(12) == LOW) {
String userList = listUsers();
for (JsonVariant user : doc.as<JsonArray>()) {
if (user["flat"] == "Flat 101") {
String message = "Button 1 pressed. Click the button below to turn on the LED.";
String buttonText = "LED On";
String callbackData = "led on";
String replyMarkup = "{\"inline_keyboard\":[[{\"text\":\"" + buttonText + "\",\"callback_data\":\"" + callbackData + "\"}]]}";
bot.sendMessage(user["chat_id"].as<String>(), message, replyMarkup);
}
}
}
if (digitalRead(13) == LOW) {
String userList = listUsers();
for (JsonVariant user : doc.as<JsonArray>()) {
if (user["flat"] == "Flat 202") {
String message = "Button 2 pressed. Click the button below to turn on the LED.";
String buttonText = "LED On";
String callbackData = "led on";
String replyMarkup = "{\"inline_keyboard\":[[{\"text\":\"" + buttonText + "\",\"callback_data\":\"" + callbackData + "\"}]]}";
bot.sendMessage(user["chat_id"].as<String>(), message, replyMarkup);
}
}
}
if (digitalRead(14) == LOW) {
String userList = listUsers();
for (JsonVariant user : doc.as<JsonArray>()) {
if (user["flat"] == "Flat 303") {
String message = "Button 3 pressed. Click the button below to turn on the LED.";
String buttonText = "LED On";
String callbackData = "led on";
String replyMarkup = "{\"inline_keyboard\":[[{\"text\":\"" + buttonText + "\",\"callback_data\":\"" + callbackData + "\"}]]}";
bot.sendMessage(user["chat_id"].as<String>(), message, replyMarkup);
}
}
}
delay(1000);
}
void handleNewMessages(int numNewMessages) {
for (int i = 0; i < numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
if (text.startsWith("/add")) {
String id = text.substring(5, text.indexOf(",", 5));
String username = text.substring(text.indexOf(",", 5) + 1, text.lastIndexOf(","));
String flat = text.substring(text.lastIndexOf(",") + 1, text.indexOf(",", text.lastIndexOf(",") + 1));
String chat_id = text.substring(text.indexOf(",", text.lastIndexOf(",") + 1) + 1);
addUser(id, username, flat, chat_id);
bot.sendMessage(chat_id, "User added successfully");
} else if (text.startsWith("/delete")) {
String id = text.substring(8);
deleteUser(id);
bot.sendMessage(chat_id, "User deleted successfully");
} else if (text.startsWith("/list")) {
String userList = listUsers();
bot.sendMessage(chat_id, userList);
} else if (text.startsWith("led on")) {
int ledPin = 2;
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
}
}
}
void addUser(String id, String username, String flat, String chat_id) {
File file = SPIFFS.open("/users.json", "r");
deserializeJson(doc, file);
file.close();
JsonObject newUser = doc.createNestedObject();
newUser["id"] = id;
newUser["username"] = username;
newUser["flat"] = flat;
newUser["chat_id"] = chat_id;
file = SPIFFS.open("/users.json", "w");
serializeJson(doc, file);
file.close();
}
void deleteUser(String id) {
File file = SPIFFS.open("/users.json", "r");
deserializeJson(doc, file);
file.close();
for (JsonVariant user : doc.as<JsonArray>()) {
if (user["id"] == id) {
user.clear();
break;
}
}
file = SPIFFS.open("/users.json", "w");
serializeJson(doc, file);
file.close();
}
String listUsers() {
File file = SPIFFS.open("/users.json", "r");
deserializeJson(doc, file);
file.close();
String userList = "";
for (JsonVariant user : doc.as<JsonArray>()) {
userList += "ID: " + user["id"].as<String>() + "\n";
userList += "Username: " + user["username"].as<String>() + "\n";
userList += "Flat: " + user["flat"].as<String>() + "\n";
userList += "Chat ID: " + user["chat_id"].as<String>() + "\n\n";
}
return userList;
}