Skip to content

Commit 90904d8

Browse files
committed
Added client handling for ESP32
1 parent e0ddfd2 commit 90904d8

File tree

2 files changed

+68
-11
lines changed

2 files changed

+68
-11
lines changed

src/UniversalTelegramBot.cpp

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ License along with this library; if not, write to the Free Software
2020
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2121
*/
2222

23+
/* **** Note Regarding Client Connection Keeping ****
24+
Client connection is established in functions that directly involve use of client,
25+
i.e sendGetToTelegram, sendPostToTelegram, and sendMultipartFormDataToTelegram.
26+
It is closed at the end of sendMultipartFormDataToTelegram, but not at the
27+
end of sendGetToTelegram and sendPostToTelegram as these may need to keep the
28+
connection alive for respose / response checking. Re-establishing a connection
29+
then wastes time which is noticeable in user experience.
30+
Due to this, it is important that connection be closed manually after calling
31+
sendGetToTelegram or sendPostToTelegram by calling closeClient();
32+
Failure to close connection causes memory leakage, and SSL errors on ESP32
33+
Note: closeClient() only closes connections on ESP32 (#ifdef ESP32).
34+
*/
35+
2336
#include "UniversalTelegramBot.h"
2437

2538
UniversalTelegramBot::UniversalTelegramBot(String token, Client &client) {
@@ -32,8 +45,14 @@ String UniversalTelegramBot::sendGetToTelegram(String command) {
3245
long now;
3346
bool avail;
3447

35-
// Connect with api.telegram.org
36-
if (client->connect(HOST, SSL_PORT)) {
48+
// Connect with api.telegram.org if not already connected
49+
if(!client->connected()){
50+
if (_debug) Serial.println(F("[BOT]Connecting to server"));
51+
if(!client->connect(HOST, SSL_PORT)){
52+
if (_debug) Serial.println(F("[BOT]Conection error"));
53+
}
54+
}
55+
if (client->connected()) {
3756

3857
if (_debug) Serial.println(F(".... connected to server"));
3958

@@ -74,8 +93,14 @@ String UniversalTelegramBot::sendPostToTelegram(String command, JsonObject& payl
7493
long now;
7594
bool responseReceived;
7695

77-
// Connect with api.telegram.org
78-
if (client->connect(HOST, SSL_PORT)) {
96+
// Connect with api.telegram.org if not already connected
97+
if(!client->connected()){
98+
if (_debug) Serial.println(F("[BOT Client]Connecting to server"));
99+
if(!client->connect(HOST, SSL_PORT)){
100+
if (_debug) Serial.println(F("[BOT Client]Conection error"));
101+
}
102+
}
103+
if (client->connected()) {
79104
// POST URI
80105
client->print("POST /" + command); client->println(F(" HTTP/1.1"));
81106
// Host header
@@ -153,8 +178,15 @@ String UniversalTelegramBot::sendMultipartFormDataToTelegram(String command, Str
153178
long now;
154179
bool responseReceived;
155180
String boundry = F("------------------------b8f610217e83e29b");
156-
// Connect with api.telegram.org
157-
if (client->connect(HOST, SSL_PORT)) {
181+
182+
// Connect with api.telegram.org if not already connected
183+
if(!client->connected()){
184+
if (_debug) Serial.println(F("[BOT Client]Connecting to server"));
185+
if(!client->connect(HOST, SSL_PORT)){
186+
if (_debug) Serial.println(F("[BOT Client]Conection error"));
187+
}
188+
}
189+
if (client->connected()) {
158190

159191
String start_request = "";
160192
String end_request = "";
@@ -262,6 +294,7 @@ String UniversalTelegramBot::sendMultipartFormDataToTelegram(String command, Str
262294
}
263295
}
264296

297+
closeClient();
265298
return body;
266299
}
267300

@@ -271,6 +304,8 @@ bool UniversalTelegramBot::getMe() {
271304
DynamicJsonBuffer jsonBuffer;
272305
JsonObject& root = jsonBuffer.parseObject(response);
273306

307+
closeClient();
308+
274309
if(root.success()) {
275310
if (root.containsKey("result")) {
276311
String _name = root["result"]["first_name"];
@@ -301,6 +336,8 @@ int UniversalTelegramBot::getUpdates(long offset) {
301336

302337
if (response == "") {
303338
if(_debug) Serial.println(F("Received empty string in response!"));
339+
// close the client as there's nothing to do with an empty string
340+
closeClient();
304341
return 0;
305342
}
306343
else {
@@ -328,18 +365,25 @@ int UniversalTelegramBot::getUpdates(long offset) {
328365
newMessageIndex++;
329366
}
330367
}
368+
// We will keep the client open because there may be a response to be given
331369
return newMessageIndex;
332370
} else {
333371
if (_debug) Serial.println(F("no new messages"));
334372
}
335373
} else {
336374
if (_debug) Serial.println(F("Response contained no 'result'"));
337375
}
338-
} else {
339-
// Buffer may not be big enough, increase buffer or reduce max number of messages
340-
if (_debug) Serial.println(F("Failed to parse update, the message could be too big for the buffer"));
376+
} else { // Parsing failed
377+
if(response.length() < 2){ // Too short a message. Maybe connection issue
378+
if (_debug) Serial.println(F("Parsing error: Message too short"));
379+
}
380+
else{
381+
// Buffer may not be big enough, increase buffer or reduce max number of messages
382+
if (_debug) Serial.println(F("Failed to parse update, the message could be too big for the buffer"));
383+
}
341384
}
342-
385+
// Close the client as no response is to be given
386+
closeClient();
343387
return 0;
344388
}
345389
}
@@ -441,7 +485,7 @@ bool UniversalTelegramBot::sendSimpleMessage(String chat_id, String text, String
441485
}
442486
}
443487
}
444-
488+
closeClient();
445489
return sent;
446490
}
447491

@@ -540,6 +584,7 @@ bool UniversalTelegramBot::sendPostMessage(JsonObject& payload) {
540584
}
541585
}
542586

587+
closeClient();
543588
return sent;
544589
}
545590

@@ -562,6 +607,7 @@ String UniversalTelegramBot::sendPostPhoto(JsonObject& payload) {
562607
}
563608
}
564609

610+
closeClient();
565611
return response;
566612
}
567613

@@ -642,5 +688,15 @@ bool UniversalTelegramBot::sendChatAction(String chat_id, String text) {
642688
}
643689
}
644690

691+
closeClient();
645692
return sent;
646693
}
694+
695+
void UniversalTelegramBot::closeClient(){
696+
#ifdef ESP32
697+
if(client->connected()){
698+
if(_debug){Serial.println(F("Closing client")); }
699+
client->stop();
700+
}
701+
#endif
702+
}

src/UniversalTelegramBot.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class UniversalTelegramBot
9393
String _token;
9494
Client *client;
9595
bool processResult(JsonObject& result, int messageIndex);
96+
void closeClient();
9697
const int maxMessageLength = 1300;
9798
};
9899

0 commit comments

Comments
 (0)