38
38
#define ZERO_COPY (STR ) ((char *)STR.c_str())
39
39
#define BOT_CMD (STR ) buildCommand(F(STR))
40
40
41
- UniversalTelegramBot::UniversalTelegramBot (const String& token, Client &client) {
41
+ UniversalTelegramBot::UniversalTelegramBot (const String& token, Client &client, int maxMessageLength ) {
42
42
updateToken (token);
43
43
this ->client = &client;
44
+ this ->maxMessageLength = maxMessageLength;
44
45
}
45
46
46
47
void UniversalTelegramBot::updateToken (const String& token) {
@@ -328,15 +329,15 @@ bool UniversalTelegramBot::setMyCommands(const String& commandArray) {
328
329
payload[" commands" ] = serialized (commandArray);
329
330
bool sent = false ;
330
331
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
334
335
unsigned long sttime = millis ();
335
336
336
337
while (millis () - sttime < 8000ul ) { // loop for a while to send the message
337
338
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);
340
341
#endif
341
342
sent = checkForOkResponse (response);
342
343
if (sent) break ;
@@ -560,7 +561,7 @@ bool UniversalTelegramBot::sendSimpleMessage(const String& chat_id, const String
560
561
}
561
562
562
563
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) {
564
565
565
566
DynamicJsonDocument payload (maxMessageLength);
566
567
payload[" chat_id" ] = chat_id;
@@ -572,9 +573,47 @@ bool UniversalTelegramBot::sendMessage(const String& chat_id, const String& text
572
573
if (parse_mode != " " )
573
574
payload[" parse_mode" ] = parse_mode;
574
575
576
+ if (disable_web_page_preview)
577
+ payload[" disable_web_page_preview" ] = disable_web_page_preview;
578
+
575
579
return sendPostMessage (payload.as <JsonObject>(), message_id); // if message id == 0 then edit is false, else edit is true
576
580
}
577
581
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
+
578
617
bool UniversalTelegramBot::sendMessageWithReplyKeyboard (
579
618
const String& chat_id, const String& text, const String& parse_mode, const String& keyboard,
580
619
bool resize, bool oneTime, bool selective) {
@@ -809,7 +848,7 @@ bool UniversalTelegramBot::answerCallbackQuery(const String &query_id, const Str
809
848
if (url.length () > 0 ) payload[" url" ] = url;
810
849
811
850
String response = sendPostToTelegram (BOT_CMD (" answerCallbackQuery" ), payload.as <JsonObject>());
812
- #ifdef _debug
851
+ #ifdef TELEGRAM_DEBUG
813
852
Serial.print (F (" answerCallbackQuery response:" ));
814
853
Serial.println (response);
815
854
#endif
0 commit comments