Skip to content

Commit 76464a8

Browse files
committed
issue #5: factored out base URL creation for commands
1 parent 7588b46 commit 76464a8

File tree

2 files changed

+37
-16
lines changed

2 files changed

+37
-16
lines changed

src/UniversalTelegramBot.cpp

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ void UniversalTelegramBot::updateToken(String token) {
4949
_token = token;
5050
}
5151

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+
5261
String UniversalTelegramBot::sendGetToTelegram(String command) {
5362
String mess = "";
5463
long now;
@@ -329,8 +338,7 @@ String UniversalTelegramBot::sendMultipartFormDataToTelegram(
329338

330339

331340
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
334342
DynamicJsonDocument doc(maxMessageLength);
335343
DeserializationError error = deserializeJson(doc, ZERO_COPY(response));
336344
closeClient();
@@ -363,8 +371,7 @@ bool UniversalTelegramBot::setMyCommands(const String& commandArray) {
363371
unsigned long sttime = millis();
364372

365373
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>());
368375
#ifdef _debug
369376
Serial.println("setMyCommands response" + response);
370377
#endif
@@ -387,9 +394,14 @@ int UniversalTelegramBot::getUpdates(long offset) {
387394
#ifdef _debug
388395
Serial.println(F("GET Update Messages"));
389396
#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+
391402
if (longPoll > 0) {
392-
command = command + "&timeout=" + String(longPoll);
403+
command += F("&timeout=");
404+
command += String(longPoll);
393405
}
394406
String response = sendGetToTelegram(command); // receive reply from telegram.org
395407

@@ -564,8 +576,12 @@ bool UniversalTelegramBot::sendSimpleMessage(String chat_id, String text,
564576

565577
if (text != "") {
566578
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;
569585
String response = sendGetToTelegram(command);
570586
#ifdef _debug
571587
Serial.println(response);
@@ -653,8 +669,7 @@ bool UniversalTelegramBot::sendPostMessage(JsonObject payload) {
653669

654670
if (payload.containsKey("text")) {
655671
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);
658673
#ifdef _debug
659674
Serial.println(response);
660675
#endif
@@ -678,8 +693,7 @@ String UniversalTelegramBot::sendPostPhoto(JsonObject payload) {
678693

679694
if (payload.containsKey("photo")) {
680695
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);
683697
#ifdef _debug
684698
Serial.println(response);
685699
#endif
@@ -762,8 +776,11 @@ bool UniversalTelegramBot::sendChatAction(String chat_id, String text) {
762776

763777
if (text != "") {
764778
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+
767784
String response = sendGetToTelegram(command);
768785

769786
#ifdef _debug
@@ -791,15 +808,17 @@ void UniversalTelegramBot::closeClient() {
791808

792809
bool UniversalTelegramBot::getFile(String *file_path, long *file_size, String file_id)
793810
{
794-
String command = "bot" + _token + "/getFile?file_id=" + file_id;
811+
String command = buildCommand(F("getFile?file_id="));
812+
command += file_id;
795813
String response = sendGetToTelegram(command); // receive reply from telegram.org
796814
DynamicJsonDocument doc(maxMessageLength);
797815
DeserializationError error = deserializeJson(doc, ZERO_COPY(response));
798816
closeClient();
799817

800818
if (!error) {
801819
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"]);
803822
*file_size = doc["result"]["file_size"].as<long>();
804823
return true;
805824
}

src/UniversalTelegramBot.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ class UniversalTelegramBot {
103103

104104
bool setMyCommands(const String& commandArray);
105105

106+
String buildCommand(String cmd);
107+
106108
int getUpdates(long offset);
107109
bool checkForOkResponse(String &response);
108110
telegramMessage messages[HANDLE_MESSAGES];

0 commit comments

Comments
 (0)