Advertisement
safwan092

Untitled

Jul 15th, 2023
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. /*******************************************************************
  2. A telegram bot for your ESP32 that demonstrates a bot
  3. that show bot action message
  4.  
  5. Parts:
  6. ESP32 D1 Mini stlye Dev board* - http://s.click.aliexpress.com/e/C6ds4my
  7. (or any ESP32 board)
  8.  
  9. = Affilate
  10.  
  11. If you find what I do useful and would like to support me,
  12. please consider becoming a sponsor on Github
  13. https://github.com/sponsors/witnessmenow/
  14.  
  15. Example originally written by Vadim Sinitski
  16.  
  17. Library written by Brian Lough
  18. YouTube: https://www.youtube.com/brianlough
  19. Tindie: https://www.tindie.com/stores/brianlough/
  20. Twitter: https://twitter.com/witnessmenow
  21. *******************************************************************/
  22. #include <WiFi.h>
  23. #include <WiFiClientSecure.h>
  24. #include <UniversalTelegramBot.h>
  25.  
  26. // Wifi network station credentials
  27. #define WIFI_SSID "YOUR_SSID"
  28. #define WIFI_PASSWORD "YOUR_PASSWORD"
  29. // Telegram BOT Token (Get from Botfather)
  30. #define BOT_TOKEN "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
  31.  
  32. const unsigned long BOT_MTBS = 1000; // mean time between scan messages
  33.  
  34. WiFiClientSecure secured_client;
  35. UniversalTelegramBot bot(BOT_TOKEN, secured_client);
  36. unsigned long bot_lasttime; // last time messages' scan has been done
  37. bool Start = false;
  38.  
  39. void handleNewMessages(int numNewMessages)
  40. {
  41. Serial.println("handleNewMessages");
  42. Serial.println(String(numNewMessages));
  43.  
  44. for (int i = 0; i < numNewMessages; i++)
  45. {
  46. String chat_id = bot.messages[i].chat_id;
  47. String text = bot.messages[i].text;
  48.  
  49. String from_name = bot.messages[i].from_name;
  50. if (from_name == "")
  51. from_name = "Guest";
  52.  
  53. if (text == "/send_test_action")
  54. {
  55. bot.sendChatAction(chat_id, "typing");
  56. delay(4000);
  57. bot.sendMessage(chat_id, "Did you see the action message?");
  58.  
  59. // You can't use own message, just choose from one of bellow
  60.  
  61. //typing for text messages
  62. //upload_photo for photos
  63. //record_video or upload_video for videos
  64. //record_audio or upload_audio for audio files
  65. //upload_document for general files
  66. //find_location for location data
  67.  
  68. //more info here - https://core.telegram.org/bots/api#sendchataction
  69. }
  70.  
  71. if (text == "/start")
  72. {
  73. String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
  74. welcome += "This is Chat Action Bot example.\n\n";
  75. welcome += "/send_test_action : to send test chat action message\n";
  76. bot.sendMessage(chat_id, welcome);
  77. }
  78. }
  79. }
  80.  
  81. void setup()
  82. {
  83. Serial.begin(115200);
  84. Serial.println();
  85.  
  86. // attempt to connect to Wifi network:
  87. Serial.print("Connecting to Wifi SSID ");
  88. Serial.print(WIFI_SSID);
  89. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  90. secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
  91. while (WiFi.status() != WL_CONNECTED)
  92. {
  93. Serial.print(".");
  94. delay(500);
  95. }
  96. Serial.print("\nWiFi connected. IP address: ");
  97. Serial.println(WiFi.localIP());
  98.  
  99. Serial.print("Retrieving time: ");
  100. configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
  101. time_t now = time(nullptr);
  102. while (now < 24 * 3600)
  103. {
  104. Serial.print(".");
  105. delay(100);
  106. now = time(nullptr);
  107. }
  108. Serial.println(now);
  109. }
  110.  
  111. void loop()
  112. {
  113. if (millis() - bot_lasttime > BOT_MTBS)
  114. {
  115. int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  116.  
  117. while (numNewMessages)
  118. {
  119. Serial.println("got response");
  120. handleNewMessages(numNewMessages);
  121. numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  122. }
  123.  
  124. bot_lasttime = millis();
  125. }
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement