Created
June 7, 2020 13:38
-
-
Save witnessmenow/5ddb52d810122d068b6d81b1ac625ce1 to your computer and use it in GitHub Desktop.
UniversalTelegramAPI library get fix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Untested! | |
// Replace the same method in UniversalTelegramBot.cpp | |
String UniversalTelegramBot::sendGetToTelegram(String command) { | |
String body = ""; | |
String headers = ""; | |
long now; | |
bool responseReceived = false; | |
// Connect with api.telegram.org if not already connected | |
if (!client->connected()) { | |
#ifdef _debug | |
Serial.println(F("[BOT]Connecting to server")); | |
#endif | |
if (!client->connect(TELEGRAM_HOST, TELEGRAM_SSL_PORT)) { | |
#ifdef _debug | |
Serial.println(F("[BOT]Conection error")); | |
#endif | |
} | |
} | |
if (client->connected()) { | |
#ifdef _debug | |
Serial.println(F(".... connected to server")); | |
#endif | |
client->print("GET /" + command); | |
client->println(F(" HTTP/1.1")); | |
client->print(F("Host:")); | |
client->println(TELEGRAM_HOST); | |
client->println(F("Accept: application/json")); | |
client->println(F("Cache-Control: no-cache")); | |
client->println(); | |
int ch_count = 0; | |
now = millis(); | |
bool finishedHeaders = false; | |
bool currentLineIsBlank = true; | |
while (millis() - now < waitForResponse) { | |
while (client->available()) { | |
char c = client->read(); | |
responseReceived = true; | |
if (!finishedHeaders) { | |
if (currentLineIsBlank && c == '\n') { | |
finishedHeaders = true; | |
} else { | |
headers = headers + c; | |
} | |
} else { | |
if (ch_count < maxMessageLength) { | |
body = body + c; | |
ch_count++; | |
} | |
} | |
if (c == '\n') currentLineIsBlank = true; | |
else if (c != '\r') currentLineIsBlank = false; | |
} | |
if (responseReceived) { | |
#ifdef _debug | |
Serial.println(); | |
Serial.println(body); | |
Serial.println(); | |
#endif | |
break; | |
} | |
} | |
} | |
return body; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment