@@ -49,6 +49,15 @@ void UniversalTelegramBot::updateToken(String token) {
49
49
_token = token;
50
50
}
51
51
52
+ String UniversalTelegramBot::buildCommand (String cmd) {
53
+ String command = F (" bot" );
54
+ command += _token;
55
+ command += F (" /" );
56
+ command += cmd;
57
+
58
+ return command;
59
+ }
60
+
52
61
String UniversalTelegramBot::sendGetToTelegram (String command) {
53
62
String mess = " " ;
54
63
long now;
@@ -329,8 +338,7 @@ String UniversalTelegramBot::sendMultipartFormDataToTelegram(
329
338
330
339
331
340
bool UniversalTelegramBot::getMe () {
332
- String command = " bot" + _token + " /getMe" ;
333
- String response = sendGetToTelegram (command); // receive reply from telegram.org
341
+ String response = sendGetToTelegram (buildCommand (F (" getMe" ))); // receive reply from telegram.org
334
342
DynamicJsonDocument doc (maxMessageLength);
335
343
DeserializationError error = deserializeJson (doc, ZERO_COPY (response));
336
344
closeClient ();
@@ -363,8 +371,7 @@ bool UniversalTelegramBot::setMyCommands(const String& commandArray) {
363
371
unsigned long sttime = millis ();
364
372
365
373
while (millis () < sttime + 8000ul ) { // loop for a while to send the message
366
- String command = " bot" + _token + " /setMyCommands" ;
367
- response = sendPostToTelegram (command, payload.as <JsonObject>());
374
+ response = sendPostToTelegram (buildCommand (F (" setMyCommands" )), payload.as <JsonObject>());
368
375
#ifdef _debug
369
376
Serial.println (" setMyCommands response" + response);
370
377
#endif
@@ -387,9 +394,14 @@ int UniversalTelegramBot::getUpdates(long offset) {
387
394
#ifdef _debug
388
395
Serial.println (F (" GET Update Messages" ));
389
396
#endif
390
- String command = " bot" + _token + " /getUpdates?offset=" + String (offset) + " &limit=" + String (HANDLE_MESSAGES);
397
+ String command = buildCommand (F (" getUpdates?offset=" ));
398
+ command += offset;
399
+ command += F (" &limit=" );
400
+ command += HANDLE_MESSAGES;
401
+
391
402
if (longPoll > 0 ) {
392
- command = command + " &timeout=" + String (longPoll);
403
+ command += F (" &timeout=" );
404
+ command += String (longPoll);
393
405
}
394
406
String response = sendGetToTelegram (command); // receive reply from telegram.org
395
407
@@ -564,8 +576,12 @@ bool UniversalTelegramBot::sendSimpleMessage(String chat_id, String text,
564
576
565
577
if (text != " " ) {
566
578
while (millis () < sttime + 8000 ) { // loop for a while to send the message
567
- String command = " bot" + _token + " /sendMessage?chat_id=" + chat_id +
568
- " &text=" + text + " &parse_mode=" + parse_mode;
579
+ String command = buildCommand (F (" sendMessage?chat_id=" ));
580
+ command += chat_id;
581
+ command += F (" &text=" );
582
+ command += text;
583
+ command += F (" &parse_mode=" );
584
+ command += parse_mode;
569
585
String response = sendGetToTelegram (command);
570
586
#ifdef _debug
571
587
Serial.println (response);
@@ -653,8 +669,7 @@ bool UniversalTelegramBot::sendPostMessage(JsonObject payload) {
653
669
654
670
if (payload.containsKey (" text" )) {
655
671
while (millis () < sttime + 8000 ) { // loop for a while to send the message
656
- String command = " bot" + _token + " /sendMessage" ;
657
- String response = sendPostToTelegram (command, payload);
672
+ String response = sendPostToTelegram (buildCommand (F (" sendMessage" )), payload);
658
673
#ifdef _debug
659
674
Serial.println (response);
660
675
#endif
@@ -678,8 +693,7 @@ String UniversalTelegramBot::sendPostPhoto(JsonObject payload) {
678
693
679
694
if (payload.containsKey (" photo" )) {
680
695
while (millis () < sttime + 8000 ) { // loop for a while to send the message
681
- String command = " bot" + _token + " /sendPhoto" ;
682
- response = sendPostToTelegram (command, payload);
696
+ response = sendPostToTelegram (buildCommand (F (" sendPhoto" )), payload);
683
697
#ifdef _debug
684
698
Serial.println (response);
685
699
#endif
@@ -762,8 +776,11 @@ bool UniversalTelegramBot::sendChatAction(String chat_id, String text) {
762
776
763
777
if (text != " " ) {
764
778
while (millis () < sttime + 8000 ) { // loop for a while to send the message
765
- String command = " bot" + _token + " /sendChatAction?chat_id=" + chat_id +
766
- " &action=" + text;
779
+ String command = buildCommand (F (" sendChatAction?chat_id=" ));
780
+ command += chat_id;
781
+ command += F (" &action=" );
782
+ command += text;
783
+
767
784
String response = sendGetToTelegram (command);
768
785
769
786
#ifdef _debug
@@ -791,15 +808,17 @@ void UniversalTelegramBot::closeClient() {
791
808
792
809
bool UniversalTelegramBot::getFile (String *file_path, long *file_size, String file_id)
793
810
{
794
- String command = " bot" + _token + " /getFile?file_id=" + file_id;
811
+ String command = buildCommand (F (" getFile?file_id=" ));
812
+ command += file_id;
795
813
String response = sendGetToTelegram (command); // receive reply from telegram.org
796
814
DynamicJsonDocument doc (maxMessageLength);
797
815
DeserializationError error = deserializeJson (doc, ZERO_COPY (response));
798
816
closeClient ();
799
817
800
818
if (!error) {
801
819
if (doc.containsKey (" result" )) {
802
- *file_path = " https://api.telegram.org/file/bot" + _token + " /" + doc[" result" ][" file_path" ].as <String>();
820
+ *file_path = F (" https://api.telegram.org/file/bot" );
821
+ *file_path += buildCommand (doc[" result" ][" file_path" ]);
803
822
*file_size = doc[" result" ][" file_size" ].as <long >();
804
823
return true ;
805
824
}
0 commit comments