@@ -64,7 +64,7 @@ String UniversalTelegramBot::buildCommand(const String& cmd) {
64
64
}
65
65
66
66
String UniversalTelegramBot::sendGetToTelegram (const String& command) {
67
- String body, headers ;
67
+ String body;
68
68
69
69
// Connect with api.telegram.org if not already connected
70
70
if (!client->connected ()) {
@@ -91,34 +91,51 @@ String UniversalTelegramBot::sendGetToTelegram(const String& command) {
91
91
client->println (F (" Cache-Control: no-cache" ));
92
92
client->println ();
93
93
94
- readHTTPAnswer (body, headers );
94
+ readHTTPAnswer (body);
95
95
}
96
96
97
97
return body;
98
98
}
99
99
100
- bool UniversalTelegramBot::readHTTPAnswer (String &body, String &headers ) {
100
+ bool UniversalTelegramBot::readHTTPAnswer (String &body) {
101
101
int ch_count = 0 ;
102
102
unsigned long now = millis ();
103
103
bool finishedHeaders = false ;
104
104
bool currentLineIsBlank = true ;
105
105
bool responseReceived = false ;
106
+ int toRead = 0 ;
107
+ String headers;
106
108
107
109
while (millis () - now < longPoll * 1000 + waitForResponse) {
108
110
while (client->available ()) {
109
111
char c = client->read ();
110
- responseReceived = true ;
111
112
112
113
if (!finishedHeaders) {
113
114
if (currentLineIsBlank && c == ' \n ' ) {
114
115
finishedHeaders = true ;
116
+
117
+ String headerLC = String (headers);
118
+ headerLC.toLowerCase ();
119
+ int ind1 = headerLC.indexOf (" content-length" );
120
+ if (ind1 != -1 ) {
121
+ int ind2 = headerLC.indexOf (" \r " , ind1 + 15 );
122
+ if (ind2 != -1 ) {
123
+ toRead = headerLC.substring (ind1 + 15 , ind2).toInt ();
124
+ headers = " " ;
125
+ #ifdef TELEGRAM_DEBUG
126
+ Serial.print (F (" Content-Length: " ));
127
+ Serial.println (toRead);
128
+ #endif
129
+ }
130
+ }
115
131
} else {
116
132
headers += c;
117
133
}
118
134
} else {
119
135
if (ch_count < maxMessageLength) {
120
136
body += c;
121
137
ch_count++;
138
+ responseReceived = toRead > 0 ? ch_count == toRead : true ;
122
139
}
123
140
}
124
141
@@ -127,21 +144,23 @@ bool UniversalTelegramBot::readHTTPAnswer(String &body, String &headers) {
127
144
}
128
145
129
146
if (responseReceived) {
130
- #ifdef TELEGRAM_DEBUG
131
- Serial.println ();
132
- Serial.println (body);
133
- Serial.println ();
134
- #endif
135
147
break ;
136
148
}
137
149
}
150
+
151
+ #ifdef TELEGRAM_DEBUG
152
+ Serial.println (F (" Body:" ));
153
+ Serial.println (body);
154
+ Serial.print (F (" ch_count: " ));
155
+ Serial.println (ch_count);
156
+ #endif
157
+
138
158
return responseReceived;
139
159
}
140
160
141
161
String UniversalTelegramBot::sendPostToTelegram (const String& command, JsonObject payload) {
142
162
143
163
String body;
144
- String headers;
145
164
146
165
// Connect with api.telegram.org if not already connected
147
166
if (!client->connected ()) {
@@ -176,10 +195,11 @@ String UniversalTelegramBot::sendPostToTelegram(const String& command, JsonObjec
176
195
177
196
client->println (out);
178
197
#ifdef TELEGRAM_DEBUG
179
- Serial.println (String (" Posting:" ) + out);
198
+ Serial.print (F (" Posting: " ));
199
+ Serial.println (out);
180
200
#endif
181
201
182
- readHTTPAnswer (body, headers );
202
+ readHTTPAnswer (body);
183
203
}
184
204
185
205
return body;
@@ -194,7 +214,6 @@ String UniversalTelegramBot::sendMultipartFormDataToTelegram(
194
214
GetNextBufferLen getNextBufferLenCallback) {
195
215
196
216
String body;
197
- String headers;
198
217
199
218
const String boundary = F (" ------------------------b8f610217e83e29b" );
200
219
@@ -252,7 +271,8 @@ String UniversalTelegramBot::sendMultipartFormDataToTelegram(
252
271
client->print (start_request);
253
272
254
273
#ifdef TELEGRAM_DEBUG
255
- Serial.print (" Start request: " + start_request);
274
+ Serial.print (F (" Start request: " ));
275
+ Serial.print (start_request);
256
276
#endif
257
277
258
278
if (getNextByteCallback == nullptr ) {
@@ -291,9 +311,10 @@ String UniversalTelegramBot::sendMultipartFormDataToTelegram(
291
311
292
312
client->print (end_request);
293
313
#ifdef TELEGRAM_DEBUG
294
- Serial.print (" End request: " + end_request);
314
+ Serial.print (F (" End request: " ));
315
+ Serial.print (end_request);
295
316
#endif
296
- readHTTPAnswer (body, headers );
317
+ readHTTPAnswer (body);
297
318
}
298
319
299
320
closeClient ();
@@ -337,7 +358,8 @@ bool UniversalTelegramBot::setMyCommands(const String& commandArray) {
337
358
while (millis () - sttime < 8000ul ) { // loop for a while to send the message
338
359
response = sendPostToTelegram (BOT_CMD (" setMyCommands" ), payload.as <JsonObject>());
339
360
#ifdef TELEGRAM_DEBUG
340
- Serial.println (" setMyCommands response" + response);
361
+ Serial.println (F (" setMyCommands response:" ));
362
+ Serial.println (response);
341
363
#endif
342
364
sent = checkForOkResponse (response);
343
365
if (sent) break ;
@@ -368,6 +390,7 @@ int UniversalTelegramBot::getUpdates(long offset) {
368
390
command += String (longPoll);
369
391
}
370
392
String response = sendGetToTelegram (command); // receive reply from telegram.org
393
+ long updateId = getUpdateIdFromResponse (response);
371
394
372
395
if (response == " " ) {
373
396
#ifdef TELEGRAM_DEBUG
@@ -416,6 +439,9 @@ int UniversalTelegramBot::getUpdates(long offset) {
416
439
#endif
417
440
}
418
441
} else { // Parsing failed
442
+ Serial.print (F (" Update ID with error: " ));
443
+ Serial.println (updateId);
444
+
419
445
if (response.length () < 2 ) { // Too short a message. Maybe a connection issue
420
446
#ifdef TELEGRAM_DEBUG
421
447
Serial.println (F (" Parsing error: Message too short" ));
@@ -432,6 +458,15 @@ int UniversalTelegramBot::getUpdates(long offset) {
432
458
}
433
459
// Close the client as no response is to be given
434
460
closeClient ();
461
+
462
+ if (error && response.length () == (unsigned ) maxMessageLength) {
463
+ Serial.print (F (" The message with update ID " ));
464
+ Serial.print (updateId);
465
+ Serial.print (F (" is too long and was skipped. The next update ID has been sent for processing." ));
466
+
467
+ return getUpdates (updateId + 1 );
468
+ }
469
+
435
470
return 0 ;
436
471
}
437
472
}
@@ -651,7 +686,7 @@ bool UniversalTelegramBot::sendMessageWithInlineKeyboard(const String& chat_id,
651
686
const String& text,
652
687
const String& parse_mode,
653
688
const String& keyboard,
654
- int message_id) { // added message_id
689
+ int message_id) {
655
690
656
691
DynamicJsonDocument payload (maxMessageLength);
657
692
payload[" chat_id" ] = chat_id;
@@ -860,3 +895,29 @@ bool UniversalTelegramBot::answerCallbackQuery(const String &query_id, const Str
860
895
closeClient ();
861
896
return answer;
862
897
}
898
+
899
+ long UniversalTelegramBot::getUpdateIdFromResponse (String response) {
900
+ response.remove (response.indexOf (" \n " ));
901
+
902
+ char updateId[20 ];
903
+ const char *str = response.c_str ();
904
+
905
+ while (*str != ' \0 ' )
906
+ {
907
+ if (*str == ' \r ' ) {
908
+ break ;
909
+ }
910
+
911
+ str++;
912
+
913
+ int i = 0 ;
914
+ while (' 0' <= *str && *str <= ' 9' )
915
+ {
916
+ updateId[i] = *str;
917
+ i++;
918
+ str++;
919
+ }
920
+ }
921
+
922
+ return atol (updateId);
923
+ }
0 commit comments