|
| 1 | +/******************************************************************* |
| 2 | + * An example of how to use a bulk messages to subscribed users. * |
| 3 | + * * |
| 4 | + * written by Vadim Sinitski * |
| 5 | + *******************************************************************/ |
| 6 | +#include <ESP8266WiFi.h> |
| 7 | +#include <WiFiClientSecure.h> |
| 8 | +#include <UniversalTelegramBot.h> |
| 9 | +#include <ArduinoJson.h> |
| 10 | +#include <LittleFS.h> |
| 11 | + |
| 12 | +// Wifi network station credentials |
| 13 | +#define WIFI_SSID "YOUR_SSID" |
| 14 | +#define WIFI_PASSWORD "YOUR_PASSWORD" |
| 15 | +// Telegram BOT Token (Get from Botfather) |
| 16 | +#define BOT_TOKEN "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" |
| 17 | + |
| 18 | +const char *SUBSCRIBED_USERS_FILENAME = "/subscribed_users.json"; // Filename for local storage |
| 19 | +const unsigned long BULK_MESSAGES_MTBS = 1500; // Mean time between send messages, 1.5 seconds |
| 20 | +const unsigned int MESSAGES_LIMIT_PER_SECOND = 25; // Telegram API have limit for bulk messages ~30 messages per second |
| 21 | +const unsigned long BOT_MTBS = 1000; // Mean time between scan messages |
| 22 | + |
| 23 | +WiFiClientSecure secured_client; |
| 24 | +X509List cert(TELEGRAM_CERTIFICATE_ROOT); |
| 25 | +UniversalTelegramBot bot(BOT_TOKEN, secured_client); |
| 26 | +DynamicJsonDocument usersDoc(1500); |
| 27 | +unsigned long bot_lasttime; // last time messages' scan has been done |
| 28 | + |
| 29 | +JsonObject getSubscribedUsers() |
| 30 | +{ |
| 31 | + File subscribedUsersFile = LittleFS.open(SUBSCRIBED_USERS_FILENAME, "r"); |
| 32 | + JsonObject users; |
| 33 | + |
| 34 | + // no file |
| 35 | + if (!subscribedUsersFile) |
| 36 | + { |
| 37 | + Serial.println("Failed to open subscribed users file"); |
| 38 | + // Create empty file (w+ not working as expect) |
| 39 | + File f = LittleFS.open(SUBSCRIBED_USERS_FILENAME, "w"); |
| 40 | + f.close(); |
| 41 | + return users; |
| 42 | + } |
| 43 | + |
| 44 | + // too large file |
| 45 | + size_t size = subscribedUsersFile.size(); |
| 46 | + if (size > 1500) |
| 47 | + { |
| 48 | + subscribedUsersFile.close(); |
| 49 | + Serial.println("Subscribed users file is too large"); |
| 50 | + return users; |
| 51 | + } |
| 52 | + |
| 53 | + String file_content = subscribedUsersFile.readString(); |
| 54 | + subscribedUsersFile.close(); |
| 55 | + DeserializationError error = deserializeJson(usersDoc, file_content); |
| 56 | + if (error) |
| 57 | + { |
| 58 | + Serial.println("Failed to parse subscribed users file"); |
| 59 | + return users; |
| 60 | + } |
| 61 | + users = usersDoc.as<JsonObject>(); |
| 62 | + |
| 63 | + return users; |
| 64 | +} |
| 65 | + |
| 66 | +bool addSubscribedUser(String chat_id, String from_name) |
| 67 | +{ |
| 68 | + JsonObject users = getSubscribedUsers(); |
| 69 | + users[chat_id] = from_name; |
| 70 | + |
| 71 | + File subscribedUsersFile = LittleFS.open(SUBSCRIBED_USERS_FILENAME, "w+"); |
| 72 | + // file not available |
| 73 | + if (!subscribedUsersFile) |
| 74 | + { |
| 75 | + subscribedUsersFile.close(); |
| 76 | + Serial.println("Failed to open subscribed users file for writing"); |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + //users.getOrCreateMember(chat_id, from_name); |
| 81 | + serializeJson(users, subscribedUsersFile); |
| 82 | + subscribedUsersFile.close(); |
| 83 | + return true; |
| 84 | +} |
| 85 | + |
| 86 | +bool removeSubscribedUser(String chat_id) |
| 87 | +{ |
| 88 | + JsonObject users = getSubscribedUsers(); |
| 89 | + users.remove(chat_id); |
| 90 | + |
| 91 | + File subscribedUsersFile = LittleFS.open(SUBSCRIBED_USERS_FILENAME, "w"); |
| 92 | + // file not available |
| 93 | + if (!subscribedUsersFile) |
| 94 | + { |
| 95 | + subscribedUsersFile.close(); |
| 96 | + Serial.println("Failed to open subscribed users file for writing"); |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + serializeJson(users, subscribedUsersFile); |
| 101 | + subscribedUsersFile.close(); |
| 102 | + return true; |
| 103 | +} |
| 104 | + |
| 105 | +void sendMessageToAllSubscribedUsers(String message) |
| 106 | +{ |
| 107 | + JsonObject users = getSubscribedUsers(); |
| 108 | + unsigned int users_processed = 0; |
| 109 | + |
| 110 | + for (JsonObject::iterator it = users.begin(); it != users.end(); ++it) |
| 111 | + { |
| 112 | + users_processed++; |
| 113 | + if (users_processed < MESSAGES_LIMIT_PER_SECOND) |
| 114 | + { |
| 115 | + const char *chat_id = it->key().c_str(); |
| 116 | + bot.sendMessage(chat_id, message, ""); |
| 117 | + } |
| 118 | + else |
| 119 | + { |
| 120 | + delay(BULK_MESSAGES_MTBS); |
| 121 | + users_processed = 0; |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +void handleNewMessages(int numNewMessages) |
| 127 | +{ |
| 128 | + Serial.print("handleNewMessages: "); |
| 129 | + Serial.println(numNewMessages); |
| 130 | + |
| 131 | + for (int i = 0; i < numNewMessages; i++) |
| 132 | + { |
| 133 | + String chat_id = bot.messages[i].chat_id; |
| 134 | + String text = bot.messages[i].text; |
| 135 | + String from_name = bot.messages[i].from_name; |
| 136 | + if (from_name == "") |
| 137 | + from_name = "Guest"; |
| 138 | + |
| 139 | + if (text == "/start") |
| 140 | + { |
| 141 | + if (addSubscribedUser(chat_id, from_name)) |
| 142 | + { |
| 143 | + String welcome = "Welcome to Universal Arduino Telegram Bot library.\n"; |
| 144 | + welcome += "This is Bulk Messages example.\n\n"; |
| 145 | + welcome += "You, " + from_name + ", have been subscribed.\n"; |
| 146 | + welcome += "/showallusers : show all subscribed users\n"; |
| 147 | + welcome += "/testbulkmessage : send test message to subscribed users\n"; |
| 148 | + welcome += "/removeallusers : remove all subscribed users\n"; |
| 149 | + welcome += "/stop : unsubscribe from bot\n"; |
| 150 | + bot.sendMessage(chat_id, welcome, "Markdown"); |
| 151 | + } |
| 152 | + else |
| 153 | + { |
| 154 | + bot.sendMessage(chat_id, "Something wrong, please try again (later?)", ""); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + if (text == "/stop") |
| 159 | + { |
| 160 | + if (removeSubscribedUser(chat_id)) |
| 161 | + { |
| 162 | + bot.sendMessage(chat_id, "Thank you, " + from_name + ", we always waiting you back", ""); |
| 163 | + } |
| 164 | + else |
| 165 | + { |
| 166 | + bot.sendMessage(chat_id, "Something wrong, please try again (later?)", ""); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + if (text == "/testbulkmessage") |
| 171 | + { |
| 172 | + sendMessageToAllSubscribedUsers("ATTENTION, this is bulk message for all subscribed users!"); |
| 173 | + } |
| 174 | + |
| 175 | + if (text == "/showallusers") |
| 176 | + { |
| 177 | + File subscribedUsersFile = LittleFS.open(SUBSCRIBED_USERS_FILENAME, "r"); |
| 178 | + // no file |
| 179 | + if (!subscribedUsersFile) |
| 180 | + { |
| 181 | + bot.sendMessage(chat_id, "No subscription file", ""); |
| 182 | + return; |
| 183 | + } |
| 184 | + size_t size = subscribedUsersFile.size(); |
| 185 | + if (size > 1024) |
| 186 | + { |
| 187 | + bot.sendMessage(chat_id, "Subscribed users file is too large", ""); |
| 188 | + } |
| 189 | + else |
| 190 | + { |
| 191 | + String file_content = subscribedUsersFile.readString(); |
| 192 | + bot.sendMessage(chat_id, file_content, ""); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + if (text == "/removeallusers") |
| 197 | + { |
| 198 | + if (LittleFS.remove(SUBSCRIBED_USERS_FILENAME)) |
| 199 | + { |
| 200 | + bot.sendMessage(chat_id, "All users removed", ""); |
| 201 | + } |
| 202 | + else |
| 203 | + { |
| 204 | + bot.sendMessage(chat_id, "Something wrong, please try again (later?)", ""); |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +void setup() |
| 211 | +{ |
| 212 | + Serial.begin(115200); |
| 213 | + Serial.println(); |
| 214 | + |
| 215 | + if (!LittleFS.begin()) |
| 216 | + { |
| 217 | + Serial.println("Failed to mount file system"); |
| 218 | + return; |
| 219 | + } |
| 220 | + |
| 221 | + // attempt to connect to Wifi network: |
| 222 | + configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP |
| 223 | + secured_client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org |
| 224 | + Serial.print("Connecting to Wifi SSID "); |
| 225 | + Serial.print(WIFI_SSID); |
| 226 | + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); |
| 227 | + while (WiFi.status() != WL_CONNECTED) |
| 228 | + { |
| 229 | + Serial.print("."); |
| 230 | + delay(500); |
| 231 | + } |
| 232 | + Serial.print("\nWiFi connected. IP address: "); |
| 233 | + Serial.println(WiFi.localIP()); |
| 234 | + |
| 235 | + // Check NTP/Time, usually it is instantaneous and you can delete the code below. |
| 236 | + Serial.print("Retrieving time: "); |
| 237 | + time_t now = time(nullptr); |
| 238 | + while (now < 24 * 3600) |
| 239 | + { |
| 240 | + Serial.print("."); |
| 241 | + delay(100); |
| 242 | + now = time(nullptr); |
| 243 | + } |
| 244 | + Serial.println(now); |
| 245 | +} |
| 246 | + |
| 247 | +void loop() |
| 248 | +{ |
| 249 | + if (millis() - bot_lasttime > BOT_MTBS) |
| 250 | + { |
| 251 | + int numNewMessages = bot.getUpdates(bot.last_message_received + 1); |
| 252 | + |
| 253 | + while (numNewMessages) |
| 254 | + { |
| 255 | + Serial.println("got response"); |
| 256 | + handleNewMessages(numNewMessages); |
| 257 | + numNewMessages = bot.getUpdates(bot.last_message_received + 1); |
| 258 | + } |
| 259 | + |
| 260 | + bot_lasttime = millis(); |
| 261 | + } |
| 262 | +} |
0 commit comments