Advertisement
ajangrahmat

UniversalTelegramBot - Echo Bot

Apr 12th, 2020
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClientSecure.h>
  3. #include <UniversalTelegramBot.h>
  4. char ssid[] = "XXXXXXX";
  5. char password[] = "XXXXX";
  6. #define BOTtoken "XXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
  7. WiFiClientSecure client;
  8. UniversalTelegramBot bot(BOTtoken, client);
  9. int Bot_mtbs = 1000;
  10. long Bot_lasttime;
  11.  
  12. void setup() {
  13.   Serial.begin(115200);
  14.   WiFi.mode(WIFI_STA);
  15.   WiFi.disconnect();
  16.   delay(100);
  17.  
  18.   Serial.print("Connecting Wifi: ");
  19.   Serial.println(ssid);
  20.   WiFi.begin(ssid, password);
  21.   while (WiFi.status() != WL_CONNECTED) {
  22.     Serial.print(".");
  23.     delay(500);
  24.   }
  25.   client.setInsecure();
  26.  
  27.   Serial.println("");
  28.   Serial.println("WiFi connected");
  29.   Serial.print("IP address: ");
  30.   Serial.println(WiFi.localIP());
  31. }
  32.  
  33. void loop() {
  34.   if (millis() > Bot_lasttime + Bot_mtbs)  {
  35.     int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  36.     while (numNewMessages) {
  37.       Serial.println("got response");
  38.       for (int i = 0; i < numNewMessages; i++) {
  39.         Serial.println(bot.messages[i].text);
  40.         bot.sendMessage(bot.messages[i].chat_id, bot.messages[i].text, "");
  41.       }
  42.       numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  43.     }
  44.     Bot_lasttime = millis();
  45.   }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement