Skip to content

Commit abd6d97

Browse files
committed
Added function deleteMessage(), maxMessageLength and fixes for disable_web_page_preview and debug
1 parent e12c0dd commit abd6d97

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

src/UniversalTelegramBot.cpp

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@
3838
#define ZERO_COPY(STR) ((char*)STR.c_str())
3939
#define BOT_CMD(STR) buildCommand(F(STR))
4040

41-
UniversalTelegramBot::UniversalTelegramBot(const String& token, Client &client) {
41+
UniversalTelegramBot::UniversalTelegramBot(const String& token, Client &client, int maxMessageLength) {
4242
updateToken(token);
4343
this->client = &client;
44+
this->maxMessageLength = maxMessageLength;
4445
}
4546

4647
void UniversalTelegramBot::updateToken(const String& token) {
@@ -328,15 +329,15 @@ bool UniversalTelegramBot::setMyCommands(const String& commandArray) {
328329
payload["commands"] = serialized(commandArray);
329330
bool sent = false;
330331
String response = "";
331-
#if defined(_debug)
332-
Serial.println(F("sendSetMyCommands: SEND Post /setMyCommands"));
333-
#endif // defined(_debug)
332+
#ifdef TELEGRAM_DEBUG
333+
Serial.println(F("sendSetMyCommands: SEND Post /setMyCommands"));
334+
#endif
334335
unsigned long sttime = millis();
335336

336337
while (millis() - sttime < 8000ul) { // loop for a while to send the message
337338
response = sendPostToTelegram(BOT_CMD("setMyCommands"), payload.as<JsonObject>());
338-
#ifdef _debug
339-
Serial.println("setMyCommands response" + response);
339+
#ifdef TELEGRAM_DEBUG
340+
Serial.println("setMyCommands response" + response);
340341
#endif
341342
sent = checkForOkResponse(response);
342343
if (sent) break;
@@ -560,7 +561,7 @@ bool UniversalTelegramBot::sendSimpleMessage(const String& chat_id, const String
560561
}
561562

562563
bool UniversalTelegramBot::sendMessage(const String& chat_id, const String& text,
563-
const String& parse_mode, int message_id, bool disable_web_page_preview) { // added message_id
564+
const String& parse_mode, int message_id, bool disable_web_page_preview) {
564565

565566
DynamicJsonDocument payload(maxMessageLength);
566567
payload["chat_id"] = chat_id;
@@ -572,9 +573,47 @@ bool UniversalTelegramBot::sendMessage(const String& chat_id, const String& text
572573
if (parse_mode != "")
573574
payload["parse_mode"] = parse_mode;
574575

576+
if (disable_web_page_preview)
577+
payload["disable_web_page_preview"] = disable_web_page_preview;
578+
575579
return sendPostMessage(payload.as<JsonObject>(), message_id); // if message id == 0 then edit is false, else edit is true
576580
}
577581

582+
/***********************************************************************
583+
* DeleteMessage - function to delete message by message_id *
584+
* Function description and limitations: *
585+
* https://core.telegram.org/bots/api#deletemessage *
586+
***********************************************************************/
587+
bool UniversalTelegramBot::deleteMessage(const String& chat_id, int message_id) {
588+
if (message_id == 0)
589+
{
590+
#ifdef TELEGRAM_DEBUG
591+
Serial.println(F("deleteMessage: message_id not passed for deletion"));
592+
#endif
593+
return false;
594+
}
595+
596+
DynamicJsonDocument payload(maxMessageLength);
597+
payload["chat_id"] = chat_id;
598+
payload["message_id"] = message_id;
599+
600+
#ifdef TELEGRAM_DEBUG
601+
Serial.print(F("deleteMessage: SEND Post Message: "));
602+
serializeJson(payload, Serial);
603+
Serial.println();
604+
#endif
605+
606+
String response = sendPostToTelegram(BOT_CMD("deleteMessage"), payload.as<JsonObject>());
607+
#ifdef TELEGRAM_DEBUG
608+
Serial.print(F("deleteMessage response:"));
609+
Serial.println(response);
610+
#endif
611+
612+
bool sent = checkForOkResponse(response);
613+
closeClient();
614+
return sent;
615+
}
616+
578617
bool UniversalTelegramBot::sendMessageWithReplyKeyboard(
579618
const String& chat_id, const String& text, const String& parse_mode, const String& keyboard,
580619
bool resize, bool oneTime, bool selective) {
@@ -809,7 +848,7 @@ bool UniversalTelegramBot::answerCallbackQuery(const String &query_id, const Str
809848
if (url.length() > 0) payload["url"] = url;
810849

811850
String response = sendPostToTelegram(BOT_CMD("answerCallbackQuery"), payload.as<JsonObject>());
812-
#ifdef _debug
851+
#ifdef TELEGRAM_DEBUG
813852
Serial.print(F("answerCallbackQuery response:"));
814853
Serial.println(response);
815854
#endif

src/UniversalTelegramBot.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2222
#ifndef UniversalTelegramBot_h
2323
#define UniversalTelegramBot_h
2424

25+
//unmark following line to enable debug mode
2526
//#define TELEGRAM_DEBUG 1
2627
#define ARDUINOJSON_DECODE_UNICODE 1
2728
#define ARDUINOJSON_USE_LONG_LONG 1
@@ -34,9 +35,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
3435
#define TELEGRAM_SSL_PORT 443
3536
#define HANDLE_MESSAGES 1
3637

37-
//unmark following line to enable debug mode
38-
//#define _debug
39-
4038
typedef bool (*MoreDataAvailable)();
4139
typedef byte (*GetNextByte)();
4240
typedef byte* (*GetNextBuffer)();
@@ -67,7 +65,7 @@ struct telegramMessage {
6765

6866
class UniversalTelegramBot {
6967
public:
70-
UniversalTelegramBot(const String& token, Client &client);
68+
UniversalTelegramBot(const String& token, Client &client, int maxMessageLength = 1500);
7169
void updateToken(const String& token);
7270
String getToken();
7371
String sendGetToTelegram(const String& command);
@@ -86,7 +84,8 @@ class UniversalTelegramBot {
8684

8785
bool sendSimpleMessage(const String& chat_id, const String& text, const String& parse_mode);
8886
bool sendMessage(const String& chat_id, const String& text, const String& parse_mode = "", int message_id = 0,
89-
bool disable_web_page_preview = false);
87+
bool disable_web_page_preview = false);
88+
bool deleteMessage(const String& chat_id, int message_id = 0);
9089
bool sendMessageWithReplyKeyboard(const String& chat_id, const String& text,
9190
const String& parse_mode, const String& keyboard,
9291
bool resize = false, bool oneTime = false,

0 commit comments

Comments
 (0)