Skip to content

Commit 54ff625

Browse files
committed
Exmaple improved.
1 parent 0a7322d commit 54ff625

File tree

1 file changed

+142
-141
lines changed

1 file changed

+142
-141
lines changed
Lines changed: 142 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -1,218 +1,219 @@
11
/*******************************************************************
2-
* An example of how to use a bulk messages to subscribed users. *
3-
* *
4-
* *
5-
* written by Vadim Sinitski *
6-
*******************************************************************/
2+
* An example of how to use a bulk messages to subscribed users. *
3+
* *
4+
* *
5+
* written by Vadim Sinitski *
6+
*******************************************************************/
77
#include <ESP8266WiFi.h>
88
#include <WiFiClientSecure.h>
99
#include <UniversalTelegramBot.h>
1010
#include <FS.h>
1111
#include <ArduinoJson.h>
1212

1313
// Initialize Wifi connection to the router
14-
char ssid[] = "xxxxxxx"; // your network SSID (name)
14+
char ssid[] = "xxxxxxx"; // your network SSID (name)
1515
char password[] = "yyyyyyyy"; // your network key
1616

1717
// Initialize Telegram BOT
1818
#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
1919

20-
WiFiClientSecure client;
21-
UniversalTelegramBot bot(BOTtoken, client);
20+
WiFiClientSecure secured_client;
21+
UniversalTelegramBot bot(BOTtoken, secured_client);
2222

23-
int Bot_mtbs = 1000; //mean time between scan messages
24-
long Bot_lasttime; //last time messages' scan has been done
23+
int Bot_mtbs = 1000; // mean time between scan messages
24+
long Bot_lasttime; // last time messages' scan has been done
2525

26-
int bulk_messages_mtbs = 1500; //mean time between send messages, 1.5 seconds
27-
int messages_limit_per_second = 25; // Telegram API have limit for bulk messages ~30 messages in 1 second
26+
int bulk_messages_mtbs = 1500; // mean time between send messages, 1.5 seconds
27+
int messages_limit_per_second = 25; // Telegram API have limit for bulk messages ~30 messages per second
28+
29+
String subscribed_users_filename = "subscribed_users.json";
2830

2931
void handleNewMessages(int numNewMessages) {
30-
Serial.println("handleNewMessages");
31-
Serial.println(String(numNewMessages));
32-
33-
for(int i=0; i<numNewMessages; i++) {
34-
String chat_id = String(bot.messages[i].chat_id);
35-
String text = bot.messages[i].text;
36-
37-
if (text == "/start") {
38-
if (addSubscribedUser(chat_id)) {
39-
bot.sendMessage(chat_id, "Welcome!", "");
40-
} else {
41-
bot.sendMessage(chat_id, "Something wrong, please try again", "");
42-
}
43-
}
44-
45-
if (text == "/stop") {
46-
bot.sendMessage(chat_id, "Thank you, we always waiting you back", "");
47-
removeSubscribedUser(chat_id);
48-
}
49-
50-
if (text == "/testbulkmessage") {
51-
sendMessageToAllSubscribedUsers("Current temperature is 0.0C");
52-
}
53-
54-
if (text == "\/showallusers") {
55-
File subscribedUsersFile = SPIFFS.open("/subscribed_users.json", "r");
56-
57-
if (!subscribedUsersFile) {
58-
bot.sendMessage(chat_id, "No subscription file", "");
59-
}
60-
61-
size_t size = subscribedUsersFile.size();
62-
63-
if (size > 1024) {
64-
bot.sendMessage(chat_id, "Subscribed users file is too large", "");
65-
} else {
66-
String file_content = subscribedUsersFile.readString();
67-
bot.sendMessage(chat_id, file_content, "");
68-
}
69-
}
70-
71-
if (text == "\/removeallusers") {
72-
SPIFFS.remove("/subscribed_users.json");
73-
}
74-
}
75-
}
32+
Serial.println("handleNewMessages");
33+
Serial.println(String(numNewMessages));
7634

77-
JsonObject& getSubscribedUsers(JsonBuffer& jsonBuffer) {
78-
File subscribedUsersFile = SPIFFS.open("/subscribed_users.json", "r");
35+
for (int i=0; i<numNewMessages; i++) {
36+
String chat_id = String(bot.messages[i].chat_id);
37+
String text = bot.messages[i].text;
7938

80-
if (!subscribedUsersFile) {
39+
String from_name = bot.messages[i].from_name;
40+
if (from_name == "") from_name = "Guest";
8141

82-
Serial.println("Failed to open subscribed users file");
42+
if (text == "/start") {
43+
if (addSubscribedUser(chat_id, from_name)) {
44+
bot.sendMessage(chat_id, "Welcome to BulkMessages example, " + from_name, "");
45+
} else {
46+
bot.sendMessage(chat_id, "Something wrong, please try again (later?)", "");
47+
}
48+
}
8349

84-
// Create empyt file (w+ not working as expect)
85-
File f = SPIFFS.open("/subscribed_users.json", "w");
86-
f.close();
50+
if (text == "/stop") {
51+
if (removeSubscribedUser(chat_id)) {
52+
bot.sendMessage(chat_id, "Thank you, " + from_name + ", we always waiting you back", "");
53+
} else {
54+
bot.sendMessage(chat_id, "Something wrong, please try again (later?)", "");
55+
}
56+
}
8757

88-
JsonObject& users = jsonBuffer.createObject();
58+
if (text == "/testbulkmessage") {
59+
sendMessageToAllSubscribedUsers("ATTENTION, this is bulk message for all subscribed users!");
60+
}
8961

90-
return users;
91-
} else {
62+
if (text == "\/showallusers") {
63+
File subscribedUsersFile = SPIFFS.open("/"+subscribed_users_filename, "r");
64+
65+
if (!subscribedUsersFile) {
66+
bot.sendMessage(chat_id, "No subscription file", "");
67+
}
9268

9369
size_t size = subscribedUsersFile.size();
9470

9571
if (size > 1024) {
96-
Serial.println("Subscribed users file is too large");
97-
//return users;
72+
bot.sendMessage(chat_id, "Subscribed users file is too large", "");
73+
} else {
74+
String file_content = subscribedUsersFile.readString();
75+
bot.sendMessage(chat_id, file_content, "");
9876
}
77+
}
9978

100-
String file_content = subscribedUsersFile.readString();
79+
if (text == "\/removeallusers") {
80+
if (SPIFFS.remove("/"+subscribed_users_filename)) {
81+
bot.sendMessage(chat_id, "All users removed", "");
82+
} else {
83+
bot.sendMessage(chat_id, "Something wrong, please try again (later?)", "");
84+
}
85+
}
86+
}
87+
}
10188

102-
JsonObject& users = jsonBuffer.parseObject(file_content);
89+
JsonObject& getSubscribedUsers(JsonBuffer& jsonBuffer) {
90+
File subscribedUsersFile = SPIFFS.open("/"+subscribed_users_filename, "r");
10391

104-
if (!users.success()) {
105-
Serial.println("Failed to parse subscribed users file");
106-
return users;
107-
}
92+
if (!subscribedUsersFile) {
93+
Serial.println("Failed to open subscribed users file");
94+
95+
// Create empty file (w+ not working as expect)
96+
File f = SPIFFS.open("/"+subscribed_users_filename, "w");
97+
f.close();
98+
99+
JsonObject& users = jsonBuffer.createObject();
100+
101+
return users;
102+
} else {
108103

109-
subscribedUsersFile.close();
104+
size_t size = subscribedUsersFile.size();
110105

111-
// Serial.println("Test");
112-
// users.printTo(Serial);
106+
if (size > 1024) {
107+
Serial.println("Subscribed users file is too large");
108+
//return users;
109+
}
110+
111+
String file_content = subscribedUsersFile.readString();
112+
113+
JsonObject& users = jsonBuffer.parseObject(file_content);
113114

115+
if (!users.success()) {
116+
Serial.println("Failed to parse subscribed users file");
114117
return users;
115118
}
116119

120+
subscribedUsersFile.close();
121+
122+
return users;
123+
}
117124
}
118125

119-
bool addSubscribedUser(String chat_id) {
120-
StaticJsonBuffer<200> jsonBuffer;
121-
JsonObject& users = getSubscribedUsers(jsonBuffer);
126+
bool addSubscribedUser(String chat_id, String from_name) {
127+
DynamicJsonBuffer jsonBuffer;
128+
JsonObject& users = getSubscribedUsers(jsonBuffer);
122129

123-
File subscribedUsersFile = SPIFFS.open("/subscribed_users.json", "w+");
130+
File subscribedUsersFile = SPIFFS.open("/"+subscribed_users_filename, "w+");
124131

125-
if (!subscribedUsersFile) {
126-
Serial.println("Failed to open subscribed users file for writing");
127-
//return false;
128-
}
132+
if (!subscribedUsersFile) {
133+
Serial.println("Failed to open subscribed users file for writing");
134+
//return false;
135+
}
129136

130-
users.set(chat_id, 1);
131-
users.printTo(subscribedUsersFile);
137+
users.set(chat_id, from_name);
138+
users.printTo(subscribedUsersFile);
132139

133-
subscribedUsersFile.close();
140+
subscribedUsersFile.close();
134141

135-
return true;
142+
return true;
136143
}
137144

138145
bool removeSubscribedUser(String chat_id) {
139-
StaticJsonBuffer<200> jsonBuffer;
140-
JsonObject& users = getSubscribedUsers(jsonBuffer);
146+
DynamicJsonBuffer jsonBuffer;
147+
JsonObject& users = getSubscribedUsers(jsonBuffer);
141148

142-
File subscribedUsersFile = SPIFFS.open("/subscribed_users.json", "w");
149+
File subscribedUsersFile = SPIFFS.open("/"+subscribed_users_filename, "w");
143150

144-
if (!subscribedUsersFile) {
145-
Serial.println("Failed to open subscribed users file for writing");
146-
return false;
147-
}
151+
if (!subscribedUsersFile) {
152+
Serial.println("Failed to open subscribed users file for writing");
153+
return false;
154+
}
148155

149-
users.remove(chat_id);
150-
users.printTo(subscribedUsersFile);
156+
users.remove(chat_id);
157+
users.printTo(subscribedUsersFile);
151158

152-
subscribedUsersFile.close();
159+
subscribedUsersFile.close();
153160

154-
return true;
161+
return true;
155162
}
156163

157164
void sendMessageToAllSubscribedUsers(String message) {
158165
int users_processed = 0;
159166

160-
StaticJsonBuffer<200> jsonBuffer;
167+
DynamicJsonBuffer jsonBuffer;
161168
JsonObject& users = getSubscribedUsers(jsonBuffer);
162169

163-
for(JsonObject::iterator it=users.begin(); it!=users.end(); ++it)
164-
{
170+
for (JsonObject::iterator it=users.begin(); it!=users.end(); ++it) {
165171
users_processed++;
166172

167173
if (users_processed < messages_limit_per_second) {
168-
const char* chat_id = it->key;
169-
bot.sendMessage(chat_id, message, "");
174+
const char* chat_id = it->key;
175+
bot.sendMessage(chat_id, message, "");
170176
} else {
171-
delay(bulk_messages_mtbs);
172-
users_processed = 0;
177+
delay(bulk_messages_mtbs);
178+
users_processed = 0;
173179
}
174180
}
175181
}
176182

177183
void setup() {
178-
Serial.begin(9600);
184+
Serial.begin(9600);
179185

180-
if (!SPIFFS.begin()) {
181-
Serial.println("Failed to mount file system");
182-
return;
183-
}
186+
if (!SPIFFS.begin()) {
187+
Serial.println("Failed to mount file system");
188+
return;
189+
}
184190

185-
// Set WiFi to station mode and disconnect from an AP if it was Previously
186-
// connected
187-
WiFi.disconnect();
188-
delay(100);
189-
// attempt to connect to Wifi network:
190-
Serial.print("Connecting Wifi: ");
191-
Serial.println(ssid);
192-
WiFi.begin(ssid, password);
193-
194-
while (WiFi.status() != WL_CONNECTED) {
195-
Serial.print(".");
196-
delay(500);
197-
}
191+
// attempt to connect to Wifi network:
192+
Serial.print("Connecting Wifi: ");
193+
Serial.println(ssid);
194+
WiFi.begin(ssid, password);
198195

199-
Serial.println("");
200-
Serial.println("WiFi connected");
201-
Serial.println("IP address: ");
202-
IPAddress ip = WiFi.localIP();
203-
Serial.println(ip);
196+
while (WiFi.status() != WL_CONNECTED) {
197+
Serial.print(".");
198+
delay(500);
199+
}
200+
201+
Serial.println("");
202+
Serial.println("WiFi connected");
203+
Serial.print("IP address: ");
204+
Serial.println(WiFi.localIP());
204205
}
205206

206207
void loop() {
207-
if (millis() > Bot_lasttime + Bot_mtbs) {
208-
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
209-
210-
while(numNewMessages) {
211-
Serial.println("got response");
212-
handleNewMessages(numNewMessages);
213-
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
214-
}
208+
if (millis() > Bot_lasttime + Bot_mtbs) {
209+
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
215210

216-
Bot_lasttime = millis();
211+
while(numNewMessages) {
212+
Serial.println("got response");
213+
handleNewMessages(numNewMessages);
214+
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
217215
}
216+
217+
Bot_lasttime = millis();
218+
}
218219
}

0 commit comments

Comments
 (0)