@@ -64,7 +64,7 @@ String UniversalTelegramBot::buildCommand(const String& cmd) {
6464}
6565
6666String UniversalTelegramBot::sendGetToTelegram (const String& command) {
67- String body, headers ;
67+ String body;
6868
6969 // Connect with api.telegram.org if not already connected
7070 if (!client->connected ()) {
@@ -91,34 +91,51 @@ String UniversalTelegramBot::sendGetToTelegram(const String& command) {
9191 client->println (F (" Cache-Control: no-cache" ));
9292 client->println ();
9393
94- readHTTPAnswer (body, headers );
94+ readHTTPAnswer (body);
9595 }
9696
9797 return body;
9898}
9999
100- bool UniversalTelegramBot::readHTTPAnswer (String &body, String &headers ) {
100+ bool UniversalTelegramBot::readHTTPAnswer (String &body) {
101101 int ch_count = 0 ;
102102 unsigned long now = millis ();
103103 bool finishedHeaders = false ;
104104 bool currentLineIsBlank = true ;
105105 bool responseReceived = false ;
106+ int toRead = 0 ;
107+ String headers;
106108
107109 while (millis () - now < longPoll * 1000 + waitForResponse) {
108110 while (client->available ()) {
109111 char c = client->read ();
110- responseReceived = true ;
111112
112113 if (!finishedHeaders) {
113114 if (currentLineIsBlank && c == ' \n ' ) {
114115 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+ }
115131 } else {
116132 headers += c;
117133 }
118134 } else {
119135 if (ch_count < maxMessageLength) {
120136 body += c;
121137 ch_count++;
138+ responseReceived = toRead > 0 ? ch_count == toRead : true ;
122139 }
123140 }
124141
@@ -127,21 +144,23 @@ bool UniversalTelegramBot::readHTTPAnswer(String &body, String &headers) {
127144 }
128145
129146 if (responseReceived) {
130- #ifdef TELEGRAM_DEBUG
131- Serial.println ();
132- Serial.println (body);
133- Serial.println ();
134- #endif
135147 break ;
136148 }
137149 }
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+
138158 return responseReceived;
139159}
140160
141161String UniversalTelegramBot::sendPostToTelegram (const String& command, JsonObject payload) {
142162
143163 String body;
144- String headers;
145164
146165 // Connect with api.telegram.org if not already connected
147166 if (!client->connected ()) {
@@ -176,10 +195,11 @@ String UniversalTelegramBot::sendPostToTelegram(const String& command, JsonObjec
176195
177196 client->println (out);
178197 #ifdef TELEGRAM_DEBUG
179- Serial.println (String (" Posting:" ) + out);
198+ Serial.print (F (" Posting: " ));
199+ Serial.println (out);
180200 #endif
181201
182- readHTTPAnswer (body, headers );
202+ readHTTPAnswer (body);
183203 }
184204
185205 return body;
@@ -194,7 +214,6 @@ String UniversalTelegramBot::sendMultipartFormDataToTelegram(
194214 GetNextBufferLen getNextBufferLenCallback) {
195215
196216 String body;
197- String headers;
198217
199218 const String boundary = F (" ------------------------b8f610217e83e29b" );
200219
@@ -252,7 +271,8 @@ String UniversalTelegramBot::sendMultipartFormDataToTelegram(
252271 client->print (start_request);
253272
254273 #ifdef TELEGRAM_DEBUG
255- Serial.print (" Start request: " + start_request);
274+ Serial.print (F (" Start request: " ));
275+ Serial.print (start_request);
256276 #endif
257277
258278 if (getNextByteCallback == nullptr ) {
@@ -291,9 +311,10 @@ String UniversalTelegramBot::sendMultipartFormDataToTelegram(
291311
292312 client->print (end_request);
293313 #ifdef TELEGRAM_DEBUG
294- Serial.print (" End request: " + end_request);
314+ Serial.print (F (" End request: " ));
315+ Serial.print (end_request);
295316 #endif
296- readHTTPAnswer (body, headers );
317+ readHTTPAnswer (body);
297318 }
298319
299320 closeClient ();
@@ -337,7 +358,8 @@ bool UniversalTelegramBot::setMyCommands(const String& commandArray) {
337358 while (millis () - sttime < 8000ul ) { // loop for a while to send the message
338359 response = sendPostToTelegram (BOT_CMD (" setMyCommands" ), payload.as <JsonObject>());
339360 #ifdef TELEGRAM_DEBUG
340- Serial.println (" setMyCommands response" + response);
361+ Serial.println (F (" setMyCommands response:" ));
362+ Serial.println (response);
341363 #endif
342364 sent = checkForOkResponse (response);
343365 if (sent) break ;
@@ -368,6 +390,7 @@ int UniversalTelegramBot::getUpdates(long offset) {
368390 command += String (longPoll);
369391 }
370392 String response = sendGetToTelegram (command); // receive reply from telegram.org
393+ long updateId = getUpdateIdFromResponse (response);
371394
372395 if (response == " " ) {
373396 #ifdef TELEGRAM_DEBUG
@@ -416,6 +439,9 @@ int UniversalTelegramBot::getUpdates(long offset) {
416439 #endif
417440 }
418441 } else { // Parsing failed
442+ Serial.print (F (" Update ID with error: " ));
443+ Serial.println (updateId);
444+
419445 if (response.length () < 2 ) { // Too short a message. Maybe a connection issue
420446 #ifdef TELEGRAM_DEBUG
421447 Serial.println (F (" Parsing error: Message too short" ));
@@ -432,6 +458,15 @@ int UniversalTelegramBot::getUpdates(long offset) {
432458 }
433459 // Close the client as no response is to be given
434460 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+
435470 return 0 ;
436471 }
437472}
@@ -651,7 +686,7 @@ bool UniversalTelegramBot::sendMessageWithInlineKeyboard(const String& chat_id,
651686 const String& text,
652687 const String& parse_mode,
653688 const String& keyboard,
654- int message_id) { // added message_id
689+ int message_id) {
655690
656691 DynamicJsonDocument payload (maxMessageLength);
657692 payload[" chat_id" ] = chat_id;
@@ -860,3 +895,29 @@ bool UniversalTelegramBot::answerCallbackQuery(const String &query_id, const Str
860895 closeClient ();
861896 return answer;
862897}
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