|
| 1 | +#include <ESP8266WiFi.h> |
| 2 | +#include <ArduinoOTA.h> |
| 3 | +#include <UniversalTelegramBot.h> |
| 4 | + |
| 5 | +#define BOTtoken "1235351031:AAFy85Gc9y1raA9dYYSTe4S8_Yfr1J0D-lg" // "648272766:AAEkW5FaFMeHqWwuNBsZJckFEOdhlSVisEc" |
| 6 | + |
| 7 | +WiFiClientSecure client; |
| 8 | +UniversalTelegramBot bot(BOTtoken, client); |
| 9 | + |
| 10 | +int Bot_mtbs = 1000; //mean time between scan messages |
| 11 | +long Bot_lasttime; //last time messages' scan has been done |
| 12 | + |
| 13 | +void bot_setup() { |
| 14 | + client.setInsecure(); // Required for ESP8266 |
| 15 | + const String commands = F("[" |
| 16 | + "{\"command\":\"help\", \"description\":\"Get bot usage help\"}," |
| 17 | + "{\"command\":\"start\", \"description\":\"Message sent when you open a chat with a bot\"}," |
| 18 | + "{\"command\":\"status\",\"description\":\"Answer device current status\"}" // no comma on last command |
| 19 | + "]"); |
| 20 | + bot.setMyCommands(commands); |
| 21 | + bot.sendMessage("25235518", "Hola amigo!", "Markdown"); |
| 22 | +} |
| 23 | + |
| 24 | +void setup() { |
| 25 | + Serial.begin(115200); |
| 26 | + Serial.print("Connecting Wifi: "); |
| 27 | + WiFi.begin(); // Using stored credentials |
| 28 | + while (WiFi.status() != WL_CONNECTED) { |
| 29 | + Serial.print("."); |
| 30 | + delay(500); |
| 31 | + } |
| 32 | + Serial.println("\r\nWiFi connected"); |
| 33 | + |
| 34 | + ArduinoOTA.begin(); // You should set a password for OTA. Ideally using MD5 hashes |
| 35 | + bot_setup(); |
| 36 | +} |
| 37 | + |
| 38 | +void loop() { |
| 39 | + ArduinoOTA.handle(); // Only program on the serial port the first time. Then on WiFi! :-) |
| 40 | + |
| 41 | + if (millis() > Bot_lasttime + Bot_mtbs) { |
| 42 | + String answer; |
| 43 | + int numNewMessages = bot.getUpdates(bot.last_message_received + 1); |
| 44 | + |
| 45 | + for (int i = 0; i < numNewMessages; i++) { |
| 46 | + telegramMessage &msg = bot.messages[i]; |
| 47 | + Serial.println("Received " + msg.text); |
| 48 | + if (msg.text == "/help") answer = "So you need _help_, uh? me too! use /start or /status"; |
| 49 | + else if (msg.text == "/start") answer = "Welcome my new friend! You are the first *" + msg.from_name + "* I've ever met"; |
| 50 | + else if (msg.text == "/status") answer = "All is good here, thanks for asking!"; |
| 51 | + else answer = "Say what?"; |
| 52 | + |
| 53 | + bot.sendMessage(msg.chat_id, answer, "Markdown"); |
| 54 | + } |
| 55 | + |
| 56 | + Bot_lasttime = millis(); |
| 57 | + } |
| 58 | +} |
0 commit comments