@@ -26,9 +26,6 @@ long Bot_lasttime; //last time messages' scan has been done
26
26
int bulk_messages_mtbs = 1500 ; // mean time between send messages, 1.5 seconds
27
27
int messages_limit_per_second = 25 ; // Telegram API have limit for bulk messages ~30 messages in 1 second
28
28
29
- StaticJsonBuffer<200 > jsonBuffer;
30
- JsonObject& subscribed_users = jsonBuffer.createObject();
31
-
32
29
void handleNewMessages (int numNewMessages) {
33
30
Serial.println (" handleNewMessages" );
34
31
Serial.println (String (numNewMessages));
@@ -38,99 +35,132 @@ void handleNewMessages(int numNewMessages) {
38
35
String text = bot.messages [i].text ;
39
36
40
37
if (text == " /start" ) {
41
- addSubscribedUser (chat_id);
42
- bot.sendMessage (chat_id, " Welcome" , " " );
43
- loadSubscribedUsersFile ();
38
+ if (addSubscribedUser (chat_id)) {
39
+ bot.sendMessage (chat_id, " Welcome!" , " " );
40
+ } else {
41
+ bot.sendMessage (chat_id, " Something wrong, please try again" , " " );
42
+ }
44
43
}
45
44
46
45
if (text == " /stop" ) {
47
46
bot.sendMessage (chat_id, " Thank you, we always waiting you back" , " " );
48
47
removeSubscribedUser (chat_id);
49
- loadSubscribedUsersFile ();
50
48
}
51
49
52
50
if (text == " /testbulkmessage" ) {
53
51
sendMessageToAllSubscribedUsers (" Current temperature is 0.0C" );
54
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
+ }
55
74
}
56
75
}
57
76
58
- bool loadSubscribedUsersFile ( ) {
77
+ JsonObject& getSubscribedUsers (JsonBuffer& jsonBuffer ) {
59
78
File subscribedUsersFile = SPIFFS.open (" /subscribed_users.json" , " r" );
60
79
61
80
if (!subscribedUsersFile) {
81
+
62
82
Serial.println (" Failed to open subscribed users file" );
63
- return false ;
64
- }
65
83
66
- size_t size = subscribedUsersFile.size ();
84
+ // Create empyt file (w+ not working as expect)
85
+ File f = SPIFFS.open (" /subscribed_users.json" , " w" );
86
+ f.close ();
67
87
68
- if (size > 1024 ) {
69
- Serial.println (" Subscribed users file is too large" );
70
- return false ;
71
- }
88
+ JsonObject& users = jsonBuffer.createObject ();
72
89
73
- // Allocate a buffer to store contents of the file.
74
- std::unique_ptr< char []> buf ( new char [size]);
90
+ return users;
91
+ } else {
75
92
76
- // We don't use String here because ArduinoJson library requires the input
77
- // buffer to be mutable. If you don't use ArduinoJson, you may as well
78
- // use configFile.readString instead.
79
- subscribedUsersFile.readBytes (buf.get (), size);
93
+ size_t size = subscribedUsersFile.size ();
80
94
81
- StaticJsonBuffer<200 > jsonBuffer;
82
- JsonObject& subscribed_users = jsonBuffer.parseObject (buf.get ());
95
+ if (size > 1024 ) {
96
+ Serial.println (" Subscribed users file is too large" );
97
+ // return users;
98
+ }
83
99
84
- if (!subscribed_users.success ()) {
85
- Serial.println (" Failed to parse subscribed users file" );
86
- return false ;
87
- }
100
+ String file_content = subscribedUsersFile.readString ();
88
101
89
- subscribedUsersFile. close ( );
102
+ JsonObject& users = jsonBuffer. parseObject (file_content );
90
103
91
- subscribed_users.printTo (Serial);
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
+ }
92
116
93
- return true ;
94
117
}
95
118
96
119
bool addSubscribedUser (String chat_id) {
97
- File subscribedUsersFile = SPIFFS.open (" /subscribed_users.json" , " w" );
120
+ StaticJsonBuffer<200 > jsonBuffer;
121
+ JsonObject& users = getSubscribedUsers (jsonBuffer);
122
+
123
+ File subscribedUsersFile = SPIFFS.open (" /subscribed_users.json" , " w+" );
98
124
99
125
if (!subscribedUsersFile) {
100
126
Serial.println (" Failed to open subscribed users file for writing" );
101
- return false ;
127
+ // return false;
102
128
}
103
129
104
- subscribed_users[chat_id] = 1 ;
105
-
106
- subscribed_users.printTo (subscribedUsersFile);
130
+ users.set (chat_id, 1 );
131
+ users.printTo (subscribedUsersFile);
107
132
108
133
subscribedUsersFile.close ();
109
134
110
135
return true ;
111
136
}
112
137
113
138
bool removeSubscribedUser (String chat_id) {
139
+ StaticJsonBuffer<200 > jsonBuffer;
140
+ JsonObject& users = getSubscribedUsers (jsonBuffer);
141
+
114
142
File subscribedUsersFile = SPIFFS.open (" /subscribed_users.json" , " w" );
115
143
116
144
if (!subscribedUsersFile) {
117
145
Serial.println (" Failed to open subscribed users file for writing" );
118
146
return false ;
119
147
}
120
148
121
- subscribed_users.remove (chat_id);
122
-
123
- subscribed_users.printTo (subscribedUsersFile);
149
+ users.remove (chat_id);
150
+ users.printTo (subscribedUsersFile);
124
151
125
152
subscribedUsersFile.close ();
126
153
127
154
return true ;
128
155
}
129
156
130
157
void sendMessageToAllSubscribedUsers (String message) {
131
- int users_processed = 0 ;
158
+ int users_processed = 0 ;
132
159
133
- for (JsonObject::iterator it=subscribed_users.begin (); it!=subscribed_users.end (); ++it)
160
+ StaticJsonBuffer<200 > jsonBuffer;
161
+ JsonObject& users = getSubscribedUsers (jsonBuffer);
162
+
163
+ for (JsonObject::iterator it=users.begin (); it!=users.end (); ++it)
134
164
{
135
165
users_processed++;
136
166
@@ -152,8 +182,6 @@ void setup() {
152
182
return ;
153
183
}
154
184
155
- loadSubscribedUsersFile ();
156
-
157
185
// Set WiFi to station mode and disconnect from an AP if it was Previously
158
186
// connected
159
187
WiFi.disconnect ();
0 commit comments