Skip to content

Commit df02494

Browse files
committed
Merge remote-tracking branch 'didgerihorn/V1.2.0'
2 parents 74f39db + ce41193 commit df02494

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

src/UniversalTelegramBot.cpp

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,54 @@ bool UniversalTelegramBot::getMe() {
343343
return false;
344344
}
345345

346+
/*********************************************************************************
347+
* SetMyCommands - Update the command list of the bot on the telegram server *
348+
* (Argument to pass: Serialied array of BotCommand) *
349+
* CAUTION: All commands must be lower-case *
350+
* Returns true, if the command list was updated successfully *
351+
********************************************************************************/
352+
bool UniversalTelegramBot::setMyCommands(const String& commandArray) {
353+
if (commandArray.isEmpty()) {
354+
#if defined(_debug)
355+
Serial.println(F("sendSetMyCommands: commandArray is empty"));
356+
#endif // defined(_debug)
357+
return false;
358+
}
359+
360+
DynamicJsonDocument _commandArray(maxMessageLength);
361+
DeserializationError err = deserializeJson(_commandArray, commandArray);
362+
if (err) {
363+
#if defined(_debug)
364+
Serial.println(F("sendSetMyCommands: Deserialization Error"));
365+
Serial.println(commandArray);
366+
#endif // defined(_debug)
367+
return false;
368+
}
369+
370+
DynamicJsonDocument payload(maxMessageLength);
371+
payload["commands"] = serialized(commandArray);
372+
bool sent = false;
373+
String response = "";
374+
#if defined(_debug)
375+
Serial.println(F("sendSetMyCommands: SEND Post /setMyCommands"));
376+
#endif // defined(_debug)
377+
unsigned long sttime = millis();
378+
379+
while (millis() < sttime + 8000ul) { // loop for a while to send the message
380+
String command = "bot" + _token + "/setMyCommands";
381+
response = sendPostToTelegram(command, payload.as<JsonObject>());
382+
#ifdef _debug
383+
Serial.println("setMyCommands response" + response);
384+
#endif
385+
sent = checkForOkResponse(response);
386+
if (sent) break;
387+
388+
}
389+
closeClient();
390+
return response;
391+
}
392+
393+
346394
/***************************************************************
347395
* GetUpdates - function to receive messages from telegram *
348396
* (Argument to pass: the last+1 message to read) *
@@ -691,7 +739,7 @@ String UniversalTelegramBot::sendPhoto(String chat_id, String photo,
691739
payload["chat_id"] = chat_id;
692740
payload["photo"] = photo;
693741

694-
if (caption)
742+
if (!caption.isEmpty())
695743
payload["caption"] = caption;
696744

697745
if (disable_notification)
@@ -700,7 +748,7 @@ String UniversalTelegramBot::sendPhoto(String chat_id, String photo,
700748
if (reply_to_message_id && reply_to_message_id != 0)
701749
payload["reply_to_message_id"] = reply_to_message_id;
702750

703-
if (keyboard) {
751+
if (!keyboard.isEmpty()) {
704752
JsonObject replyMarkup = payload.createNestedObject("reply_markup");
705753
DynamicJsonDocument keyboardBuffer(maxMessageLength); // assuming keyboard buffer will alwas be limited to 1024 bytes
706754
deserializeJson(keyboardBuffer, keyboard);

src/UniversalTelegramBot.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ class UniversalTelegramBot {
9797
bool disable_notification = false,
9898
int reply_to_message_id = 0, String keyboard = "");
9999

100+
bool setMyCommands(const String& commandArray);
101+
100102
int getUpdates(long offset);
101103
bool checkForOkResponse(String response);
102104
telegramMessage messages[HANDLE_MESSAGES];

0 commit comments

Comments
 (0)