Fixed Telegram response decoding stopped working after 20210621

Fixed Telegram response decoding stopped working after 20210621 and exception on long result message (#12451)
This commit is contained in:
Theo Arends 2021-06-24 18:29:12 +02:00
parent 09b7b513e2
commit ef508c629f
3 changed files with 36 additions and 29 deletions

View File

@ -15,6 +15,7 @@ All notable changes to this project will be documented in this file.
- ESP32 Webcam add boundary marker before sending mjpeg image (#12376) - ESP32 Webcam add boundary marker before sending mjpeg image (#12376)
- DDS238-2 wrong reactive power value (#12283) - DDS238-2 wrong reactive power value (#12283)
- NO VALID JSON regression from may 4th (#12440) - NO VALID JSON regression from may 4th (#12440)
- Telegram response decoding stopped working after 20210621 and exception on long result message (#12451)
## [Released] ## [Released]

View File

@ -108,6 +108,7 @@ The latter links can be used for OTA upgrades too like ``OtaUrl http://ota.tasmo
- DDS238-2 wrong reactive power value [#12283](https://github.com/arendst/Tasmota/issues/12283) - DDS238-2 wrong reactive power value [#12283](https://github.com/arendst/Tasmota/issues/12283)
- ESP32 Webcam add boundary marker before sending mjpeg image [#12376](https://github.com/arendst/Tasmota/issues/12376) - ESP32 Webcam add boundary marker before sending mjpeg image [#12376](https://github.com/arendst/Tasmota/issues/12376)
- NO VALID JSON regression from may 4th [#12440](https://github.com/arendst/Tasmota/issues/12440) - NO VALID JSON regression from may 4th [#12440](https://github.com/arendst/Tasmota/issues/12440)
- Telegram response decoding stopped working after 20210621 and exception on long result message [#12451](https://github.com/arendst/Tasmota/issues/12451)
### Noted ### Noted
- ESP32 single core **tasmota32solo1.bin** binary can only be uploaded using the GUI as OTA upload will trigger the watchdog timer - ESP32 single core **tasmota32solo1.bin** binary can only be uploaded using the GUI as OTA upload will trigger the watchdog timer

View File

@ -103,51 +103,56 @@ bool TelegramInit(void) {
return init_done; return init_done;
} }
String TelegramConnectToTelegram(String command) { String TelegramConnectToTelegram(const String &command) {
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("TGM: Cmnd %s"), command.c_str()); // AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("TGM: Cmnd '%s'"), command.c_str());
if (!TelegramInit()) { return ""; } if (!TelegramInit()) { return ""; }
String host = F("api.telegram.org");
String response = ""; String response = "";
uint32_t tls_connect_time = millis(); uint32_t tls_connect_time = millis();
if (telegramClient->connect("api.telegram.org", 443)) { if (telegramClient->connect(host.c_str(), 443)) {
// AddLog(LOG_LEVEL_DEBUG, PSTR("TGM: Connected in %d ms, max ThunkStack used %d"), millis() - tls_connect_time, telegramClient->getMaxThunkStackUse()); // AddLog(LOG_LEVEL_DEBUG, PSTR("TGM: Connected in %d ms, max ThunkStack used %d"), millis() - tls_connect_time, telegramClient->getMaxThunkStackUse());
// telegramClient->println("GET /"+command); // telegramClient->println("GET /"+command); // Fails after 20210621
String request = "GET /" + command + " HTTP/1.1\r\nHost: " + host + "\r\nConnection: close\r\n\r\n";
String request = String("GET /") + command +
" HTTP/1.1\r\n" +
"Host: api.telegram.org" +
"\r\n" + "Connection: close\r\n\r\n";
telegramClient->print(request); telegramClient->print(request);
/*
Response before 20210621:
{"ok":true,"result":[]}
Response after 20210621:
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Thu, 24 Jun 2021 15:26:20 GMT
Content-Type: application/json
Content-Length: 23
Connection: close
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Expose-Headers: Content-Length,Content-Type,Date,Server,Connection
{"ok":true,"result":[]}
*/
char c; char c;
int ch_count=0; bool available = false;
uint32_t now = millis(); uint32_t now = millis();
bool avail = false; while (!available && (millis() -now < 1500)) {
bool start = false;
while (millis() -now < 1500) {
while (telegramClient->available()) { while (telegramClient->available()) {
char c = telegramClient->read(); c = telegramClient->read();
if (ch_count < 1000) { // Allow up to two messages
if (c == '{') { if (c == '{') {
start = true; available = true; // Skip headers (+-400 bytes) and start response at first JSON
} }
if (start) { if (available) {
response = response + c; response += c;
ch_count++; if (response.length() > 800) { // Allow up to two messages
}
}
avail = true;
}
if (avail) {
break; break;
} }
} }
}
}
telegramClient->stop(); telegramClient->stop();
} }
@ -216,7 +221,7 @@ void TelegramGetUpdates(uint32_t offset) {
// } // }
// ]} // ]}
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("TGM: Response %s"), response.c_str()); AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("TGM: Response '%s'"), response.c_str());
JsonParser parser((char*)response.c_str()); JsonParser parser((char*)response.c_str());
JsonParserObject root = parser.getRootObject(); JsonParserObject root = parser.getRootObject();
@ -260,7 +265,7 @@ void TelegramGetUpdates(uint32_t offset) {
} }
} }
bool TelegramSendMessage(String chat_id, String text) { bool TelegramSendMessage(const String &chat_id, const String &text) {
AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("TGM: sendMessage")); AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("TGM: sendMessage"));
if (!TelegramInit()) { return false; } if (!TelegramInit()) { return false; }