#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
#define BOT_TOKEN "8211036391:AAEU0q16zuM-43Ahw5-CR70VrnCx07mFe4k"
#define CHAT_ID "905866858"
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);
const int ledPin = 4;
// Keyboard custom
String keyboardJson = "[[\"/on\", \"/off\"], [\"/bantuan\"]]";
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW); // Pastikan LED mati saat start
// Connect to Wi-Fi
WiFi.begin(ssid, password);
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("\nConnected to WiFi");
// Kirim pesan awal dengan keyboard
String welcomeMsg = "🤖 *LED Control Bot* 🤖\n";
welcomeMsg += "Gunakan tombol di bawah atau ketik perintah:\n";
welcomeMsg += "• /on - Nyalakan LED\n";
welcomeMsg += "• /off - Matikan LED\n";
welcomeMsg += "• /bantuan - Tampilkan pesan bantuan\n";
bot.sendMessageWithReplyKeyboard(CHAT_ID, welcomeMsg, "", keyboardJson, true);
}
void handleMessage(String chat_id, String text) {
text.toLowerCase();
if (text == "/on" || text == "on") {
digitalWrite(ledPin, HIGH);
bot.sendMessage(chat_id, "🔴 LED sekarang NYALA", "");
}
else if (text == "/off" || text == "off") {
digitalWrite(ledPin, LOW);
bot.sendMessage(chat_id, "⚪ LED sekarang MATI", "");
}
else if (text == "/start" || text == "/help" || text == "help") {
String helpMsg = "🛠 *Panduan Penggunaan* 🛠\n\n";
helpMsg += "Anda dapat mengontrol LED dengan:\n";
helpMsg += "• Tombol keyboard yang tersedia\n";
helpMsg += "• Atau dengan perintah text:\n";
helpMsg += " - /on atau 'on' untuk menyalakan\n";
helpMsg += " - /off atau 'off' untuk mematikan\n\n";
helpMsg += "Status LED saat ini: ";
helpMsg += digitalRead(ledPin) ? "NYALA 🔴" : "MATI ⚪";
bot.sendMessageWithReplyKeyboard(chat_id, helpMsg, "", keyboardJson, true);
}
else {
bot.sendMessage(chat_id, "Perintah tidak dikenali. Ketik /help untuk bantuan", "");
}
}
void loop() {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
while(numNewMessages) {
for (int i=0; i<numNewMessages; i++) {
handleMessage(String(bot.messages[i].chat_id), bot.messages[i].text);
}
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
delay(100);
}