38
38
UniversalTelegramBot::UniversalTelegramBot (String token, Client &client) {
39
39
_token = token;
40
40
#ifdef ARDUINO_ESP8266_RELEASE_2_5_0
41
- // client->setInsecure();
41
+ // client->setInsecure();
42
42
#endif
43
43
this ->client = &client;
44
44
}
@@ -51,19 +51,19 @@ String UniversalTelegramBot::sendGetToTelegram(String command) {
51
51
// Connect with api.telegram.org if not already connected
52
52
if (!client->connected ()) {
53
53
#ifdef _debug
54
- Serial.println (F (" [BOT]Connecting to server" ));
55
- #endif
54
+ Serial.println (F (" [BOT]Connecting to server" ));
55
+ #endif
56
56
if (!client->connect (HOST, SSL_PORT)) {
57
57
#ifdef _debug
58
- Serial.println (F (" [BOT]Conection error" ));
59
- #endif
58
+ Serial.println (F (" [BOT]Conection error" ));
59
+ #endif
60
60
}
61
61
}
62
62
if (client->connected ()) {
63
63
64
64
#ifdef _debug
65
- Serial.println (F (" .... connected to server" ));
66
- #endif
65
+ Serial.println (F (" .... connected to server" ));
66
+ #endif
67
67
68
68
String a = " " ;
69
69
char c;
@@ -104,12 +104,12 @@ String UniversalTelegramBot::sendPostToTelegram(String command, JsonObject paylo
104
104
// Connect with api.telegram.org if not already connected
105
105
if (!client->connected ()) {
106
106
#ifdef _debug
107
- Serial.println (F (" [BOT Client]Connecting to server" ));
108
- #endif
107
+ Serial.println (F (" [BOT Client]Connecting to server" ));
108
+ #endif
109
109
if (!client->connect (HOST, SSL_PORT)) {
110
110
#ifdef _debug
111
- Serial.println (F (" [BOT Client]Conection error" ));
112
- #endif
111
+ Serial.println (F (" [BOT Client]Conection error" ));
112
+ #endif
113
113
}
114
114
}
115
115
if (client->connected ()) {
@@ -180,7 +180,7 @@ String UniversalTelegramBot::sendMultipartFormDataToTelegram(
180
180
String contentType, String chat_id, int fileSize,
181
181
MoreDataAvailable moreDataAvailableCallback,
182
182
GetNextByte getNextByteCallback,
183
- GetNextBuffer getNextBufferCallback,
183
+ GetNextBuffer getNextBufferCallback,
184
184
GetNextBufferLen getNextBufferLenCallback) {
185
185
186
186
String body = " " ;
@@ -195,12 +195,12 @@ String UniversalTelegramBot::sendMultipartFormDataToTelegram(
195
195
// Connect with api.telegram.org if not already connected
196
196
if (!client->connected ()) {
197
197
#ifdef _debug
198
- Serial.println (F (" [BOT Client]Connecting to server" ));
199
- #endif
198
+ Serial.println (F (" [BOT Client]Connecting to server" ));
199
+ #endif
200
200
if (!client->connect (HOST, SSL_PORT)) {
201
201
#ifdef _debug
202
- Serial.println (F (" [BOT Client]Conection error" ));
203
- #endif
202
+ Serial.println (F (" [BOT Client]Conection error" ));
203
+ #endif
204
204
}
205
205
}
206
206
if (client->connected ()) {
@@ -230,29 +230,29 @@ String UniversalTelegramBot::sendMultipartFormDataToTelegram(
230
230
231
231
int contentLength = fileSize + start_request.length () + end_request.length ();
232
232
#ifdef _debug
233
- Serial.println (" Content-Length: " + String (contentLength));
234
- #endif
233
+ Serial.println (" Content-Length: " + String (contentLength));
234
+ #endif
235
235
client->print (" Content-Length: " );
236
236
client->println (String (contentLength));
237
237
client->println (" Content-Type: multipart/form-data; boundary=" + boundry);
238
238
client->println (" " );
239
239
client->print (start_request);
240
240
241
241
#ifdef _debug
242
- Serial.print (" Start request: " + start_request);
243
- #endif
242
+ Serial.print (" Start request: " + start_request);
243
+ #endif
244
244
245
245
if (getNextByteCallback == nullptr ) {
246
246
while (moreDataAvailableCallback ()) {
247
247
client->write ((const uint8_t *)getNextBufferCallback (), getNextBufferLenCallback ());
248
248
#ifdef _debug
249
- Serial.println (F (" Sending photo from buffer" ));
250
- #endif
249
+ Serial.println (F (" Sending photo from buffer" ));
250
+ #endif
251
251
}
252
252
} else {
253
253
#ifdef _debug
254
- Serial.println (F (" Sending photo by binary" ));
255
- #endif
254
+ Serial.println (F (" Sending photo by binary" ));
255
+ #endif
256
256
byte buffer[512 ];
257
257
int count = 0 ;
258
258
char ch;
@@ -279,8 +279,8 @@ String UniversalTelegramBot::sendMultipartFormDataToTelegram(
279
279
280
280
client->print (end_request);
281
281
#ifdef _debug
282
- Serial.print (" End request: " + end_request);
283
- #endif
282
+ Serial.print (" End request: " + end_request);
283
+ #endif
284
284
int ch_count = 0 ;
285
285
now = millis ();
286
286
@@ -351,7 +351,7 @@ bool UniversalTelegramBot::getMe() {
351
351
int UniversalTelegramBot::getUpdates (long offset) {
352
352
353
353
#ifdef _debug
354
- Serial.println (F (" GET Update Messages" ));
354
+ Serial.println (F (" GET Update Messages" ));
355
355
#endif
356
356
String command = " bot" + _token + " /getUpdates?offset=" + String (offset) + " &limit=" + String (HANDLE_MESSAGES);
357
357
if (longPoll > 0 ) {
@@ -361,8 +361,8 @@ int UniversalTelegramBot::getUpdates(long offset) {
361
361
362
362
if (response == " " ) {
363
363
#ifdef _debug
364
- Serial.println (F (" Received empty string in response!" ));
365
- #endif
364
+ Serial.println (F (" Received empty string in response!" ));
365
+ #endif
366
366
// close the client as there's nothing to do with an empty string
367
367
closeClient ();
368
368
return 0 ;
@@ -376,19 +376,19 @@ int UniversalTelegramBot::getUpdates(long offset) {
376
376
// Parse response into Json object
377
377
DynamicJsonDocument doc (maxMessageLength);
378
378
DeserializationError error = deserializeJson (doc, response);
379
- #ifdef _debug
380
- Serial.print (F (" GetUpdates parsed jsonDoc: " ));
381
- serializeJson (doc, Serial);
382
- Serial.println ();
383
- #endif
384
-
379
+ #ifdef _debug
380
+ Serial.print (F (" GetUpdates parsed jsonDoc: " ));
381
+ serializeJson (doc, Serial);
382
+ Serial.println ();
383
+ #endif
384
+
385
385
JsonObject obj = doc.as <JsonObject>(); // there is nothing better right now
386
386
if (!error) {
387
387
#ifdef _debug
388
- Serial.print (F (" GetUpdates parsed jsonObj: " ));
389
- serializeJson (obj, Serial);
390
- Serial.println ();
391
- #endif
388
+ Serial.print (F (" GetUpdates parsed jsonObj: " ));
389
+ serializeJson (obj, Serial);
390
+ Serial.println ();
391
+ #endif
392
392
if (obj.containsKey (" result" )) {
393
393
int resultArrayLength = doc[" result" ].size ();
394
394
if (resultArrayLength > 0 ) {
@@ -403,27 +403,27 @@ int UniversalTelegramBot::getUpdates(long offset) {
403
403
return newMessageIndex;
404
404
} else {
405
405
#ifdef _debug
406
- Serial.println (F (" no new messages" ));
407
- #endif
406
+ Serial.println (F (" no new messages" ));
407
+ #endif
408
408
}
409
409
} else {
410
410
#ifdef _debug
411
- Serial.println (F (" Response contained no 'result'" ));
412
- #endif
411
+ Serial.println (F (" Response contained no 'result'" ));
412
+ #endif
413
413
}
414
414
} else { // Parsing failed
415
415
if (response.length () < 2 ) { // Too short a message. Maybe a connection issue
416
416
#ifdef _debug
417
- Serial.println (F (" Parsing error: Message too short" ));
418
- #endif
417
+ Serial.println (F (" Parsing error: Message too short" ));
418
+ #endif
419
419
} else {
420
420
// Buffer may not be big enough, increase buffer or reduce max number of
421
421
// messages
422
422
#ifdef _debug
423
- Serial.print (F (" Failed to parse update, the message could be too "
423
+ Serial.print (F (" Failed to parse update, the message could be too "
424
424
" big for the buffer. Error code: " ));
425
- Serial.println (error.c_str ()); // debug print of parsing error
426
- #endif
425
+ Serial.println (error.c_str ()); // debug print of parsing error
426
+ #endif
427
427
}
428
428
}
429
429
// Close the client as no response is to be given
@@ -517,7 +517,7 @@ bool UniversalTelegramBot::sendSimpleMessage(String chat_id, String text,
517
517
518
518
bool sent = false ;
519
519
#ifdef _debug
520
- Serial.println (F (" sendSimpleMessage: SEND Simple Message" ));
520
+ Serial.println (F (" sendSimpleMessage: SEND Simple Message" ));
521
521
#endif
522
522
long sttime = millis ();
523
523
@@ -527,8 +527,8 @@ bool UniversalTelegramBot::sendSimpleMessage(String chat_id, String text,
527
527
" &text=" + text + " &parse_mode=" + parse_mode;
528
528
String response = sendGetToTelegram (command);
529
529
#ifdef _debug
530
- Serial.println (response);
531
- #endif
530
+ Serial.println (response);
531
+ #endif
532
532
sent = checkForOkResponse (response);
533
533
if (sent) break ;
534
534
}
@@ -614,8 +614,8 @@ bool UniversalTelegramBot::sendPostMessage(JsonObject payload) {
614
614
bool sent = false ;
615
615
#ifdef _debug
616
616
Serial.print (F (" sendPostMessage: SEND Post Message: " ));
617
- serializeJson (payload, Serial);
618
- Serial.println ();
617
+ serializeJson (payload, Serial);
618
+ Serial.println ();
619
619
#endif
620
620
long sttime = millis ();
621
621
@@ -624,8 +624,8 @@ bool UniversalTelegramBot::sendPostMessage(JsonObject payload) {
624
624
String command = " bot" + _token + " /sendMessage" ;
625
625
String response = sendPostToTelegram (command, payload);
626
626
#ifdef _debug
627
- Serial.println (response);
628
- #endif
627
+ Serial.println (response);
628
+ #endif
629
629
sent = checkForOkResponse (response);
630
630
if (sent) break ;
631
631
}
@@ -640,7 +640,7 @@ String UniversalTelegramBot::sendPostPhoto(JsonObject payload) {
640
640
bool sent = false ;
641
641
String response = " " ;
642
642
#ifdef _debug
643
- Serial.println (F (" sendPostPhoto: SEND Post Photo" ));
643
+ Serial.println (F (" sendPostPhoto: SEND Post Photo" ));
644
644
#endif
645
645
long sttime = millis ();
646
646
@@ -649,8 +649,8 @@ String UniversalTelegramBot::sendPostPhoto(JsonObject payload) {
649
649
String command = " bot" + _token + " /sendPhoto" ;
650
650
response = sendPostToTelegram (command, payload);
651
651
#ifdef _debug
652
- Serial.println (response);
653
- #endif
652
+ Serial.println (response);
653
+ #endif
654
654
sent = checkForOkResponse (response);
655
655
if (sent) break ;
656
656
@@ -667,15 +667,15 @@ String UniversalTelegramBot::sendPhotoByBinary(
667
667
GetNextByte getNextByteCallback, GetNextBuffer getNextBufferCallback, GetNextBufferLen getNextBufferLenCallback) {
668
668
669
669
#ifdef _debug
670
- Serial.println (F (" sendPhotoByBinary: SEND Photo" ));
670
+ Serial.println (F (" sendPhotoByBinary: SEND Photo" ));
671
671
#endif
672
672
673
673
String response = sendMultipartFormDataToTelegram (" sendPhoto" , " photo" , " img.jpg" ,
674
674
contentType, chat_id, fileSize,
675
675
moreDataAvailableCallback, getNextByteCallback, getNextBufferCallback, getNextBufferLenCallback);
676
676
677
677
#ifdef _debug
678
- Serial.println (response);
678
+ Serial.println (response);
679
679
#endif
680
680
681
681
return response;
@@ -727,7 +727,7 @@ bool UniversalTelegramBot::sendChatAction(String chat_id, String text) {
727
727
728
728
bool sent = false ;
729
729
#ifdef _debug
730
- Serial.println (F (" SEND Chat Action Message" ));
730
+ Serial.println (F (" SEND Chat Action Message" ));
731
731
#endif
732
732
long sttime = millis ();
733
733
@@ -738,8 +738,8 @@ bool UniversalTelegramBot::sendChatAction(String chat_id, String text) {
738
738
String response = sendGetToTelegram (command);
739
739
740
740
#ifdef _debug
741
- Serial.println (response);
742
- #endif
741
+ Serial.println (response);
742
+ #endif
743
743
sent = checkForOkResponse (response);
744
744
745
745
if (sent) break ;
@@ -754,9 +754,9 @@ bool UniversalTelegramBot::sendChatAction(String chat_id, String text) {
754
754
void UniversalTelegramBot::closeClient () {
755
755
if (client->connected ()) {
756
756
#ifdef _debug
757
- Serial.println (F (" Closing client" ));
758
- #endif
759
- client->stop ();
757
+ Serial.println (F (" Closing client" ));
758
+ #endif
759
+ client->stop ();
760
760
}
761
761
}
762
762
0 commit comments