1
1
/* ******************************************************************
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
+ *******************************************************************/
7
7
#include < ESP8266WiFi.h>
8
8
#include < WiFiClientSecure.h>
9
9
#include < UniversalTelegramBot.h>
10
10
#include < FS.h>
11
11
#include < ArduinoJson.h>
12
12
13
13
// Initialize Wifi connection to the router
14
- char ssid[] = " xxxxxxx" ; // your network SSID (name)
14
+ char ssid[] = " xxxxxxx" ; // your network SSID (name)
15
15
char password[] = " yyyyyyyy" ; // your network key
16
16
17
17
// Initialize Telegram BOT
18
18
#define BOTtoken " XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
19
19
20
- WiFiClientSecure client ;
21
- UniversalTelegramBot bot (BOTtoken, client );
20
+ WiFiClientSecure secured_client ;
21
+ UniversalTelegramBot bot (BOTtoken, secured_client );
22
22
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
25
25
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" ;
28
30
29
31
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));
76
34
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 ;
79
38
80
- if (!subscribedUsersFile) {
39
+ String from_name = bot.messages [i].from_name ;
40
+ if (from_name == " " ) from_name = " Guest" ;
81
41
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
+ }
83
49
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
+ }
87
57
88
- JsonObject& users = jsonBuffer.createObject ();
58
+ if (text == " /testbulkmessage" ) {
59
+ sendMessageToAllSubscribedUsers (" ATTENTION, this is bulk message for all subscribed users!" );
60
+ }
89
61
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
+ }
92
68
93
69
size_t size = subscribedUsersFile.size ();
94
70
95
71
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, " " );
98
76
}
77
+ }
99
78
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
+ }
101
88
102
- JsonObject& users = jsonBuffer.parseObject (file_content);
89
+ JsonObject& getSubscribedUsers (JsonBuffer& jsonBuffer) {
90
+ File subscribedUsersFile = SPIFFS.open (" /" +subscribed_users_filename, " r" );
103
91
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 {
108
103
109
- subscribedUsersFile.close ();
104
+ size_t size = subscribedUsersFile.size ();
110
105
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);
113
114
115
+ if (!users.success ()) {
116
+ Serial.println (" Failed to parse subscribed users file" );
114
117
return users;
115
118
}
116
119
120
+ subscribedUsersFile.close ();
121
+
122
+ return users;
123
+ }
117
124
}
118
125
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);
122
129
123
- File subscribedUsersFile = SPIFFS.open (" /subscribed_users.json " , " w+" );
130
+ File subscribedUsersFile = SPIFFS.open (" /" +subscribed_users_filename , " w+" );
124
131
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
+ }
129
136
130
- users.set (chat_id, 1 );
131
- users.printTo (subscribedUsersFile);
137
+ users.set (chat_id, from_name );
138
+ users.printTo (subscribedUsersFile);
132
139
133
- subscribedUsersFile.close ();
140
+ subscribedUsersFile.close ();
134
141
135
- return true ;
142
+ return true ;
136
143
}
137
144
138
145
bool removeSubscribedUser (String chat_id) {
139
- StaticJsonBuffer< 200 > jsonBuffer;
140
- JsonObject& users = getSubscribedUsers (jsonBuffer);
146
+ DynamicJsonBuffer jsonBuffer;
147
+ JsonObject& users = getSubscribedUsers (jsonBuffer);
141
148
142
- File subscribedUsersFile = SPIFFS.open (" /subscribed_users.json " , " w" );
149
+ File subscribedUsersFile = SPIFFS.open (" /" +subscribed_users_filename , " w" );
143
150
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
+ }
148
155
149
- users.remove (chat_id);
150
- users.printTo (subscribedUsersFile);
156
+ users.remove (chat_id);
157
+ users.printTo (subscribedUsersFile);
151
158
152
- subscribedUsersFile.close ();
159
+ subscribedUsersFile.close ();
153
160
154
- return true ;
161
+ return true ;
155
162
}
156
163
157
164
void sendMessageToAllSubscribedUsers (String message) {
158
165
int users_processed = 0 ;
159
166
160
- StaticJsonBuffer< 200 > jsonBuffer;
167
+ DynamicJsonBuffer jsonBuffer;
161
168
JsonObject& users = getSubscribedUsers (jsonBuffer);
162
169
163
- for (JsonObject::iterator it=users.begin (); it!=users.end (); ++it)
164
- {
170
+ for (JsonObject::iterator it=users.begin (); it!=users.end (); ++it) {
165
171
users_processed++;
166
172
167
173
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, " " );
170
176
} else {
171
- delay (bulk_messages_mtbs);
172
- users_processed = 0 ;
177
+ delay (bulk_messages_mtbs);
178
+ users_processed = 0 ;
173
179
}
174
180
}
175
181
}
176
182
177
183
void setup () {
178
- Serial.begin (9600 );
184
+ Serial.begin (9600 );
179
185
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
+ }
184
190
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);
198
195
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 ());
204
205
}
205
206
206
207
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 );
215
210
216
- Bot_lasttime = millis ();
211
+ while (numNewMessages) {
212
+ Serial.println (" got response" );
213
+ handleNewMessages (numNewMessages);
214
+ numNewMessages = bot.getUpdates (bot.last_message_received + 1 );
217
215
}
216
+
217
+ Bot_lasttime = millis ();
218
+ }
218
219
}
0 commit comments