4
1
mirror of https://github.com/QuasarApp/qTbot.git synced 2025-05-08 11:19:44 +00:00

added functions for working with a keyboard

This commit is contained in:
Andrei Yankovich 2023-10-08 13:45:49 +02:00
parent 3de4578043
commit bcad34ef98
21 changed files with 507 additions and 99 deletions

@ -35,6 +35,7 @@ set(QTBOT_PACKAGE_ID "quasarapp.core.qTbot")
option(QTBOT_TESTS "This option disables or enables tests of the ${PROJECT_NAME} project" ON)
option(QTBOT_EXAMPLE "This option disables or enables example app of the ${PROJECT_NAME} project" ON)
option(QTBOT_PRINT_RQUESTS "This option disables or enables printing requests" OFF)
if (ANDROID OR IOS OR QA_WASM32)
set(QTBOT_TESTS OFF CACHE BOOL "This option force disbled for ANDROID IOS QA_WASM32 and Not Qt projects" FORCE)

@ -44,9 +44,21 @@ int main(int argc, char *argv[]) {
if (tmsg->contains(tmsg->Audio)) {
filesStack.push_back(bot.getFile(tmsg->audio()->fileId(), qTbot::iFile::Local));
}
bot.sendSpecificMessageWithKeyboard(tmsg->chatId(), "I see it - я вижу это", {{{"test_button", [tmsg, &bot](const QString& queryId){
bot.sendSpecificMessage(tmsg->chatId(), "stop to presse a button",{}, queryId);
}}}}, {}, false, false, tmsg->messageId());
bot.sendSpecificMessageWithKeyboard(tmsg->chatId(),
"I see it - я вижу это",
{{{"test_button", [tmsg, &bot](const QString& queryId, const QVariant& msgId){
static int index = 0;
bot.editSpecificMessageWithKeyboard(msgId,
tmsg->chatId(),
"I see it - я вижу это. Presedd count: " + QString::number(index++), true, false,
{{{"test_button", [](auto , auto ){}}, {"test_button 2", [](auto , auto ){}}}},
queryId);
}}}}, {}, false, false, tmsg->messageId());
bot.sendSpecificMessageWithKeyboard(tmsg->chatId(),
"I see it - я вижу это (интерактивная клавиатура)",
{{{"test_button"},
{"test_button"},}}, {}, true, true, tmsg->messageId());
}
}

@ -10,6 +10,9 @@ cmake_minimum_required(VERSION 3.19)
set(CURRENT_PROJECT "${PROJECT_NAME}")
add_definitions(-DQTBOT_LIBRARY)
if (QTBOT_PRINT_RQUESTS)
add_definitions(-DQTBOT_PRINT_RQUESTS)
endif()
file(GLOB_RECURSE SOURCE_CPP
"src/*.cpp"

@ -0,0 +1,19 @@
//#
//# Copyright (C) 2023-2023 QuasarApp.
//# Distributed under the GPLv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#include "telegramdeletemessage.h"
namespace qTbot {
TelegramDeleteMessage::TelegramDeleteMessage(const QVariant &chatId,
const QVariant &messageId):
TelegramSingleRquest("deleteMessage")
{
addArg("chat_id", chatId);
addArg("message_id", messageId);
}
}

@ -0,0 +1,25 @@
//#
//# Copyright (C) 2023-2023 QuasarApp.
//# Distributed under the GPLv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#ifndef TELEGRAMDELETEMESSAGE_H
#define TELEGRAMDELETEMESSAGE_H
#include "requests/telegramsinglerquest.h"
namespace qTbot {
/**
* @brief The TelegramDeleteMessage class remove message with id.
*/
class TelegramDeleteMessage: public TelegramSingleRquest
{
public:
TelegramDeleteMessage(const QVariant& chatId,
const QVariant& messageId);
};
}
#endif // TELEGRAMDELETEMESSAGE_H

@ -0,0 +1,33 @@
//#
//# Copyright (C) 2023-2023 QuasarApp.
//# Distributed under the GPLv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#include "telegrameditmessage.h"
namespace qTbot {
TelegramEditMessage::TelegramEditMessage(const QVariant &idEditedMessage,
const QVariant& chatId,
const QString& newText,
bool markdown,
bool disableWebPagePreview,
const QString& callBackQueryId,
const QMap<QString, QSharedPointer<QJsonObject>>& extraObjects):
TelegramSendMsg(chatId,
newText,
extraObjects,
0,
markdown,
callBackQueryId,
disableWebPagePreview) {
setRequest("editMessageText");
addArg("message_id", idEditedMessage);
}
}

@ -0,0 +1,29 @@
//#
//# Copyright (C) 2023-2023 QuasarApp.
//# Distributed under the GPLv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#ifndef TELEGRAMEDITMESSAGE_H
#define TELEGRAMEDITMESSAGE_H
#include "telegramsendmsg.h"
namespace qTbot {
/**
* @brief The TelegramEditMessage class This command edit keyboard of the message.
*/
class TelegramEditMessage: public TelegramSendMsg
{
public:
TelegramEditMessage(const QVariant& idEditedMessage,
const QVariant& chatId,
const QString& newText,
bool markdown = true,
bool disableWebPagePreview = false,
const QString& callBackQueryId = "",
const QMap<QString, QSharedPointer<QJsonObject>>& extraObjects = {});
};
}
#endif // TELEGRAMEDITMESSAGE_H

@ -16,7 +16,7 @@ namespace qTbot {
/**
* @brief The TelegramGetFile class build http request for the getting files from tellgram messager.
*/
class QTBOT_EXPORT TelegramGetFile final: public TelegramSingleRquest
class TelegramGetFile final: public TelegramSingleRquest
{
public:
TelegramGetFile(const QString& fileId);

@ -16,7 +16,7 @@ namespace qTbot {
/**
* @brief The TelegramGetMsg class just prepare get request to tellegram
*/
class QTBOT_EXPORT TelegramGetMe final: public TelegramSingleRquest
class TelegramGetMe final: public TelegramSingleRquest
{
public:
TelegramGetMe();

@ -17,7 +17,7 @@ namespace qTbot {
/**
* @brief The TelegramGetUpdate class This is implementation of the get update method of the Telegram API
*/
class QTBOT_EXPORT TelegramGetUpdate final: public TelegramSingleRquest
class TelegramGetUpdate final: public TelegramSingleRquest
{
public:
TelegramGetUpdate();

@ -16,7 +16,7 @@ namespace qTbot {
/**
* @brief The TelegrammDownloadFile class prepare link to download telegramm files.
*/
class QTBOT_EXPORT TelegrammDownloadFile final: public TelegramSingleRquest
class TelegrammDownloadFile final: public TelegramSingleRquest
{
public:
TelegrammDownloadFile(const QString &filePath);

@ -13,7 +13,7 @@ namespace qTbot {
TelegramSendMsg::TelegramSendMsg(const QVariant &chatId,
const QString &text,
const QMap<QString, QJsonObject> &extraObjects,
const QMap<QString, QSharedPointer<QJsonObject> > &extraObjects,
unsigned long long replyToMessageId,
bool markdown,
const QString &callBackQueryId,
@ -22,7 +22,11 @@ TelegramSendMsg::TelegramSendMsg(const QVariant &chatId,
TelegramSingleRquest("sendMessage")
{
QMap<QString, QVariant> args {{"chat_id", chatId}, {"text", text}};
QMap<QString, QVariant> args {{"chat_id", chatId}};
if (text.size()) {
args["text"] = text;
}
if (replyToMessageId) {
args["reply_to_message_id"] = replyToMessageId;
@ -41,7 +45,7 @@ TelegramSendMsg::TelegramSendMsg(const QVariant &chatId,
}
for (auto it = extraObjects.begin(); it != extraObjects.end(); it = std::next(it)) {
args[it.key()] = QJsonDocument(it.value()).toJson(QJsonDocument::Compact);
args[it.key()] = QJsonDocument(*it.value()).toJson(QJsonDocument::Compact);
}
setArgs(args);

@ -14,12 +14,12 @@ namespace qTbot {
/**
* @brief The TelegramSendMsg class This method send a message to the server.
*/
class QTBOT_EXPORT TelegramSendMsg: public TelegramSingleRquest
class TelegramSendMsg: public TelegramSingleRquest
{
public:
TelegramSendMsg(const QVariant& chatId,
const QString& text,
const QMap<QString, QJsonObject>& extraObjects = {},
const QMap<QString, QSharedPointer<QJsonObject>>& extraObjects = {},
unsigned long long replyToMessageId = 0,
bool markdown = true,
const QString& callBackQueryId = "",

@ -54,8 +54,20 @@ const QMap<QString, QVariant>& TelegramSingleRquest::args() const {
return _args;
}
void TelegramSingleRquest::addArg(const QString &key, const QVariant &val) {
_args[key] = val;
}
void TelegramSingleRquest::setArgs(const QMap<QString, QVariant> &newArgs) {
_args = newArgs;
}
const QString& TelegramSingleRquest::request() const {
return _request;
}
void TelegramSingleRquest::setRequest(const QString &newRequest) {
_request = newRequest;
}
}

@ -33,7 +33,7 @@ namespace qTbot {
* @endcode
*/
class QTBOT_EXPORT TelegramSingleRquest: public iRequest
class TelegramSingleRquest: public iRequest
{
public:
/**
@ -55,12 +55,31 @@ public:
protected:
/**
* @brief request return current requests commnad.
* @return current requests commnad.
*/
const QString& request() const;
/**
* @brief setRequest sets custom requests commnad
* @param newRequest new custom commnad of the request.
*/
void setRequest(const QString &newRequest);
/**
* @brief args This method returns a current list of arguments
* @return current list of arguments
*/
const QMap<QString, QVariant> &args() const;
/**
* @brief addArg This method push new arg, to arguments list
* @param key This is new argument key
* @param val this is new argument value.
*/
void addArg(const QString& key, const QVariant& val);
/**
* @brief setArgs For the some requests list of arguments posible to build only after constructor.
* @param newArgs This is new list of arguments.

@ -50,8 +50,15 @@ QSharedPointer<QNetworkReply> IBot::sendRequest(const QSharedPointer<iRequest> &
doRemoveFinishedRequests();
auto && url = makeUrl(rquest);
#ifdef QTBOT_PRINT_RQUESTS
qDebug() << url;
#endif
auto&& networkReplay = QSharedPointer<QNetworkReply>(
_manager->get(QNetworkRequest(makeUrl(rquest))));
_manager->get(QNetworkRequest(url)));
size_t address = reinterpret_cast<size_t>(networkReplay.get());
_replayStorage[address] = networkReplay;

@ -54,6 +54,13 @@ public:
*/
virtual bool sendMessage(const QVariant& chatId, const QString& text) = 0;
/**
* @brief deleteMessage This is main method to delete messages.
* @param chatId This is cahat id wher will be removed message.
* @param messageId This is removed message id.
* @return true if request was be prepared successful
*/
virtual bool deleteMessage(const QVariant& chatId, const QVariant& messageId) = 0;
/**
* @brief Get a file by its ID.

@ -16,6 +16,8 @@
#include <requests/telegramgetfile.h>
#include <requests/telegramgetme.h>
#include <requests/telegramsendmsg.h>
#include <requests/telegramdeletemessage.h>
#include <requests/telegrameditmessage.h>
#include <QNetworkReply>
#include <QSharedPointer>
@ -28,6 +30,7 @@
#include <qTbot/messages/telegramfile.h>
#include <qTbot/messages/telegramupdate.h>
namespace qTbot {
ITelegramBot::ITelegramBot() {
@ -64,7 +67,7 @@ bool ITelegramBot::sendMessage(const QVariant &chatId, const QString &text) {
bool ITelegramBot::sendSpecificMessage(const QVariant & chatId,
const QString &text,
const QMap<QString, QJsonObject> &extraObjects,
const QMap<QString, QSharedPointer<QJsonObject>> &extraObjects,
const QString &callBackQueryId,
unsigned long long replyToMessageId,
bool markdown,
@ -89,14 +92,14 @@ bool ITelegramBot::sendSpecificMessage(const QVariant & chatId,
}
bool ITelegramBot::sendSpecificMessageWithKeyboard(const QVariant &chatId,
const QString &text,
const QList<QString> &keyboard,
const QString &callBackQueryId,
bool onTimeKeyboard,
bool autoResizeKeyboard,
unsigned long long replyToMessageId,
bool markdown,
bool disableWebPagePreview) {
const QString &text,
const QList<QList<QString>> &keyboard,
const QString &callBackQueryId,
bool onTimeKeyboard,
bool autoResizeKeyboard,
unsigned long long replyToMessageId,
bool markdown,
bool disableWebPagePreview) {
if (!chatId.isValid() || chatId.isNull())
return false;
@ -105,23 +108,9 @@ bool ITelegramBot::sendSpecificMessageWithKeyboard(const QVariant &chatId,
return false;
}
QMap<QString, QJsonObject> extraObjects;
QJsonObject keyboardJson;
QJsonArray keyboardArray;
for (auto it = keyboard.begin(); it != keyboard.end(); it = std::next(it)) {
keyboardArray.push_back(QJsonObject{ {"text", *it} });
}
keyboardJson["keyboard"] = keyboardArray;
keyboardJson["resize_keyboard"] = autoResizeKeyboard;
keyboardJson["one_time_keyboard"] = onTimeKeyboard;
extraObjects["reply_markup"] = keyboardJson;
auto msg = QSharedPointer<TelegramSendMsg>::create(chatId,
text,
extraObjects,
prepareKeyboard(autoResizeKeyboard, onTimeKeyboard, keyboard),
replyToMessageId,
markdown,
callBackQueryId,
@ -130,25 +119,53 @@ bool ITelegramBot::sendSpecificMessageWithKeyboard(const QVariant &chatId,
return bool(sendRequest(msg));
}
bool ITelegramBot::sendSpecificMessageWithKeyboard(const QVariant &chatId,
const QString &text,
const QList<QMap<QString, std::function<void(const QString&)> >> &keyboard,
const QString &callBackQueryId,
bool onTimeKeyboard,
bool autoResizeKeyboard,
unsigned long long replyToMessageId,
bool markdown,
bool disableWebPagePreview) {
bool ITelegramBot::deleteMessage(const QVariant &chatId, const QVariant &messageId) {
if (!chatId.isValid() || chatId.isNull())
return false;
if (!messageId.isValid() || messageId.isNull())
return false;
auto msg = QSharedPointer<TelegramDeleteMessage>::create(chatId,
messageId);
return bool(sendRequest(msg));
}
bool ITelegramBot::editSpecificMessageWithKeyboard(const QVariant & messageId,
const QVariant &chatId,
const QString &newText,
bool markdown,
bool disableWebPagePreview,
const QList<QList<QString>> &keyboard,
const QString &callBackQueryId,
bool onTimeKeyboard,
bool autoResizeKeyboard) {
if (!chatId.isValid() || chatId.isNull())
return false;
if (text.isEmpty()) {
if (!messageId.isValid() || messageId.isNull())
return false;
}
QMap<QString, QJsonObject> extraObjects;
QJsonObject keyboardJson;
auto msg = QSharedPointer<TelegramEditMessage>::create(messageId,
chatId,
newText,
markdown,
disableWebPagePreview,
callBackQueryId,
prepareKeyboard(autoResizeKeyboard,
onTimeKeyboard,
keyboard));
return bool(sendRequest(msg));
}
QMap<QString, QSharedPointer<QJsonObject>>
qTbot::ITelegramBot::prepareInlineKeyBoard(const QList<QMap<QString, std::function<void (const QString &, const QVariant &)> > > &keyboard)
{
QMap<QString, QSharedPointer<QJsonObject>> extraObjects;
auto&& keyboardJson = QSharedPointer<QJsonObject>::create();
QJsonArray keyboardArray;
for (const auto& map : keyboard) {
@ -156,22 +173,119 @@ bool ITelegramBot::sendSpecificMessageWithKeyboard(const QVariant &chatId,
for (auto it = map.begin(); it != map.end(); it = std::next(it)) {
auto&& callBackKey = QString("callback_data_%0").arg(rand());
keyboardLineArray.push_back(QJsonObject{ {"text", it.key()}, {"callback_data", callBackKey } });
_handleButtons[callBackKey] = {it.value(), onTimeKeyboard};
_handleButtons[callBackKey] = {it.value()};
}
keyboardArray.push_back(keyboardLineArray);
}
keyboardJson["inline_keyboard"] = keyboardArray;
keyboardJson["resize_keyboard"] = autoResizeKeyboard;
keyboardJson["one_time_keyboard"] = onTimeKeyboard;
(*keyboardJson)["inline_keyboard"] = keyboardArray;
extraObjects["reply_markup"] = keyboardJson;
return extraObjects;
}
QMap<QString, QSharedPointer<QJsonObject>>
qTbot::ITelegramBot::prepareKeyboard(bool autoResizeKeyboard,
bool onTimeKeyboard,
const QList<QList<QString>> &keyboard) {
QMap<QString, QSharedPointer<QJsonObject>> extraObjects;
auto&& keyboardJson = QSharedPointer<QJsonObject>::create();
QJsonArray keyboardArray;
for (const auto &row :keyboard) {
QJsonArray keyboardLineArray;
for (auto it = row.begin(); it != row.end(); it = std::next(it)) {
keyboardLineArray.push_back(QJsonObject{ {"text", *it} });
}
keyboardArray.push_back(keyboardLineArray);
}
(*keyboardJson)["keyboard"] = keyboardArray;
(*keyboardJson)["resize_keyboard"] = autoResizeKeyboard;
(*keyboardJson)["one_time_keyboard"] = onTimeKeyboard;
extraObjects["reply_markup"] = keyboardJson;
return extraObjects;
}
bool ITelegramBot::editSpecificMessageWithKeyboard(const QVariant &messageId,
const QVariant &chatId,
const QString &text,
bool markdown,
bool disableWebPagePreview,
const QList<QMap<QString, std::function<void (const QString &, const QVariant&)> > > &keyboard,
const QString &callBackQueryId) {
if (!chatId.isValid() || chatId.isNull())
return false;
if (!messageId.isValid() || messageId.isNull())
return false;
auto msg = QSharedPointer<TelegramEditMessage>::create(messageId,
chatId,
text,
markdown,
disableWebPagePreview,
callBackQueryId,
prepareInlineKeyBoard(keyboard));
return bool(sendRequest(msg));
}
bool ITelegramBot::editSpecificMessage(const QVariant &messageId,
const QVariant &chatId,
const QString& newText,
const QString &callBackQueryId,
bool markdown,
bool disableWebPagePreview) {
if (!chatId.isValid() || chatId.isNull())
return false;
if (!messageId.isValid() || messageId.isNull())
return false;
if (newText.isEmpty())
return false;
auto msg = QSharedPointer<TelegramEditMessage>::create(messageId,
chatId,
newText,
markdown,
disableWebPagePreview,
callBackQueryId
);
return bool(sendRequest(msg));
}
bool ITelegramBot::sendSpecificMessageWithKeyboard(const QVariant &chatId,
const QString &text,
const QList<QMap<QString, std::function<void(const QString&, const QVariant& msgID)> >> &keyboard,
const QString &callBackQueryId,
unsigned long long replyToMessageId,
bool markdown,
bool disableWebPagePreview) {
if (!chatId.isValid() || chatId.isNull())
return false;
if (text.isEmpty()) {
return false;
}
auto msg = QSharedPointer<TelegramSendMsg>::create(chatId,
text,
extraObjects,
prepareInlineKeyBoard(keyboard),
replyToMessageId,
markdown,
callBackQueryId,
@ -289,13 +403,8 @@ void ITelegramBot::handleIncomeNewUpdate(const QSharedPointer<iUpdate> & update)
if (auto&& queryUpd = tupdate->callbackQueryUpdate()) {
auto &&handleButtonKey = queryUpd->callBackData();
auto [cb, isOneTimeKeyboard] = _handleButtons.value(handleButtonKey);
if (cb) {
cb(handleButtonKey);
if (isOneTimeKeyboard) {
_handleButtons.remove(handleButtonKey);
}
if (auto&& cb = _handleButtons.value(handleButtonKey)) {
cb(handleButtonKey, queryUpd->messageId());
}
}
}

@ -37,19 +37,20 @@ public:
bool sendMessage(const QVariant &chatId, const QString& text) override;
/**
* @brief Sends a message to a chat or channel with optional additional objects.
* @brief Send a message to a chat or channel with optional additional objects.
*
* The `sendSpecificMessage` method is used to send a message to a chat. This method allows
* The `sendSpecificMessage` method is used to send a message to a chat or channel. This method allows
* sending text messages to chats and channels. You can also specify additional
* parameters such as text formatting, a reply to a specific message, disabling
* parameters such as text formatting, replying to a specific message, disabling
* web page preview, and including extra JSON objects in the message.
*
* @param chatId The identifier of the chat or channel to which the message will be sent.
* It can be a string, number, or another valid data type containing the chat identifier.
* @param text The text of the message to be sent.
* @param extraObjects A map containing additional JSON objects to include in the message.
* @param callBackQueryId A unique identifier for this message (can be used to identify responses).
* @param replyToMessageId The identifier of the message to which you want to reply. If you want to send
* a regular message without a reply, leave this field as 0.
* a regular message without replying to another message, leave this field as 0.
* @param markdown A flag indicating whether the message text should be formatted using Markdown.
* If `true`, the text will be formatted using Markdown syntax. If `false`, the text will be
* sent as plain text.
@ -68,24 +69,24 @@ public:
* {"key1", {"value1"}},
* {"key2", {"value2"}}
* };
* bool result = sendSpecificMessage(chatId, "This is a reply with additional objects.", additionalObjects, messageId);
* bool result = sendSpecificMessage(chatId, "This is a reply with additional objects.", additionalObjects, "123456789", messageId);
*
* // Send a message with disabled web page preview
* bool result = sendSpecificMessage(chatId, "Check)", {}, 0, true, true);
* bool result = sendSpecificMessage(chatId, "Check)", {}, "987654321", 0, false, true);
* @endcode
*/
bool sendSpecificMessage(const QVariant &chatId,
const QString& text,
const QMap<QString, QJsonObject> &extraObjects = {},
const QMap<QString, QSharedPointer<QJsonObject> > &extraObjects = {},
const QString &callBackQueryId = {},
unsigned long long replyToMessageId = 0,
bool markdown = true,
bool disableWebPagePreview = false);
/**
* @brief Sends a message to a chat or channel with an optional custom **inline keyboard**.
* @brief Send a message to a chat or channel with an optional custom **inline keyboard**.
*
* The `sendSpecificMessage` method is used to send a message to a chat or channel. This method allows
* The `sendSpecificMessageWithKeyboard` method is used to send a message to a chat or channel. This method allows
* sending text messages with optional interactive **inline keyboards** to chats and channels. You can also specify
* additional parameters such as text formatting, replying to a specific message, disabling web page preview,
* and providing a custom inline keyboard for user interaction.
@ -95,10 +96,7 @@ public:
* @param text The text of the message to be sent.
* @param keyboard A list of maps, each containing buttons and corresponding callback functions for a custom inline keyboard.
* Each map should have button text as the key and a callback function as the value.
* @param onTimeKeyboard A flag indicating whether the inline keyboard should be hidden after the user interacts with it
* (one-time keyboard). Default is `false`.
* @param autoResizeKeyboard A flag indicating whether the inline keyboard should automatically adjust its size based on
* the number of buttons. Default is `false`.
* @param callBackQueryId A unique identifier for this message (can be used to identify responses).
* @param replyToMessageId The identifier of the message to which you want to reply. If you want to send a regular message
* without replying to another message, leave this field as 0. Default is 0.
* @param markdown A flag indicating whether the message text should be formatted using Markdown syntax. If `true`, the text
@ -111,41 +109,40 @@ public:
* Usage examples:
* @code
* // Send a plain text message
* bool result = sendSpecificMessage(chatId, "Hello, world!");
* bool result = sendSpecificMessageWithKeyboard(chatId, "Hello, world!", {}, "123456789");
*
* // Send a formatted text message as a reply to another message with a custom inline keyboard
* QList<QMap<QString, std::function<void()>>> customKeyboard = {
* QList<QMap<QString, std::function<void(const QString &queryID)>>> customKeyboard = {
* {
* {"Button 1", []() { Callback function for Button 1 }},
* {"Button 2", []() { Callback function for Button 2 }}
* {"Button 1", [](const QString &queryID) { "Callback function for Button 1" }},
* {"Button 2", [](const QString &queryID) { "Callback function for Button 2" }}
* }
* };
* bool result = sendSpecificMessage(chatId, "This is a reply with a custom inline keyboard.", customKeyboard, true, false, messageId);
* bool result = sendSpecificMessageWithKeyboard(chatId, "This is a reply with a custom inline keyboard.", customKeyboard, "987654321", messageId);
*
* // Send a message with disabled web page preview
* bool result = sendSpecificMessage(chatId, "Check)", {}, false, true, 0, false, true);
* bool result = sendSpecificMessageWithKeyboard(chatId, "Check)", {}, "555555555", 0, false, true);
* @endcode
*/
bool sendSpecificMessageWithKeyboard(const QVariant &chatId,
const QString& text,
const QList<QMap<QString, std::function<void (const QString &queryID)> > > &keyboard,
const QString &callBackQueryId,
bool onTimeKeyboard = false,
bool autoResizeKeyboard = false,
const QList<QMap<QString, std::function<void (const QString &, const QVariant &)> > > &keyboard,
const QString &callBackQueryId = "",
unsigned long long replyToMessageId = 0,
bool markdown = true,
bool disableWebPagePreview = false);
/**
* @brief sendSpecificMessageWithKeyBoard sends a specific message with a keyboard to a chat.
* @brief Send a specific message with a custom keyboard to a chat.
*
* This method sends a specific message with a keyboard to a chat. It allows
* you to customize various aspects of the message, such as the text, keyboard,
* This method sends a specific message with a custom keyboard to a chat, allowing
* you to customize various aspects of the message, such as the text, keyboard layout,
* and other options.
*
* @param chatId The identifier of the chat to which the message will be sent.
* @param text The text of the message to be sent.
* @param keyboard A list of text buttons for the keyboard placed below the message.
* @param keyboard A list of text buttons to display in the custom keyboard.
* @param callBackQueryId A unique identifier for this message (can be used to identify responses).
* @param onTimeKeyboard A flag indicating whether the keyboard should be hidden after
* selecting one of the buttons (one-time keyboard).
* @param autoResizeKeyboard A flag indicating whether the keyboard should automatically
@ -159,7 +156,7 @@ public:
*
* @example
* @code
* bool result = sendSpecificMessage(chatId, "Hello, how are you?", {"Button 1", "Button 2"}, true, true, 0, true, false);
* bool result = sendSpecificMessageWithKeyboard(chatId, "Hello, how are you?", {"Button 1", "Button 2"}, "123456789", true, true, 0, true, false);
* if (result) {
* qDebug() << "Message sent successfully.";
* } else {
@ -175,12 +172,129 @@ public:
* and web page preview options in the message.
*/
bool sendSpecificMessageWithKeyboard(const QVariant &chatId,
const QString& text,
const QList<QString> &keyboard,
const QString &callBackQueryId,
bool onTimeKeyboard = false,
bool autoResizeKeyboard = false,
unsigned long long replyToMessageId = 0,
const QString& text,
const QList<QList<QString> > &keyboard,
const QString &callBackQueryId,
bool onTimeKeyboard = false,
bool autoResizeKeyboard = false,
unsigned long long replyToMessageId = 0,
bool markdown = true,
bool disableWebPagePreview = false);
bool deleteMessage(const QVariant& chatId, const QVariant& messageId) override;
/**
* @brief Edit a specific message's keyboard in a chat or channel.
*
* The `editSpecificMessageWithKeyboard` method is used to edit the keyboard of a specific message in a chat or channel.
* This method allows you to modify the keyboard layout and options of an existing message, including adding or changing buttons.
*
* @param chatId The identifier of the chat or channel where the message to be edited is located.
* It can be a string, number, or another valid data type containing the chat identifier.
* @param keyboard A list of text buttons for the new keyboard layout.
* @param callBackQueryId A unique identifier for this message (can be used to identify responses).
* @param onTimeKeyboard A flag indicating whether the new inline keyboard should be hidden after the user interacts with it
* (one-time keyboard). Default is `false`.
* @param autoResizeKeyboard A flag indicating whether the new inline keyboard should automatically adjust its size based on
* the number of buttons. Default is `false`.
*
* @return `true` if the message keyboard was successfully edited, and `false` otherwise.
*
* Usage example:
* @code
* // Edit the keyboard of a specific message
* bool result = editSpecificMessageWithKeyboard(chatId, {"New Button 1", "New Button 2"}, "123456789", true, true);
* if (result) {
* qDebug() << "Message keyboard edited successfully.";
* } else {
* qDebug() << "Error editing the message keyboard.";
* }
* @endcode
*/
bool editSpecificMessageWithKeyboard(const QVariant &messageId,
const QVariant &chatId,
const QString &newText,
bool markdown = true,
bool disableWebPagePreview = false,
const QList<QList<QString> > &keyboard = {},
const QString &callBackQueryId = "",
bool onTimeKeyboard = false,
bool autoResizeKeyboard = false);
/**
* @brief Edit the keyboard of a specific message in a chat or channel.
*
* The `editSpecificMessageWithKeyboard` method is used to modify the keyboard of a specific message in a chat or channel.
* This method allows you to customize the layout and behavior of the keyboard by providing a list of text buttons
* along with their corresponding callback functions.
*
* @param chatId The identifier of the chat or channel where the message to be edited is located.
* It can be a string, number, or another valid data type containing the chat identifier.
* @param keyboard A list of maps, each containing buttons and their corresponding callback functions for the new keyboard layout.
* Each map should have button text as the key and a callback function as the value.
* @param callBackQueryId A unique identifier for this message (can be used to identify responses).
*
* @return `true` if the message keyboard was successfully edited, and `false` otherwise.
*
* Usage example:
* @code
* // Define a custom keyboard layout with callback functions
* QList<QMap<QString, std::function<void(const QString &queryID)>> customKeyboard = {
* {
* {"Button 1", [](const QString &queryID) { Callback function for Button 1 }},
* {"Button 2", [](const QString &queryID) { Callback function for Button 2 }}
* }
* };
*
* // Edit the keyboard of a specific message with the custom keyboard layout
* bool result = editSpecificMessageWithKeyboard(chatId, customKeyboard, "123456789");
* if (result) {
* qDebug() << "Message keyboard edited successfully.";
* } else {
* qDebug() << "Error editing the message keyboard.";
* }
* @endcode
*/
bool editSpecificMessageWithKeyboard(const QVariant& messageId,
const QVariant &chatId,
const QString &text,
bool markdown = true,
bool disableWebPagePreview = false,
const QList<QMap<QString, std::function<void (const QString &, const QVariant &)> > > &keyboard = {},
const QString &callBackQueryId = "");
/**
* @brief Edit a specific message in a chat or channel with optional additional objects.
*
* The `editSpecificMessage` method is used to edit the content of a specific message in a chat or channel. This method allows
* you to modify the message text, various message options, and additional parameters.
*
* @param messageId The identifier of the message to be edited.
* @param chatId The identifier of the chat or channel where the message to be edited is located.
* It can be a string, number, or another valid data type containing the chat identifier.
* @param newText The updated text for the message.
* @param callBackQueryId A unique identifier for this message (can be used to identify responses).
* @param markdown A flag indicating whether the edited message text should be formatted using Markdown syntax.
* If `true`, the text will be formatted using Markdown. If `false`, the text will be sent as plain text.
* Default is `true`.
* @param disableWebPagePreview A flag indicating whether to disable web page preview in the edited message.
* If `true`, web page preview will be disabled. Default is `false`.
*
* @return `true` if the message was successfully edited, and `false` otherwise.
*
* Usage examples:
* @code
* // Edit the text of a specific message
* bool result = editSpecificMessage(123456789, chatId, "Updated message text");
*
* // Edit the message with a callback query identifier and disable web page preview
* bool result = editSpecificMessage(987654321, chatId, "Updated message text with callback query ID", "555555555", false, true);
* @endcode
*/
bool editSpecificMessage(const QVariant &messageId,
const QVariant &chatId,
const QString &newText,
const QString &callBackQueryId = {},
bool markdown = true,
bool disableWebPagePreview = false);
@ -297,11 +411,15 @@ private slots:
private:
QString findFileInlocatStorage(const QString& fileId) const;
QMap<QString, QSharedPointer<QJsonObject>>
prepareInlineKeyBoard(const QList<QMap<QString, std::function<void (const QString &, const QVariant &)> > > &keyboard);
QMap<QString, QSharedPointer<QJsonObject>>
prepareKeyboard(bool autoResizeKeyboard, bool onTimeKeyboard, const QList<QList<QString> > &keyboard);
unsigned long long _id = 0;
QString _username;
QSharedPointer<QNetworkReply> _loginReplay;
QMap<QString, QPair<std::function<void(const QString&)>, bool>> _handleButtons;
QMap<QString, std::function<void(const QString&, const QVariant&)>> _handleButtons;
QHash<QString, QSharedPointer<TelegramFile>> _filesMetaInfo;

@ -39,4 +39,8 @@ QString TelegramInlineKeyBoardCallBack::languageCode() const {
QString TelegramInlineKeyBoardCallBack::callBackData() const {
return rawJson()["data"].toString();
}
QVariant TelegramInlineKeyBoardCallBack::messageId() const {
return rawJson()["message"]["message_id"].toVariant();
}
}

@ -64,6 +64,12 @@ public:
*/
QString callBackData() const;
/**
* @brief messageId id of the message when was be presed button.
* @return id of the message when was be presed button.
*/
QVariant messageId() const;
};
}
#endif // TELEGRAMINLINEKEYBOARDCALLBACK_H