From ada22dd3940272368803d0ed473af4638380e450 Mon Sep 17 00:00:00 2001 From: EndrII Date: Sat, 18 Nov 2023 00:04:48 +0100 Subject: [PATCH] added structure for general arguments of send messages --- src/example/main.cpp | 5 +- .../src/private/requests/telegramsendmsg.cpp | 31 +--- .../src/private/requests/telegramsendmsg.h | 19 +-- src/qTbot/src/public/qTbot/itelegrambot.cpp | 23 +-- src/qTbot/src/public/qTbot/itelegrambot.h | 137 ++++++------------ src/qTbot/src/public/qTbot/telegrammargs.cpp | 53 +++++++ src/qTbot/src/public/qTbot/telegrammargs.h | 72 +++++++++ 7 files changed, 191 insertions(+), 149 deletions(-) create mode 100644 src/qTbot/src/public/qTbot/telegrammargs.cpp create mode 100644 src/qTbot/src/public/qTbot/telegrammargs.h diff --git a/src/example/main.cpp b/src/example/main.cpp index 590466a..f7fa0c0 100644 --- a/src/example/main.cpp +++ b/src/example/main.cpp @@ -44,16 +44,17 @@ 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, const QVariant& msgId){ static int index = 0; bot.editSpecificMessageWithKeyboard(msgId, tmsg->chatId(), - "I see it - я вижу это. Presedd count: " + QString::number(index++), true, false, + "I see it - я вижу это. Presedd count: " + QString::number(index++),"", false, {{{"test_button", [](auto , auto ){}}, {"test_button 2", [](auto , auto ){}}}}, queryId); - }}}}, {}, false, false, tmsg->messageId()); + }}}}, "", tmsg->messageId(), "", false); bot.sendSpecificMessageWithKeyboard(tmsg->chatId(), "I see it - я вижу это (интерактивная клавиатура)", diff --git a/src/qTbot/src/private/requests/telegramsendmsg.cpp b/src/qTbot/src/private/requests/telegramsendmsg.cpp index 7e864ae..d777da7 100644 --- a/src/qTbot/src/private/requests/telegramsendmsg.cpp +++ b/src/qTbot/src/private/requests/telegramsendmsg.cpp @@ -11,38 +11,13 @@ #include namespace qTbot { -TelegramSendMsg::TelegramSendMsg(const QVariant &chatId, - const QString &text, - const QHash > &extraObjects, - unsigned long long replyToMessageId, - const QString& parseMode, - const QString &callBackQueryId, - bool disableWebPagePreview) +TelegramSendMsg::TelegramSendMsg(const TelegrammArgs& generalArgs, + const QHash > &extraObjects) : TelegramSingleRquest("sendMessage") { - QMap args {{"chat_id", chatId}}; - - if (text.size()) { - args["text"] = text; - } - - if (replyToMessageId) { - args["reply_to_message_id"] = replyToMessageId; - } - - if (parseMode.size()) { - args["parse_mode"] = parseMode; - } - - if (disableWebPagePreview) { - args["disable_web_page_preview"] = disableWebPagePreview; - } - - if (callBackQueryId.size()) { - args["callback_query_id"] = callBackQueryId; - } + QMap&& args = generalArgs.toMap(); for (auto it = extraObjects.begin(); it != extraObjects.end(); it = std::next(it)) { args[it.key()] = QJsonDocument(*it.value()).toJson(QJsonDocument::Compact); diff --git a/src/qTbot/src/private/requests/telegramsendmsg.h b/src/qTbot/src/private/requests/telegramsendmsg.h index 1a6778a..0e41081 100644 --- a/src/qTbot/src/private/requests/telegramsendmsg.h +++ b/src/qTbot/src/private/requests/telegramsendmsg.h @@ -8,6 +8,7 @@ #ifndef TELEGRAMSENDMSG_H #define TELEGRAMSENDMSG_H +#include "qTbot/telegrammargs.h" #include "telegramsinglerquest.h" namespace qTbot { @@ -17,24 +18,14 @@ namespace qTbot { class TelegramSendMsg: public TelegramSingleRquest { public: + /** * @brief TelegramSendMsg - * @param chatId - * @param text + * @param args * @param extraObjects - * @param replyToMessageId - * @param parseMode This is mode of parsing. supported modes is markdown and html - * @param callBackQueryId - * @param disableWebPagePreview */ - TelegramSendMsg(const QVariant& chatId, - const QString& text, - const ExtraJsonObjects& extraObjects = {}, - unsigned long long replyToMessageId = 0, - const QString& parseMode = "", - const QString& callBackQueryId = "", - bool disableWebPagePreview = false - ); + TelegramSendMsg(const TelegrammArgs& args, + const ExtraJsonObjects& extraObjects = {}); }; } #endif // TELEGRAMSENDMSG_H diff --git a/src/qTbot/src/public/qTbot/itelegrambot.cpp b/src/qTbot/src/public/qTbot/itelegrambot.cpp index 4660848..6dcc1b1 100644 --- a/src/qTbot/src/public/qTbot/itelegrambot.cpp +++ b/src/qTbot/src/public/qTbot/itelegrambot.cpp @@ -66,21 +66,16 @@ bool ITelegramBot::login(const QByteArray &token) { } bool ITelegramBot::sendMessage(const QVariant &chatId, const QString &text) { - return sendSpecificMessage(chatId.toLongLong(), text); + return sendSpecificMessage(TelegrammArgs{chatId, text}); } -bool ITelegramBot::sendSpecificMessage(const QVariant & chatId, - const QString &text, - const ExtraJsonObjects &extraObjects, - const QString &callBackQueryId, - unsigned long long replyToMessageId, - const QString& parseMode, - bool disableWebPagePreview) { +bool ITelegramBot::sendSpecificMessage(const TelegrammArgs& args, + const ExtraJsonObjects &extraObjects) { - if (!chatId.isValid() || chatId.isNull()) + if (!args.chatId.isValid() || args.chatId.isNull()) return false; - if (text.isEmpty()) { + if (args.text.isEmpty()) { return false; } @@ -403,6 +398,14 @@ bool ITelegramBot::sendFile(const QByteArray &file, const QString &fileName, con return sendFileWithDescription(file, fileName, chatId, ""); } +bool ITelegramBot::sendFileMessage(const QFileInfo &file, const TelegrammArgs &args) { + +} + +bool ITelegramBot::sendFileMessage(const QByteArray &file, const QString &fileName, const TelegrammArgs &args) { + +} + bool ITelegramBot::sendPhoto(const QFileInfo &photo, const QVariant &chatId, const QString &description, diff --git a/src/qTbot/src/public/qTbot/itelegrambot.h b/src/qTbot/src/public/qTbot/itelegrambot.h index de24453..6d0ef46 100644 --- a/src/qTbot/src/public/qTbot/itelegrambot.h +++ b/src/qTbot/src/public/qTbot/itelegrambot.h @@ -11,6 +11,7 @@ #define ITELEGRAMBOT_H #include "ibot.h" +#include "qTbot/telegrammargs.h" #include class QNetworkAccessManager; @@ -45,90 +46,50 @@ public: * @brief Sends a specific message to a chat. * * This function sends a specific message to a chat. You can include text and additional objects as needed to customize the message's content and behavior. - * - * @param chatId The unique identifier of the chat to send the message to. - * @param text The text content of the message to be sent. + * @param args - This is structure with general arguments of the tellegram message. + * Use This structure for sets target and another arguments. * @param extraObjects A map containing additional objects associated with the message (optional). - * @param callBackQueryId The unique identifier for callback queries triggered by the message (optional). - * @param replyToMessageId The unique identifier of the message to reply to, if any (optional). - * @param parseMode Sets a text parsing mode. supports markdown and html, by default will be used html mode. If you want to disable parsing mode sets this option to empty string. - * @param disableWebPagePreview Set to true to disable web page previews for links in the message (optional). * * @return Returns true if the message was sent successfully, false otherwise. * * @note The extraObjects parameter is a map where each key represents the object's name, and the associated value is a shared pointer to a JSON object (optional). - * @note The callBackQueryId parameter is used to handle callback queries when applicable (optional). - * @note By default, web page previews for links in the message are not disabled (optional). */ - bool sendSpecificMessage(const QVariant &chatId, - const QString& text, - const qTbot::ExtraJsonObjects &extraObjects = {}, - const QString &callBackQueryId = {}, - unsigned long long replyToMessageId = 0, - const QString &parseMode = "html", - bool disableWebPagePreview = false); + bool sendSpecificMessage(const TelegrammArgs& args, + const qTbot::ExtraJsonObjects &extraObjects = {}); /** * @brief Sends a specific message with a custom keyboard to a chat. - * * This function sends a specific message to a chat with a custom keyboard. The message can contain text and additional settings to customize its behavior. - * - * @param chatId The unique identifier of the chat to send the message to. - * @param text The text content of the message to be sent. + * @param args - This is structure with general arguments of the tellegram message. + * Use This structure for sets target and another arguments. * @param keyboard A list of maps where each map represents a button with a callback function. - * @param callBackQueryId The unique identifier for callback queries triggered by the message (optional). - * @param replyToMessageId The unique identifier of the message to reply to, if any (optional). - * @param parseMode Sets a text parsing mode. supports markdown and html, - * by default will be used html mode. If you want to disable parsing mode sets this option to empty string. - * @param disableWebPagePreview Set to true to disable web page previews for links in the message (optional). - * * @return Returns true if the message was sent successfully, false otherwise. * * @note The keyboard parameter should be a list of maps where each map represents a button. * The button's label is the map key, and the associated callback function is the map value. - * @note The callBackQueryId parameter is used to handle callback queries when buttons are pressed (optional). - * @note By default, web page previews for links in the message are not disabled (optional). */ - bool sendSpecificMessageWithKeyboard(const QVariant &chatId, - const QString& text, - const KeyboardOnMessage &keyboard, - const QString &callBackQueryId = "", - unsigned long long replyToMessageId = 0, - const QString &parseMode = "html", - bool disableWebPagePreview = false); + bool sendSpecificMessageWithKeyboard(const TelegrammArgs& args, + const KeyboardOnMessage &keyboard); /** * @brief Sends a specific message with a custom keyboard to a chat. * * This function sends a specific message to a chat with a custom keyboard. * The message can contain text and additional settings to customize its behavior. - * - * @param chatId The unique identifier of the chat to send the message to. - * @param text The text content of the message to be sent. + * @param args - This is structure with general arguments of the tellegram message. + * Use This structure for sets target and another arguments. * @param keyboard A list of lists containing the keyboard buttons to display. - * @param callBackQueryId The unique identifier for callback queries triggered by the message. * @param onTimeKeyboard Set to true to display the keyboard only once. * @param autoResizeKeyboard Set to true to automatically resize the keyboard. - * @param replyToMessageId The unique identifier of the message to reply to, if any. - * @param parseMode Sets a text parsing mode. supports markdown and html, - * by default will be used html mode. If you want to disable parsing mode sets this option to empty string. - * @param disableWebPagePreview Set to true to disable web page previews for links in the message. * * @return Returns true if the message was sent successfully, false otherwise. * - * @note The keyboard parameter should be a list of lists of strings representing keyboard buttons. - * @note The callBackQueryId parameter is used to handle callback queries when buttons are pressed. * @note By default, web page previews for links in the message are not disabled. */ - bool sendSpecificMessageWithKeyboard(const QVariant &chatId, - const QString& text, + bool sendSpecificMessageWithKeyboard(const TelegrammArgs& args, const QList > &keyboard, - const QString &callBackQueryId = "", bool onTimeKeyboard = false, - bool autoResizeKeyboard = true, - unsigned long long replyToMessageId = 0, - const QString &parseMode = "html", - bool disableWebPagePreview = false); + bool autoResizeKeyboard = true); bool deleteMessage(const QVariant& chatId, const QVariant& messageId) override; @@ -136,31 +97,20 @@ public: * @brief Edits a specific message with a custom keyboard in a chat. * * This function allows you to edit a specific message in a chat with a custom keyboard. You can update the message's text and customize its behavior as needed. - * + * @param args - This is structure with general arguments of the tellegram message. + * Use This structure for sets target and another arguments. * @param messageId The unique identifier of the message to edit. - * @param chatId The unique identifier of the chat containing the message. - * @param newText The new text content for the message. - * @param parseMode Sets a text parsing mode. supports markdown and html, - * by default will be used html mode. If you want to disable parsing mode sets this option to empty string. - * @param disableWebPagePreview Set to true to disable web page previews for links in the new text (optional). * @param keyboard A list of lists containing the new keyboard buttons to display (optional). - * @param callBackQueryId The unique identifier for callback queries triggered by the edited message (optional). * @param onTimeKeyboard Set to true to display the keyboard only once (optional). * @param autoResizeKeyboard Set to true to automatically resize the keyboard (optional). * * @return Returns true if the message was successfully edited, false otherwise. * - * @note The keyboard parameter should be a list of lists of strings representing keyboard buttons (optional). - * @note The callBackQueryId parameter is used to handle callback queries when buttons are pressed (optional). * @note By default, web page previews for links in the new text are not disabled (optional). */ bool editSpecificMessageWithKeyboard(const QVariant &messageId, - const QVariant &chatId, - const QString &newText, - const QString &parseMode = "html", - bool disableWebPagePreview = false, + const TelegrammArgs& args, const QList > &keyboard = {}, - const QString &callBackQueryId = "", bool onTimeKeyboard = false, bool autoResizeKeyboard = false); @@ -171,27 +121,17 @@ public: * This function allows you to edit a specific message in a chat with a custom keyboard. You can update the message's text and customize its behavior as needed. * * @param messageId The unique identifier of the message to edit. - * @param chatId The unique identifier of the chat containing the message. - * @param text The new text content for the message. - * @param parseMode Sets a text parsing mode. supports markdown and html, - * by default will be used html mode. If you want to disable parsing mode sets this option to empty string. - * @param disableWebPagePreview Set to true to disable web page previews for links in the new text (optional). + * @param args - This is structure with general arguments of the tellegram message. + * Use This structure for sets target and another arguments. * @param keyboard A list of maps where each map represents a button with a callback function (optional). - * @param callBackQueryId The unique identifier for callback queries triggered by the edited message (optional). * * @return Returns true if the message was successfully edited, false otherwise. * - * @note The keyboard parameter should be a list of maps where each map represents a button. The button's label is the map key, and the associated callback function is the map value (optional). - * @note The callBackQueryId parameter is used to handle callback queries when buttons are pressed (optional). * @note By default, web page previews for links in the new text are not disabled (optional). - */ - bool editSpecificMessageWithKeyboard(const QVariant& messageId, - const QVariant &chatId, - const QString &text, - const QString &parseMode = "html", - bool disableWebPagePreview = false, - const KeyboardOnMessage &keyboard = {}, - const QString &callBackQueryId = ""); + */ + bool editSpecificMessageWithKeyboard(const TelegrammArgs& args, + const QVariant& messageId, + const KeyboardOnMessage &keyboard = {}); /** * @brief Edits a keyboard of message in a chat. @@ -220,24 +160,14 @@ public: * This function allows you to edit a specific message in a chat. You can update the message's text and customize its behavior as needed. * * @param messageId The unique identifier of the message to edit. - * @param chatId The unique identifier of the chat containing the message. - * @param newText The new text content for the message. - * @param callBackQueryId The unique identifier for callback queries triggered by the edited message (optional). - * @param parseMode Sets a text parsing mode. supports markdown and html, - * by default will be used html mode. If you want to disable parsing mode sets this option to empty string. - * @param disableWebPagePreview Set to true to disable web page previews for links in the new text (optional). - * + * @param args - This is structure with general arguments of the tellegram message. + * Use This structure for sets target and another arguments. * @return Returns true if the message was successfully edited, false otherwise. * - * @note The callBackQueryId parameter is used to handle callback queries when applicable (optional). * @note By default, web page previews for links in the new text are not disabled (optional). */ bool editSpecificMessage(const QVariant &messageId, - const QVariant &chatId, - const QString &newText, - const QString &callBackQueryId = {}, - const QString &parseMode = "html", - bool disableWebPagePreview = false); + const TelegrammArgs& args); [[nodiscard("do not forget to save shared pointer of file handler, because it's will not save inner bot object.")]] QSharedPointer getFile(const QString& fileId, iFile::Type fileType = iFile::Type::Ram) override; @@ -255,6 +185,23 @@ public: bool sendFile( const QByteArray& file, const QString& fileName, const QVariant& chatId) override; + /** + * @brief sendFileMessage This method sents a message with file. + * @param file This is path to sents file. + * @param args This is general arguments of the message, include a chatID. + * @return true if the message sents successful else false + */ + bool sendFileMessage( const QFileInfo& file, const TelegrammArgs& args); + + /** + * @brief sendFileMessage This method sents a message with file. + * @param file This is data that will be sents as a file with name @a fileName. + * @param fileName This is name of the file. + * @param args This is general arguments of the message, include a chatID. + * @return true if the message sents successful else false + */ + bool sendFileMessage( const QByteArray& file, const QString& fileName, const TelegrammArgs& args); + /** * @brief sendPhoto This method will send image into chat with @a chatId * @param photo this is photo path. diff --git a/src/qTbot/src/public/qTbot/telegrammargs.cpp b/src/qTbot/src/public/qTbot/telegrammargs.cpp new file mode 100644 index 0000000..d848103 --- /dev/null +++ b/src/qTbot/src/public/qTbot/telegrammargs.cpp @@ -0,0 +1,53 @@ +//# +//# 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 "telegrammargs.h" +namespace qTbot { + +TelegrammArgs::TelegrammArgs(const QVariant &id, + const QString &text, + const QString &parseMode, + unsigned long long replyToMessageId, + bool disableWebPagePreview, + const QString &callBackQueryId) +{ + + this->chatId = id; + this->text = text; + this->callBackQueryId = callBackQueryId; + this->disableWebPagePreview = disableWebPagePreview; + this->replyToMessageId = replyToMessageId; + this->parseMode = parseMode; +} + +QMap TelegrammArgs::toMap() const { + QMap args {{"chat_id", chatId}}; + + if (text.size()) { + args["text"] = text; + } + + if (replyToMessageId) { + args["reply_to_message_id"] = replyToMessageId; + } + + if (parseMode.size()) { + args["parse_mode"] = parseMode; + } + + if (disableWebPagePreview) { + args["disable_web_page_preview"] = disableWebPagePreview; + } + + if (callBackQueryId.size()) { + args["callback_query_id"] = callBackQueryId; + } + + return args; +} + +} diff --git a/src/qTbot/src/public/qTbot/telegrammargs.h b/src/qTbot/src/public/qTbot/telegrammargs.h new file mode 100644 index 0000000..69deadc --- /dev/null +++ b/src/qTbot/src/public/qTbot/telegrammargs.h @@ -0,0 +1,72 @@ +//# +//# 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 TELEGRAMMARGS_H +#define TELEGRAMMARGS_H + +#include + +namespace qTbot { + +/** + * @brief The TelegrammArgs class is base structure for the all tellegram message arguments. + */ +struct TelegrammArgs +{ + TelegrammArgs(const QVariant& id, + const QString& text = "", + const QString& parseMode = "", + unsigned long long replyToMessageId = 0, + bool disableWebPagePreview = false, + const QString& callBackQueryId = "" + ); + + /** + * @brief Text of the message. + * Default: "". + */ + QString text = ""; + + /** + * @brief Chat ID where the message will be sent. + * Default: {}. + */ + QVariant chatId = {}; + + /** + * @brief Callback query ID. + * Default: "". + */ + QString callBackQueryId = {}; + + /** + * @brief ID of the message to which a reply will be given. + * Default: 0. + */ + unsigned long long replyToMessageId = 0; + + /** + * @brief Parse mode of the message. + * Default: "html". + */ + QString parseMode = "html"; + + /** + * @brief Disable web page preview when sending links. + * Default: false. + */ + bool disableWebPagePreview = false; + + /** + * @brief toMap convert all arguments to the map. + * @return + */ + QMap toMap() const; +}; + +} +#endif // TELEGRAMMARGS_H