1
+ /* ******************************************************************
2
+ * An example of how to use a bulk messages to subscribed users. *
3
+ * *
4
+ * *
5
+ * written by Vadim Sinitski *
6
+ *******************************************************************/
7
+ #include < ESP8266WiFi.h>
8
+ #include < WiFiClientSecure.h>
9
+ #include < UniversalTelegramBot.h>
10
+ #include < FS.h>
11
+ #include < ArduinoJson.h>
12
+
13
+ // Initialize Wifi connection to the router
14
+ char ssid[] = " xxxxxxx" ; // your network SSID (name)
15
+ char password[] = " yyyyyyyy" ; // your network key
16
+
17
+ // Initialize Telegram BOT
18
+ #define BOTtoken " XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get off Botfather)
19
+
20
+ WiFiClientSecure client;
21
+ UniversalTelegramBot bot (BOTtoken, client);
22
+
23
+ int Bot_mtbs = 1000 ; // mean time between scan messages
24
+ long Bot_lasttime; // last time messages' scan has been done
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
28
+
29
+ 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
+ }
76
+
77
+ JsonObject& getSubscribedUsers (JsonBuffer& jsonBuffer) {
78
+ File subscribedUsersFile = SPIFFS.open (" /subscribed_users.json" , " r" );
79
+
80
+ if (!subscribedUsersFile) {
81
+
82
+ Serial.println (" Failed to open subscribed users file" );
83
+
84
+ // Create empyt file (w+ not working as expect)
85
+ File f = SPIFFS.open (" /subscribed_users.json" , " w" );
86
+ f.close ();
87
+
88
+ JsonObject& users = jsonBuffer.createObject ();
89
+
90
+ return users;
91
+ } else {
92
+
93
+ size_t size = subscribedUsersFile.size ();
94
+
95
+ if (size > 1024 ) {
96
+ Serial.println (" Subscribed users file is too large" );
97
+ // return users;
98
+ }
99
+
100
+ String file_content = subscribedUsersFile.readString ();
101
+
102
+ JsonObject& users = jsonBuffer.parseObject (file_content);
103
+
104
+ if (!users.success ()) {
105
+ Serial.println (" Failed to parse subscribed users file" );
106
+ return users;
107
+ }
108
+
109
+ subscribedUsersFile.close ();
110
+
111
+ // Serial.println("Test");
112
+ // users.printTo(Serial);
113
+
114
+ return users;
115
+ }
116
+
117
+ }
118
+
119
+ bool addSubscribedUser (String chat_id) {
120
+ StaticJsonBuffer<200 > jsonBuffer;
121
+ JsonObject& users = getSubscribedUsers (jsonBuffer);
122
+
123
+ File subscribedUsersFile = SPIFFS.open (" /subscribed_users.json" , " w+" );
124
+
125
+ if (!subscribedUsersFile) {
126
+ Serial.println (" Failed to open subscribed users file for writing" );
127
+ // return false;
128
+ }
129
+
130
+ users.set (chat_id, 1 );
131
+ users.printTo (subscribedUsersFile);
132
+
133
+ subscribedUsersFile.close ();
134
+
135
+ return true ;
136
+ }
137
+
138
+ bool removeSubscribedUser (String chat_id) {
139
+ StaticJsonBuffer<200 > jsonBuffer;
140
+ JsonObject& users = getSubscribedUsers (jsonBuffer);
141
+
142
+ File subscribedUsersFile = SPIFFS.open (" /subscribed_users.json" , " w" );
143
+
144
+ if (!subscribedUsersFile) {
145
+ Serial.println (" Failed to open subscribed users file for writing" );
146
+ return false ;
147
+ }
148
+
149
+ users.remove (chat_id);
150
+ users.printTo (subscribedUsersFile);
151
+
152
+ subscribedUsersFile.close ();
153
+
154
+ return true ;
155
+ }
156
+
157
+ void sendMessageToAllSubscribedUsers (String message) {
158
+ int users_processed = 0 ;
159
+
160
+ StaticJsonBuffer<200 > jsonBuffer;
161
+ JsonObject& users = getSubscribedUsers (jsonBuffer);
162
+
163
+ for (JsonObject::iterator it=users.begin (); it!=users.end (); ++it)
164
+ {
165
+ users_processed++;
166
+
167
+ if (users_processed < messages_limit_per_second) {
168
+ const char * chat_id = it->key ;
169
+ bot.sendMessage (chat_id, message, " " );
170
+ } else {
171
+ delay (bulk_messages_mtbs);
172
+ users_processed = 0 ;
173
+ }
174
+ }
175
+ }
176
+
177
+ void setup () {
178
+ Serial.begin (9600 );
179
+
180
+ if (!SPIFFS.begin ()) {
181
+ Serial.println (" Failed to mount file system" );
182
+ return ;
183
+ }
184
+
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
+ }
198
+
199
+ Serial.println (" " );
200
+ Serial.println (" WiFi connected" );
201
+ Serial.println (" IP address: " );
202
+ IPAddress ip = WiFi.localIP ();
203
+ Serial.println (ip);
204
+ }
205
+
206
+ void loop () {
207
+ if (millis () > Bot_lasttime + Bot_mtbs) {
208
+ int numNewMessages = bot.getUpdates (bot.last_message_recived + 1 );
209
+
210
+ while (numNewMessages) {
211
+ Serial.println (" got response" );
212
+ handleNewMessages (numNewMessages);
213
+ numNewMessages = bot.getUpdates (bot.last_message_recived + 1 );
214
+ }
215
+
216
+ Bot_lasttime = millis ();
217
+ }
218
+ }
0 commit comments