|
| 1 | +/******************************************************************* |
| 2 | + A telegram bot for your ESP32 that demonstrates sending an image |
| 3 | + from URL. |
| 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 | + |
| 23 | +// ---------------------------- |
| 24 | +// Standard ESP32 Libraries |
| 25 | +// ---------------------------- |
| 26 | + |
| 27 | +#include <WiFi.h> |
| 28 | +#include <WiFiClientSecure.h> |
| 29 | + |
| 30 | +// ---------------------------- |
| 31 | +// Additional Libraries - each one of these will need to be installed. |
| 32 | +// ---------------------------- |
| 33 | + |
| 34 | +#include <UniversalTelegramBot.h> |
| 35 | + |
| 36 | +#include <ArduinoJson.h> |
| 37 | +// Library used for parsing Json from the API responses |
| 38 | + |
| 39 | +// Search for "Arduino Json" in the Arduino Library manager |
| 40 | +// https://github.com/bblanchon/ArduinoJson |
| 41 | + |
| 42 | +// Initialize Wifi connection to the router |
| 43 | +char ssid[] = "XXXXXX"; // your network SSID (name) |
| 44 | +char password[] = "YYYYYY"; // your network key |
| 45 | + |
| 46 | +// Initialize Telegram BOT |
| 47 | +#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather) |
| 48 | + |
| 49 | +WiFiClientSecure client; |
| 50 | +UniversalTelegramBot bot(BOTtoken, client); |
| 51 | + |
| 52 | +//Checks for new messages every 1 second. |
| 53 | +int botRequestDelay = 1000; |
| 54 | +unsigned long lastTimeBotRan; |
| 55 | + |
| 56 | +String test_photo_url = "https://www.arduino.cc/en/uploads/Trademark/ArduinoCommunityLogo.png"; |
| 57 | + |
| 58 | +void handleNewMessages(int numNewMessages) { |
| 59 | + Serial.println("handleNewMessages"); |
| 60 | + Serial.println(String(numNewMessages)); |
| 61 | + |
| 62 | + for (int i=0; i<numNewMessages; i++) { |
| 63 | + String chat_id = String(bot.messages[i].chat_id); |
| 64 | + String text = bot.messages[i].text; |
| 65 | + |
| 66 | + String from_name = bot.messages[i].from_name; |
| 67 | + if (from_name == "") from_name = "Guest"; |
| 68 | + |
| 69 | + if (text == "/get_test_photo") { |
| 70 | + bot.sendPhoto(chat_id, test_photo_url, "Caption is optional, you may not use photo caption"); |
| 71 | + } |
| 72 | + |
| 73 | + if (text == "/start") { |
| 74 | + String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n"; |
| 75 | + welcome += "This is Send Image From URL example.\n\n"; |
| 76 | + welcome += "/get_test_photo : getting test photo\n"; |
| 77 | + |
| 78 | + bot.sendMessage(chat_id, welcome, ""); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +void setup() { |
| 84 | + Serial.begin(115200); |
| 85 | + |
| 86 | + // Set WiFi to station mode and disconnect from an AP if it was Previously connected |
| 87 | + WiFi.mode(WIFI_STA); |
| 88 | + WiFi.disconnect(); |
| 89 | + delay(100); |
| 90 | + |
| 91 | + // attempt to connect to Wifi network: |
| 92 | + Serial.print("Connecting Wifi: "); |
| 93 | + Serial.println(ssid); |
| 94 | + WiFi.begin(ssid, password); |
| 95 | + |
| 96 | + while (WiFi.status() != WL_CONNECTED) { |
| 97 | + Serial.print("."); |
| 98 | + delay(500); |
| 99 | + } |
| 100 | + |
| 101 | + Serial.println(""); |
| 102 | + Serial.println("WiFi connected"); |
| 103 | + Serial.print("IP address: "); |
| 104 | + Serial.println(WiFi.localIP()); |
| 105 | +} |
| 106 | + |
| 107 | +void loop() { |
| 108 | + if (millis() > lastTimeBotRan + botRequestDelay) { |
| 109 | + int numNewMessages = bot.getUpdates(bot.last_message_received + 1); |
| 110 | + |
| 111 | + while(numNewMessages) { |
| 112 | + Serial.println("got response"); |
| 113 | + handleNewMessages(numNewMessages); |
| 114 | + numNewMessages = bot.getUpdates(bot.last_message_received + 1); |
| 115 | + } |
| 116 | + |
| 117 | + lastTimeBotRan = millis(); |
| 118 | + } |
| 119 | +} |
0 commit comments