Advertisement
safwan092

Untitled

Feb 7th, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. #include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
  2. #include <WiFi.h>
  3. #include <WiFiClientSecure.h>
  4. #include <UniversalTelegramBot.h>
  5.  
  6.  
  7. #define BOT_TOKEN "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
  8.  
  9. const unsigned long BOT_MTBS = 1000;
  10.  
  11. WiFiClientSecure secured_client;
  12. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  13. unsigned long bot_lasttime;
  14. bool Start = false;
  15.  
  16. void handleNewMessages(int numNewMessages)
  17. {
  18. Serial.println("handleNewMessages");
  19. Serial.println(String(numNewMessages));
  20.  
  21. for (int i = 0; i < numNewMessages; i++)
  22. {
  23. String chat_id = bot.messages[i].chat_id;
  24. String text = bot.messages[i].text;
  25.  
  26. String from_name = bot.messages[i].from_name;
  27. if (from_name == "")
  28. from_name = "Guest";
  29.  
  30. if (text == "/send_test_action")
  31. {
  32. bot.sendChatAction(chat_id, "typing");
  33. delay(4000);
  34. bot.sendMessage(chat_id, "Did you see the action message?");
  35. }
  36.  
  37. if (text == "/start")
  38. {
  39. String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
  40. welcome += "This is Chat Action Bot example.\n\n";
  41. welcome += "/send_test_action : to send test chat action message\n";
  42. bot.sendMessage(chat_id, welcome);
  43. }
  44. }
  45. }
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53. void setup() {
  54. Serial.begin(115200);
  55. Serial.println();
  56. Connect_To_WiFi();
  57. Connect_To_Telegram();
  58.  
  59. }
  60.  
  61. void loop() {
  62. ReConnect_To_WiFi();
  63. handel_messages_from_Telegram_Users();
  64.  
  65. }
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. void ReConnect_To_WiFi() {
  82. while (WiFi.status() != WL_CONNECTED) {
  83. //delay(500);
  84. Serial.print(".");
  85. Connect_To_WiFi();
  86. }
  87. }
  88.  
  89. void Connect_To_WiFi() {
  90. WiFiManager wm;
  91. bool res;
  92. res = wm.autoConnect("Senior Project [Setup WiFi]"); // password protected ap
  93.  
  94. if (!res) {
  95. Serial.println("Failed to Connect!!");
  96. // ESP.restart();
  97. }
  98. else {
  99. //if you get here you have connected to the WiFi
  100. Serial.println("Connected...");
  101. }
  102. }
  103.  
  104. void handel_messages_from_Telegram_Users() {
  105. if (millis() - bot_lasttime > BOT_MTBS)
  106. {
  107. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  108.  
  109. while (numNewMessages)
  110. {
  111. Serial.println("got response");
  112. handleNewMessages(numNewMessages);
  113. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  114. }
  115.  
  116. bot_lasttime = millis();
  117. }
  118. }
  119.  
  120.  
  121. void Connect_To_Telegram() {
  122. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  123. Serial.print("Retrieving time: ");
  124. configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  125. time_t now = time(nullptr);
  126. while (now < 24 * 3600)
  127. {
  128. Serial.print(".");
  129. delay(100);
  130. now = time(nullptr);
  131. }
  132. Serial.println(now);
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement