Skip to content

Commit e9b3dca

Browse files
committed
Do not pring debug code and added _debug variable for better use
1 parent 19d7aa5 commit e9b3dca

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

src/UniversalTelegramBot.cpp

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ String UniversalTelegramBot::sendGetToTelegram(String command) {
3535
bool avail;
3636
// Connect with api.telegram.org
3737
if (client->connect(HOST, SSL_PORT)) {
38-
// Serial.println(".... connected to server");
38+
if (_debug) Serial.println(".... connected to server");
3939
String a="";
4040
char c;
4141
int ch_count=0;
@@ -53,9 +53,11 @@ String UniversalTelegramBot::sendGetToTelegram(String command) {
5353
avail=true;
5454
}
5555
if (avail) {
56-
// Serial.println();
57-
// Serial.println(mess);
58-
// Serial.println();
56+
if (_debug) {
57+
Serial.println();
58+
Serial.println(mess);
59+
Serial.println();
60+
}
5961
break;
6062
}
6163
}
@@ -102,9 +104,11 @@ String UniversalTelegramBot::sendPostToTelegram(String command, JsonObject& payl
102104
responseRecieved=true;
103105
}
104106
if (responseRecieved) {
105-
// Serial.println();
106-
// Serial.println(response);
107-
// Serial.println();
107+
if (_debug) {
108+
Serial.println();
109+
Serial.println(response);
110+
Serial.println();
111+
}
108112
break;
109113
}
110114
}
@@ -145,14 +149,16 @@ bool UniversalTelegramBot::getMe() {
145149

146150
int UniversalTelegramBot::getUpdates(long offset) {
147151

148-
//Serial.println("GET Update Messages ");
152+
if (_debug) Serial.println("GET Update Messages ");
149153
String command="bot"+_token+"/getUpdates?offset="+String(offset)+"&limit="+String(HANDLE_MESSAGES);
150154
String response = sendGetToTelegram(command); //recieve reply from telegram.org
151155
if (response != "") {
152-
// Serial.print("incoming message length");
153-
// Serial.println(response.length());
154-
// Serial.print("Creating StaticJsonBuffer of size: ");
155-
// Serial.println(MAX_BUFFER_SIZE);
156+
if (_debug) {
157+
Serial.print("incoming message length");
158+
Serial.println(response.length());
159+
Serial.print("Creating StaticJsonBuffer of size: ");
160+
Serial.println(MAX_BUFFER_SIZE);
161+
}
156162

157163
// Parse response into Json object
158164
//StaticJsonBuffer<MAX_BUFFER_SIZE> jsonBuffer;
@@ -161,7 +167,7 @@ int UniversalTelegramBot::getUpdates(long offset) {
161167

162168
if(root.success()) {
163169
// root.printTo(Serial);
164-
// Serial.println();
170+
if (_debug) Serial.println();
165171
if (root.containsKey("result")) {
166172
int resultArrayLength = root["result"].size();
167173
if(resultArrayLength > 0) {
@@ -186,14 +192,14 @@ int UniversalTelegramBot::getUpdates(long offset) {
186192
}
187193
return newMessageIndex;
188194
} else {
189-
//Serial.println("no new messages");
195+
if (_debug) Serial.println("no new messages");
190196
}
191197
} else {
192-
Serial.println("Response contained no 'result'");
198+
if (_debug) Serial.println("Response contained no 'result'");
193199
}
194200
} else {
195201
// Buffer may not be big enough, increase buffer or reduce max number of messages
196-
Serial.println("Failed to parse update, the message could be too big for the buffer");
202+
if (_debug) Serial.println("Failed to parse update, the message could be too big for the buffer");
197203
}
198204

199205
return 0;
@@ -207,13 +213,13 @@ int UniversalTelegramBot::getUpdates(long offset) {
207213
bool UniversalTelegramBot::sendSimpleMessage(String chat_id, String text, String parse_mode) {
208214

209215
bool sent=false;
210-
Serial.println("SEND Simple Message ");
216+
if (_debug) Serial.println("SEND Simple Message ");
211217
long sttime=millis();
212218
if (text!="") {
213219
while (millis()<sttime+8000) { // loop for a while to send the message
214220
String command="bot"+_token+"/sendMessage?chat_id="+chat_id+"&text="+text+"&parse_mode="+parse_mode;
215221
String response = sendGetToTelegram(command);
216-
//Serial.println(response);
222+
if (_debug) Serial.println(response);
217223
sent = checkForOkResponse(response);
218224
if(sent){
219225
break;
@@ -281,13 +287,13 @@ bool UniversalTelegramBot::sendMessageWithReplyKeyboard(String chat_id, String t
281287
bool UniversalTelegramBot::sendPostMessage(JsonObject& payload) {
282288

283289
bool sent=false;
284-
Serial.println("SEND Post Message ");
290+
if (_debug) Serial.println("SEND Post Message ");
285291
long sttime=millis();
286292
if (payload.containsKey("text")) {
287293
while (millis()<sttime+8000) { // loop for a while to send the message
288294
String command = "bot"+_token+"/sendMessage";
289295
String response = sendPostToTelegram(command, payload);
290-
//Serial.println(response);
296+
if (_debug) Serial.println(response);
291297
sent = checkForOkResponse(response);
292298
if(sent){
293299
break;

src/UniversalTelegramBot.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class UniversalTelegramBot
6666
Client *client;
6767
const int maxMessageLength = 1000;
6868
bool checkForOkResponse(String response);
69+
bool _debug = false;
6970
};
7071

7172
#endif

0 commit comments

Comments
 (0)