From 66aac2a5d4a1b5705c8f73129e55539ed220ad7c Mon Sep 17 00:00:00 2001 From: EndrII Date: Tue, 14 Nov 2023 22:58:49 +0100 Subject: [PATCH 01/12] added method to edit keyboard --- .../private/requests/telegrameditmessage.cpp | 18 ++--- .../private/requests/telegrameditmessage.h | 12 +-- .../telegrameditmessagereplymarkup.cpp | 20 +++++ .../requests/telegrameditmessagereplymarkup.h | 27 +++++++ .../private/requests/telegramsenddocument.cpp | 14 +++- .../private/requests/telegramsenddocument.h | 10 ++- .../src/private/requests/telegramsendfile.cpp | 14 +++- .../src/private/requests/telegramsendfile.h | 7 +- .../src/private/requests/telegramsendmsg.cpp | 8 +- .../src/private/requests/telegramsendmsg.h | 14 +++- .../private/requests/telegramsendphoto.cpp | 10 ++- .../src/private/requests/telegramsendphoto.h | 6 +- src/qTbot/src/public/qTbot/irequest.h | 5 ++ src/qTbot/src/public/qTbot/itelegrambot.cpp | 77 ++++++++++++------ src/qTbot/src/public/qTbot/itelegrambot.h | 79 ++++++++++++------- 15 files changed, 235 insertions(+), 86 deletions(-) create mode 100644 src/qTbot/src/private/requests/telegrameditmessagereplymarkup.cpp create mode 100644 src/qTbot/src/private/requests/telegrameditmessagereplymarkup.h diff --git a/src/qTbot/src/private/requests/telegrameditmessage.cpp b/src/qTbot/src/private/requests/telegrameditmessage.cpp index 8440a4c..34d07bf 100644 --- a/src/qTbot/src/private/requests/telegrameditmessage.cpp +++ b/src/qTbot/src/private/requests/telegrameditmessage.cpp @@ -10,23 +10,23 @@ namespace qTbot { TelegramEditMessage::TelegramEditMessage(const QVariant &idEditedMessage, - const QVariant& chatId, - const QString& newText, - bool markdown, - bool disableWebPagePreview, - const QString& callBackQueryId, - const QMap>& extraObjects): + const QVariant& chatId, + const QString& newText, + const QString& parseMode, + bool disableWebPagePreview, + const QString& callBackQueryId, + const QHash > &extraObjects): TelegramSendMsg(chatId, newText, extraObjects, 0, - markdown, + parseMode, callBackQueryId, disableWebPagePreview) { - setRequest("editMessageText"); - addArg("message_id", idEditedMessage); + setRequest("editMessageText"); + addArg("message_id", idEditedMessage); } diff --git a/src/qTbot/src/private/requests/telegrameditmessage.h b/src/qTbot/src/private/requests/telegrameditmessage.h index 3b57149..b04787c 100644 --- a/src/qTbot/src/private/requests/telegrameditmessage.h +++ b/src/qTbot/src/private/requests/telegrameditmessage.h @@ -18,12 +18,12 @@ 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>& extraObjects = {}); + const QVariant& chatId, + const QString& newText, + const QString& parseMode = "", + bool disableWebPagePreview = false, + const QString& callBackQueryId = "", + const ExtraJsonObjects& extraObjects = {}); }; } #endif // TELEGRAMEDITMESSAGE_H diff --git a/src/qTbot/src/private/requests/telegrameditmessagereplymarkup.cpp b/src/qTbot/src/private/requests/telegrameditmessagereplymarkup.cpp new file mode 100644 index 0000000..8d659cc --- /dev/null +++ b/src/qTbot/src/private/requests/telegrameditmessagereplymarkup.cpp @@ -0,0 +1,20 @@ +//# +//# 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 "telegrameditmessagereplymarkup.h" + +namespace qTbot { + +TelegramEditMessageReplyMarkup::TelegramEditMessageReplyMarkup(const QVariant &idEditedMessage, + const QVariant &chatId, + const QString &callBackQueryId, + const ExtraJsonObjects &extraObjects): + TelegramEditMessage(idEditedMessage, chatId, "", "", false, callBackQueryId, extraObjects) +{ + setRequest("editMessageReplyMarkup"); +} +} diff --git a/src/qTbot/src/private/requests/telegrameditmessagereplymarkup.h b/src/qTbot/src/private/requests/telegrameditmessagereplymarkup.h new file mode 100644 index 0000000..776374f --- /dev/null +++ b/src/qTbot/src/private/requests/telegrameditmessagereplymarkup.h @@ -0,0 +1,27 @@ +//# +//# 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 TELEGRAMEDITMESSAGEREPLYMARKUP_H +#define TELEGRAMEDITMESSAGEREPLYMARKUP_H + +#include "telegrameditmessage.h" +namespace qTbot { + +/** + * @brief The TelegramEditMessageReplyMarkup class just edit alredy created Markups. + */ +class TelegramEditMessageReplyMarkup: public TelegramEditMessage +{ +public: + TelegramEditMessageReplyMarkup(const QVariant& idEditedMessage, + const QVariant& chatId, + const QString& callBackQueryId = "", + const ExtraJsonObjects& extraObjects = {}); +}; +} +#endif // TELEGRAMEDITMESSAGEREPLYMARKUP_H diff --git a/src/qTbot/src/private/requests/telegramsenddocument.cpp b/src/qTbot/src/private/requests/telegramsenddocument.cpp index 85e2135..6e8f459 100644 --- a/src/qTbot/src/private/requests/telegramsenddocument.cpp +++ b/src/qTbot/src/private/requests/telegramsenddocument.cpp @@ -11,12 +11,18 @@ namespace qTbot { TelegramSendDocument::TelegramSendDocument(const QVariant &chatId, const QString &text, const QString& fileName, - const QByteArray &data): - TelegramSendFile("sendDocument", chatId, text, fileName, TELEGRAM_DOCUMENT, data) {} + const QByteArray &data, + const QString &parseMode, + unsigned long long replyToMessageId, + const ExtraJsonObjects &extraObjects): + TelegramSendFile("sendDocument", chatId, text, fileName, TELEGRAM_DOCUMENT, data, parseMode, replyToMessageId, extraObjects) {} TelegramSendDocument::TelegramSendDocument(const QVariant &chatId, const QString &text, - const QFileInfo &file): - TelegramSendFile("sendDocument", chatId, text, file) {} + const QFileInfo &file, + const QString &parseMode, + unsigned long long replyToMessageId, + const ExtraJsonObjects &extraObjects): + TelegramSendFile("sendDocument", chatId, text, file, parseMode, replyToMessageId, extraObjects) {} } diff --git a/src/qTbot/src/private/requests/telegramsenddocument.h b/src/qTbot/src/private/requests/telegramsenddocument.h index 7bd389a..8972925 100644 --- a/src/qTbot/src/private/requests/telegramsenddocument.h +++ b/src/qTbot/src/private/requests/telegramsenddocument.h @@ -22,11 +22,17 @@ public: TelegramSendDocument(const QVariant &chatId, const QString &text, const QString &fileName, - const QByteArray& data); + const QByteArray& data, + const QString& parseMode = "", + unsigned long long replyToMessageId = 0, + const ExtraJsonObjects& extraObjects = {}); TelegramSendDocument(const QVariant &chatId, const QString &text, - const QFileInfo &file); + const QFileInfo &file, + const QString& parseMode = "", + unsigned long long replyToMessageId = 0, + const ExtraJsonObjects& extraObjects = {}); }; } diff --git a/src/qTbot/src/private/requests/telegramsendfile.cpp b/src/qTbot/src/private/requests/telegramsendfile.cpp index 3b6af0e..aa6ec5c 100644 --- a/src/qTbot/src/private/requests/telegramsendfile.cpp +++ b/src/qTbot/src/private/requests/telegramsendfile.cpp @@ -17,8 +17,9 @@ qTbot::TelegramSendFile::TelegramSendFile(const QString &request, const QString &fileName, const QString &fileType, const QByteArray &data, + const QString& parseMode, unsigned long long replyToMessageId, - const QMap>& extraObjects + const ExtraJsonObjects& extraObjects ): TelegramSingleRquest(request) { @@ -30,6 +31,10 @@ qTbot::TelegramSendFile::TelegramSendFile(const QString &request, addArg("reply_to_message_id", replyToMessageId); } + if (parseMode.size()) { + addArg("parse_mode", parseMode); + } + for (auto it = extraObjects.begin(); it != extraObjects.end(); it = std::next(it)) { addArg(it.key(), QJsonDocument(*it.value()).toJson(QJsonDocument::Compact)); } @@ -41,8 +46,9 @@ qTbot::TelegramSendFile::TelegramSendFile(const QString &request, const QVariant &chatId, const QString &text, const QFileInfo &file, + const QString &parseMode, unsigned long long replyToMessageId, - const QMap > &extraObjects): + const QHash > &extraObjects): TelegramSingleRquest(request) { addArg("chat_id", chatId); @@ -54,6 +60,10 @@ qTbot::TelegramSendFile::TelegramSendFile(const QString &request, addArg("reply_to_message_id", replyToMessageId); } + if (parseMode.size()) { + addArg("parse_mode", parseMode); + } + QFile readFile(file.absoluteFilePath()); if (!readFile.open(QIODevice::ReadOnly)) { qWarning() << "Fail to open file" << file.absoluteFilePath(); diff --git a/src/qTbot/src/private/requests/telegramsendfile.h b/src/qTbot/src/private/requests/telegramsendfile.h index e0d0596..a578f48 100644 --- a/src/qTbot/src/private/requests/telegramsendfile.h +++ b/src/qTbot/src/private/requests/telegramsendfile.h @@ -28,15 +28,18 @@ public: const QString &fileName, const QString &fileType, const QByteArray& data, + const QString& parseMode = "", unsigned long long replyToMessageId = 0, - const QMap > &extraObjects = {}); + const QHash > &extraObjects = {}); TelegramSendFile(const QString &request, const QVariant &chatId, const QString &text, const QFileInfo &file, + const QString& parseMode = "", + unsigned long long replyToMessageId = 0, - const QMap > &extraObjects = {}); + const QHash > &extraObjects = {}); RequestMethod method() const override; diff --git a/src/qTbot/src/private/requests/telegramsendmsg.cpp b/src/qTbot/src/private/requests/telegramsendmsg.cpp index 0b7acb2..7e864ae 100644 --- a/src/qTbot/src/private/requests/telegramsendmsg.cpp +++ b/src/qTbot/src/private/requests/telegramsendmsg.cpp @@ -13,9 +13,9 @@ namespace qTbot { TelegramSendMsg::TelegramSendMsg(const QVariant &chatId, const QString &text, - const QMap > &extraObjects, + const QHash > &extraObjects, unsigned long long replyToMessageId, - bool markdown, + const QString& parseMode, const QString &callBackQueryId, bool disableWebPagePreview) : @@ -32,8 +32,8 @@ TelegramSendMsg::TelegramSendMsg(const QVariant &chatId, args["reply_to_message_id"] = replyToMessageId; } - if (markdown) { - args["parse_mode"] = "Markdown"; + if (parseMode.size()) { + args["parse_mode"] = parseMode; } if (disableWebPagePreview) { diff --git a/src/qTbot/src/private/requests/telegramsendmsg.h b/src/qTbot/src/private/requests/telegramsendmsg.h index 89b45e3..1a6778a 100644 --- a/src/qTbot/src/private/requests/telegramsendmsg.h +++ b/src/qTbot/src/private/requests/telegramsendmsg.h @@ -17,11 +17,21 @@ namespace qTbot { class TelegramSendMsg: public TelegramSingleRquest { public: + /** + * @brief TelegramSendMsg + * @param chatId + * @param text + * @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 QMap>& extraObjects = {}, + const ExtraJsonObjects& extraObjects = {}, unsigned long long replyToMessageId = 0, - bool markdown = true, + const QString& parseMode = "", const QString& callBackQueryId = "", bool disableWebPagePreview = false ); diff --git a/src/qTbot/src/private/requests/telegramsendphoto.cpp b/src/qTbot/src/private/requests/telegramsendphoto.cpp index 2033602..464e331 100644 --- a/src/qTbot/src/private/requests/telegramsendphoto.cpp +++ b/src/qTbot/src/private/requests/telegramsendphoto.cpp @@ -15,19 +15,21 @@ TelegramSendPhoto::TelegramSendPhoto(const QVariant &chatId, const QString &text, const QString &fileName, const QByteArray &data, + const QString& parseMode, unsigned long long replyToMessageId, - const QMap>& extraObjects + const QHash > &extraObjects ): - TelegramSendFile("sendPhoto", chatId, text, fileName, TELEGRAM_PHOTO, data, replyToMessageId, extraObjects) { + TelegramSendFile("sendPhoto", chatId, text, fileName, TELEGRAM_PHOTO, data, parseMode, replyToMessageId, extraObjects) { } TelegramSendPhoto::TelegramSendPhoto(const QVariant &chatId, const QString &text, const QFileInfo &file, + const QString& parseMode, unsigned long long replyToMessageId, - const QMap > &extraObjects): - TelegramSendFile("sendPhoto", chatId, text, file, replyToMessageId, extraObjects) { + const QHash > &extraObjects): + TelegramSendFile("sendPhoto", chatId, text, file, parseMode, replyToMessageId, extraObjects) { } diff --git a/src/qTbot/src/private/requests/telegramsendphoto.h b/src/qTbot/src/private/requests/telegramsendphoto.h index 1f7eac7..df2a401 100644 --- a/src/qTbot/src/private/requests/telegramsendphoto.h +++ b/src/qTbot/src/private/requests/telegramsendphoto.h @@ -23,15 +23,17 @@ public: const QString &text, const QString &fileName, const QByteArray& data, + const QString& parseMode = "", unsigned long long replyToMessageId = 0, - const QMap>& extraObjects = {} + const ExtraJsonObjects& extraObjects = {} ); TelegramSendPhoto(const QVariant &chatId, const QString &text, const QFileInfo &file, + const QString& parseMode = "", unsigned long long replyToMessageId = 0, - const QMap>& extraObjects = {} + const ExtraJsonObjects& extraObjects = {} ); }; diff --git a/src/qTbot/src/public/qTbot/irequest.h b/src/qTbot/src/public/qTbot/irequest.h index 33b10d0..e9d3093 100644 --- a/src/qTbot/src/public/qTbot/irequest.h +++ b/src/qTbot/src/public/qTbot/irequest.h @@ -20,6 +20,11 @@ namespace qTbot { +/** + * @brief ExtraJsonObjects hash map of the extra objects of the message. + */ +typedef QHash> ExtraJsonObjects; + /** * @brief The iRequest class Is main interface for all custom requests. * @see IBot::sendRequest diff --git a/src/qTbot/src/public/qTbot/itelegrambot.cpp b/src/qTbot/src/public/qTbot/itelegrambot.cpp index 5545187..1ef0807 100644 --- a/src/qTbot/src/public/qTbot/itelegrambot.cpp +++ b/src/qTbot/src/public/qTbot/itelegrambot.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -70,10 +71,10 @@ bool ITelegramBot::sendMessage(const QVariant &chatId, const QString &text) { bool ITelegramBot::sendSpecificMessage(const QVariant & chatId, const QString &text, - const QMap> &extraObjects, + const ExtraJsonObjects &extraObjects, const QString &callBackQueryId, unsigned long long replyToMessageId, - bool markdown, + const QString& parseMode, bool disableWebPagePreview) { if (!chatId.isValid() || chatId.isNull()) @@ -87,7 +88,7 @@ bool ITelegramBot::sendSpecificMessage(const QVariant & chatId, text, extraObjects, replyToMessageId, - markdown, + parseMode, callBackQueryId, disableWebPagePreview); @@ -101,7 +102,7 @@ bool ITelegramBot::sendSpecificMessageWithKeyboard(const QVariant &chatId, bool onTimeKeyboard, bool autoResizeKeyboard, unsigned long long replyToMessageId, - bool markdown, + const QString &parseMode, bool disableWebPagePreview) { if (!chatId.isValid() || chatId.isNull()) @@ -111,7 +112,7 @@ bool ITelegramBot::sendSpecificMessageWithKeyboard(const QVariant &chatId, text, prepareKeyboard(autoResizeKeyboard, onTimeKeyboard, keyboard), replyToMessageId, - markdown, + parseMode, callBackQueryId, disableWebPagePreview); @@ -134,7 +135,7 @@ bool ITelegramBot::deleteMessage(const QVariant &chatId, const QVariant &message bool ITelegramBot::editSpecificMessageWithKeyboard(const QVariant & messageId, const QVariant &chatId, const QString &newText, - bool markdown, + const QString &parseMode, bool disableWebPagePreview, const QList> &keyboard, const QString &callBackQueryId, @@ -150,7 +151,7 @@ bool ITelegramBot::editSpecificMessageWithKeyboard(const QVariant & messageId, auto msg = QSharedPointer::create(messageId, chatId, newText, - markdown, + parseMode, disableWebPagePreview, callBackQueryId, prepareKeyboard(autoResizeKeyboard, @@ -160,10 +161,10 @@ bool ITelegramBot::editSpecificMessageWithKeyboard(const QVariant & messageId, return bool(sendRequest(msg)); } -QMap> -qTbot::ITelegramBot::prepareInlineKeyBoard(const QList > > &keyboard) +ExtraJsonObjects +qTbot::ITelegramBot::prepareInlineKeyBoard(const KeyboardOnMessage &keyboard) { - QMap> extraObjects; + ExtraJsonObjects extraObjects; auto&& keyboardJson = QSharedPointer::create(); QJsonArray keyboardArray; @@ -185,11 +186,11 @@ qTbot::ITelegramBot::prepareInlineKeyBoard(const QList> +ExtraJsonObjects qTbot::ITelegramBot::prepareKeyboard(bool autoResizeKeyboard, bool onTimeKeyboard, const QList> &keyboard) { - QMap> extraObjects; + ExtraJsonObjects extraObjects; auto&& keyboardJson = QSharedPointer::create(); QJsonArray keyboardArray; @@ -216,7 +217,7 @@ qTbot::ITelegramBot::prepareKeyboard(bool autoResizeKeyboard, bool ITelegramBot::editSpecificMessageWithKeyboard(const QVariant &messageId, const QVariant &chatId, const QString &text, - bool markdown, + const QString &parseMode, bool disableWebPagePreview, const KeyboardOnMessage &keyboard, const QString &callBackQueryId) { @@ -230,7 +231,7 @@ bool ITelegramBot::editSpecificMessageWithKeyboard(const QVariant &messageId, auto msg = QSharedPointer::create(messageId, chatId, text, - markdown, + parseMode, disableWebPagePreview, callBackQueryId, prepareInlineKeyBoard(keyboard)); @@ -239,11 +240,30 @@ bool ITelegramBot::editSpecificMessageWithKeyboard(const QVariant &messageId, return bool(sendRequest(msg)); } +bool ITelegramBot::editMessageKeyboard(const QVariant &messageId, + const QVariant &chatId, + const KeyboardOnMessage &keyboard, + const QString &callBackQueryId) { + if (!chatId.isValid() || chatId.isNull()) + return false; + + if (!messageId.isValid() || messageId.isNull()) + return false; + + auto msg = QSharedPointer::create(messageId, + chatId, + callBackQueryId, + prepareInlineKeyBoard(keyboard)); + + + return bool(sendRequest(msg)); +} + bool ITelegramBot::editSpecificMessage(const QVariant &messageId, const QVariant &chatId, const QString& newText, const QString &callBackQueryId, - bool markdown, + const QString &parseMode, bool disableWebPagePreview) { if (!chatId.isValid() || chatId.isNull()) @@ -258,7 +278,7 @@ bool ITelegramBot::editSpecificMessage(const QVariant &messageId, auto msg = QSharedPointer::create(messageId, chatId, newText, - markdown, + parseMode, disableWebPagePreview, callBackQueryId ); @@ -272,7 +292,7 @@ bool ITelegramBot::sendSpecificMessageWithKeyboard(const QVariant &chatId, const KeyboardOnMessage &keyboard, const QString &callBackQueryId, unsigned long long replyToMessageId, - bool markdown, + const QString &parseMode, bool disableWebPagePreview) { if (!chatId.isValid() || chatId.isNull()) @@ -286,7 +306,7 @@ bool ITelegramBot::sendSpecificMessageWithKeyboard(const QVariant &chatId, text, prepareInlineKeyBoard(keyboard), replyToMessageId, - markdown, + parseMode, callBackQueryId, disableWebPagePreview); @@ -386,6 +406,7 @@ bool ITelegramBot::sendFile(const QByteArray &file, const QString &fileName, con bool ITelegramBot::sendPhoto(const QFileInfo &photo, const QVariant &chatId, const QString &description, + const QString &parseMode, unsigned long long replyToMessageId, const KeyboardOnMessage &keyboard) { if (!chatId.isValid() || chatId.isNull()) @@ -395,14 +416,20 @@ bool ITelegramBot::sendPhoto(const QFileInfo &photo, return false; } - return sendFileWithPrivate(QSharedPointer::create( - chatId, description, photo, replyToMessageId, prepareInlineKeyBoard(keyboard))); + return sendFileWithPrivate( + QSharedPointer::create(chatId, + description, + photo, + parseMode, + replyToMessageId, + prepareInlineKeyBoard(keyboard))); } bool ITelegramBot::sendPhoto(const QByteArray &photo, const QString &fileName, const QVariant &chatId, const QString &description, + const QString &parseMode, unsigned long long replyToMessageId, const KeyboardOnMessage &keyboard) { @@ -417,8 +444,14 @@ bool ITelegramBot::sendPhoto(const QByteArray &photo, return false; } - return sendFileWithPrivate(QSharedPointer::create( - chatId, description, fileName, photo, replyToMessageId, prepareInlineKeyBoard(keyboard))); + return sendFileWithPrivate( + QSharedPointer::create(chatId, + description, + fileName, + photo, + parseMode, + replyToMessageId, + prepareInlineKeyBoard(keyboard))); } bool ITelegramBot::sendFileWithDescription(const QByteArray &file, diff --git a/src/qTbot/src/public/qTbot/itelegrambot.h b/src/qTbot/src/public/qTbot/itelegrambot.h index 6d739bd..51a424f 100644 --- a/src/qTbot/src/public/qTbot/itelegrambot.h +++ b/src/qTbot/src/public/qTbot/itelegrambot.h @@ -24,7 +24,7 @@ class TelegramUpdateAnswer; class TelegramSendFile; typedef std::function ButtonCB; -typedef QList> KeyboardOnMessage; +typedef QList> KeyboardOnMessage; /** * @brief The ITelegramBot class This is base implementation of the all telegramm bots. @@ -50,22 +50,21 @@ public: * @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 markdown Set to true to enable Markdown formatting for the text (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, the message will be sent with Markdown formatting enabled (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 QMap > &extraObjects = {}, + const qTbot::ExtraJsonObjects &extraObjects = {}, const QString &callBackQueryId = {}, unsigned long long replyToMessageId = 0, - bool markdown = true, + const QString &parseMode = "html", bool disableWebPagePreview = false); /** @@ -78,14 +77,15 @@ public: * @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 markdown Set to true to enable Markdown formatting for the text (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 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, the message will be sent with Markdown formatting enabled (optional). * @note By default, web page previews for links in the message are not disabled (optional). */ bool sendSpecificMessageWithKeyboard(const QVariant &chatId, @@ -93,13 +93,14 @@ public: const KeyboardOnMessage &keyboard, const QString &callBackQueryId = "", unsigned long long replyToMessageId = 0, - bool markdown = true, + const QString &parseMode = "html", bool disableWebPagePreview = false); /** * @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. + * 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. @@ -108,14 +109,14 @@ public: * @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 markdown Set to true to enable Markdown formatting for the text. + * @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, the message will be sent with Markdown formatting enabled. * @note By default, web page previews for links in the message are not disabled. */ bool sendSpecificMessageWithKeyboard(const QVariant &chatId, @@ -125,7 +126,7 @@ public: bool onTimeKeyboard = false, bool autoResizeKeyboard = true, unsigned long long replyToMessageId = 0, - bool markdown = true, + const QString &parseMode = "html", bool disableWebPagePreview = false); bool deleteMessage(const QVariant& chatId, const QVariant& messageId) override; @@ -138,7 +139,8 @@ public: * @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 markdown Set to true to enable Markdown formatting for the new text (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 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). @@ -149,19 +151,19 @@ public: * * @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, the new text will be displayed with Markdown formatting enabled (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, - bool markdown = true, + const QString &parseMode = "html", bool disableWebPagePreview = false, const QList > &keyboard = {}, const QString &callBackQueryId = "", bool onTimeKeyboard = false, bool autoResizeKeyboard = false); + /** * @brief Edits a specific message with a custom keyboard in a chat. * @@ -170,7 +172,8 @@ public: * @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 markdown Set to true to enable Markdown formatting for the new text (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 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). @@ -179,17 +182,37 @@ public: * * @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, the new text will be displayed with Markdown formatting enabled (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, - bool markdown = true, + const QString &parseMode = "html", bool disableWebPagePreview = false, const KeyboardOnMessage &keyboard = {}, const QString &callBackQueryId = ""); + /** + * @brief Edits a keyboard of message in a chat. + * + * This function allows you to edit a keyboard of specific message. + * + * @param messageId The unique identifier of the message to edit. + * @param chatId The unique identifier of the chat containing the message. + * @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 editMessageKeyboard(const QVariant& messageId, + const QVariant &chatId, + const KeyboardOnMessage &keyboard = {}, + const QString &callBackQueryId = ""); + /** * @brief Edits a specific message in a chat. * @@ -199,20 +222,20 @@ public: * @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 markdown Set to true to enable Markdown formatting for the new text (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). * * @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, the new text will be displayed with Markdown formatting enabled (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 = {}, - bool markdown = true, + const QString &parseMode = "html", bool disableWebPagePreview = false); [[nodiscard("do not forget to save shared pointer of file handler, because it's will not save inner bot object.")]] @@ -242,6 +265,7 @@ public: bool sendPhoto(const QFileInfo& photo, const QVariant& chatId, const QString &description, + const QString &parseMode = "html", unsigned long long replyToMessageId = 0, const KeyboardOnMessage &keyboard = {}); @@ -257,7 +281,7 @@ public: bool sendPhoto(const QByteArray& photo, const QString& fileName, const QVariant& chatId, - const QString &description, + const QString &description, const QString &parseMode, unsigned long long replyToMessageId = 0, const KeyboardOnMessage &keyboard = {}); @@ -411,10 +435,11 @@ private: bool sendFileWithPrivate(const QSharedPointer& file); QString findFileInlocatStorage(const QString& fileId) const; - QMap> - prepareInlineKeyBoard(const QList > > &keyboard); - QMap> - prepareKeyboard(bool autoResizeKeyboard, bool onTimeKeyboard, const QList > &keyboard); + QHash > + prepareInlineKeyBoard(const KeyboardOnMessage &keyboard); + ExtraJsonObjects prepareKeyboard(bool autoResizeKeyboard, + bool onTimeKeyboard, + const QList > &keyboard); unsigned long long _id = 0; From f5862a83a6deabc91b704bdef2b1386c768388b0 Mon Sep 17 00:00:00 2001 From: EndrII Date: Wed, 15 Nov 2023 23:52:25 +0100 Subject: [PATCH 02/12] handle last message id --- src/qTbot/src/public/qTbot/itelegrambot.cpp | 50 ++++++++++++++++----- src/qTbot/src/public/qTbot/itelegrambot.h | 19 +++++++- 2 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/qTbot/src/public/qTbot/itelegrambot.cpp b/src/qTbot/src/public/qTbot/itelegrambot.cpp index 1ef0807..4660848 100644 --- a/src/qTbot/src/public/qTbot/itelegrambot.cpp +++ b/src/qTbot/src/public/qTbot/itelegrambot.cpp @@ -92,7 +92,7 @@ bool ITelegramBot::sendSpecificMessage(const QVariant & chatId, callBackQueryId, disableWebPagePreview); - return bool(sendRequest(msg)); + return sendMessageRequest(msg); } bool ITelegramBot::sendSpecificMessageWithKeyboard(const QVariant &chatId, @@ -116,7 +116,7 @@ bool ITelegramBot::sendSpecificMessageWithKeyboard(const QVariant &chatId, callBackQueryId, disableWebPagePreview); - return bool(sendRequest(msg)); + return sendMessageRequest(msg); } bool ITelegramBot::deleteMessage(const QVariant &chatId, const QVariant &messageId) { @@ -129,7 +129,7 @@ bool ITelegramBot::deleteMessage(const QVariant &chatId, const QVariant &message auto msg = QSharedPointer::create(chatId, messageId); - return bool(sendRequest(msg)); + return sendMessageRequest(msg); } bool ITelegramBot::editSpecificMessageWithKeyboard(const QVariant & messageId, @@ -158,7 +158,7 @@ bool ITelegramBot::editSpecificMessageWithKeyboard(const QVariant & messageId, onTimeKeyboard, keyboard)); - return bool(sendRequest(msg)); + return sendMessageRequest(msg); } ExtraJsonObjects @@ -237,7 +237,7 @@ bool ITelegramBot::editSpecificMessageWithKeyboard(const QVariant &messageId, prepareInlineKeyBoard(keyboard)); - return bool(sendRequest(msg)); + return sendMessageRequest(msg); } bool ITelegramBot::editMessageKeyboard(const QVariant &messageId, @@ -256,7 +256,7 @@ bool ITelegramBot::editMessageKeyboard(const QVariant &messageId, prepareInlineKeyBoard(keyboard)); - return bool(sendRequest(msg)); + return sendMessageRequest(msg); } bool ITelegramBot::editSpecificMessage(const QVariant &messageId, @@ -284,7 +284,7 @@ bool ITelegramBot::editSpecificMessage(const QVariant &messageId, ); - return bool(sendRequest(msg)); + return sendMessageRequest(msg); } bool ITelegramBot::sendSpecificMessageWithKeyboard(const QVariant &chatId, @@ -310,7 +310,7 @@ bool ITelegramBot::sendSpecificMessageWithKeyboard(const QVariant &chatId, callBackQueryId, disableWebPagePreview); - return bool(sendRequest(msg)); + return sendMessageRequest(msg); } QSharedPointer ITelegramBot::getFile(const QString &fileId, iFile::Type fileType) { @@ -508,9 +508,9 @@ bool ITelegramBot::sendLocation(const QVariant &chatId, return false; } - auto&& request = QSharedPointer::create(chatId, text, latitude, longitude, replyToMessageId); + auto&& msg = QSharedPointer::create(chatId, text, latitude, longitude, replyToMessageId); - return bool(sendRequest(request)); + return sendMessageRequest(msg); } int ITelegramBot::getFileSizeByUniqueId(const QString &id) const { @@ -547,6 +547,30 @@ void ITelegramBot::handleIncomeNewUpdate(const QSharedPointer & update) } } +bool ITelegramBot::sendMessageRequest(const QSharedPointer &rquest) { + auto&& reply = IBot::sendRequest(rquest); + if (reply) { + connect(reply.get(), &QNetworkReply::finished, this, + [ reply, this]() { + + if (reply->error() == QNetworkReply::NoError) { + QByteArray&& responseData = reply->readAll(); + QJsonDocument json = QJsonDocument::fromJson(responseData); + + const QJsonObject&& obj = json.object(); + if (obj.contains("result")) { + unsigned long long chatId = obj["result"]["chat"]["id"].toInteger(); + if (chatId) { + _lastMessageId[chatId] = obj["result"]["message_id"].toInt(); + } + } + } + }); + } + + return bool(reply); +} + void ITelegramBot::handleLogin() { if (_loginReplay) { @@ -596,7 +620,7 @@ void ITelegramBot::handleFileHeader(const QWeakPointer &sender, } bool ITelegramBot::sendFileWithPrivate(const QSharedPointer &file) { - return bool(sendRequest(file)); + return sendMessageRequest(file); } QString ITelegramBot::findFileInlocatStorage(const QString &fileId) const { @@ -629,6 +653,10 @@ const QString &ITelegramBot::username() const { return _username; } +int ITelegramBot::gelLastMessageId(unsigned long long &chatId) const { + return _lastMessageId.value(chatId, 0); +} + unsigned long long ITelegramBot::id() const { return _id; } diff --git a/src/qTbot/src/public/qTbot/itelegrambot.h b/src/qTbot/src/public/qTbot/itelegrambot.h index 51a424f..de24453 100644 --- a/src/qTbot/src/public/qTbot/itelegrambot.h +++ b/src/qTbot/src/public/qTbot/itelegrambot.h @@ -22,6 +22,7 @@ class ITelegramMessage; class TelegramFile; class TelegramUpdateAnswer; class TelegramSendFile; +class TelegramSingleRquest; typedef std::function ButtonCB; typedef QList> KeyboardOnMessage; @@ -385,6 +386,13 @@ public: */ const QString& username() const; + /** + * @brief gelLastMessageId this method returns last sendet message id. + * @param chatId chat id, when you want to get last message id. + * @return message id. + */ + int gelLastMessageId(unsigned long long &chatId) const; + protected: /** @@ -423,6 +431,13 @@ protected: void handleIncomeNewUpdate(const QSharedPointer &) override; + /** + * @brief sendMessageRequest This method invoke when bot will be sent eny messages into chat. + * @param rquest This is a message request. + * @return true if the message sent successful else false. + */ + virtual bool sendMessageRequest(const QSharedPointer &rquest); + private slots: void handleLogin(); void handleLoginErr(QNetworkReply::NetworkError err); @@ -441,14 +456,16 @@ private: bool onTimeKeyboard, const QList > &keyboard); - unsigned long long _id = 0; QString _username; QSharedPointer _loginReplay; QMap> _handleButtons; + QHash _lastMessageId; + QHash> _filesMetaInfo; + }; } #endif // ITELEGRAMBOT_H From ada22dd3940272368803d0ed473af4638380e450 Mon Sep 17 00:00:00 2001 From: EndrII Date: Sat, 18 Nov 2023 00:04:48 +0100 Subject: [PATCH 03/12] 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 From 5cbe21b8515f5845ad0421801ae6102d629e9abc Mon Sep 17 00:00:00 2001 From: EndrII Date: Sat, 18 Nov 2023 10:35:32 +0100 Subject: [PATCH 04/12] fix #11 --- src/example/main.cpp | 33 ++- .../private/requests/telegrameditmessage.cpp | 20 +- .../private/requests/telegrameditmessage.h | 6 +- .../telegrameditmessagereplymarkup.cpp | 7 +- .../requests/telegrameditmessagereplymarkup.h | 3 +- .../private/requests/telegramsenddocument.cpp | 14 +- .../private/requests/telegramsenddocument.h | 10 +- .../src/private/requests/telegramsendfile.cpp | 40 +-- .../src/private/requests/telegramsendfile.h | 13 +- .../src/private/requests/telegramsendmsg.cpp | 2 +- .../src/private/requests/telegramsendmsg.h | 10 +- .../private/requests/telegramsendphoto.cpp | 23 +- .../src/private/requests/telegramsendphoto.h | 10 +- .../private/requests/telegramsinglerquest.cpp | 3 +- src/qTbot/src/public/qTbot/itelegrambot.cpp | 227 ++++++------------ src/qTbot/src/public/qTbot/itelegrambot.h | 64 ++--- .../{telegrammargs.cpp => telegramargs.cpp} | 23 +- .../qTbot/{telegrammargs.h => telegramargs.h} | 29 +-- 18 files changed, 180 insertions(+), 357 deletions(-) rename src/qTbot/src/public/qTbot/{telegrammargs.cpp => telegramargs.cpp} (63%) rename src/qTbot/src/public/qTbot/{telegrammargs.h => telegramargs.h} (60%) diff --git a/src/example/main.cpp b/src/example/main.cpp index f7fa0c0..8e01b82 100644 --- a/src/example/main.cpp +++ b/src/example/main.cpp @@ -45,21 +45,30 @@ int main(int argc, char *argv[]) { filesStack.push_back(bot.getFile(tmsg->audio()->fileId(), qTbot::iFile::Local)); } - bot.sendSpecificMessageWithKeyboard(tmsg->chatId(), - "I see it - я вижу это", + bot.sendSpecificMessageWithKeyboard(qTbot::TelegramArgs{tmsg->chatId(), "I see it", tmsg->messageId()}, {{{"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++),"", false, - {{{"test_button", [](auto , auto ){}}, {"test_button 2", [](auto , auto ){}}}}, - queryId); - }}}}, "", tmsg->messageId(), "", false); + static int index = 0; - bot.sendSpecificMessageWithKeyboard(tmsg->chatId(), - "I see it - я вижу это (интерактивная клавиатура)", + auto&& args = qTbot::TelegramArgs{tmsg->chatId(), + "I see it. Presed count: " + QString::number(index++), + tmsg->messageId(), + "", + false, + queryId}; + + auto&& keyboard = qTbot::KeyboardOnMessage{ + {{"test_button", [](auto , auto ){}}, + {"test_button 2", [](auto , auto ){}}}}; + + bot.editSpecificMessageWithKeyboard(msgId, + args, + keyboard + ); + }}}}); + + bot.sendSpecificMessageWithKeyboard(qTbot::TelegramArgs{tmsg->chatId(), "I see it", tmsg->messageId()}, {{{"test_button"}, - {"test_button"},}}, {}, true, true, tmsg->messageId()); + {"test_button"},}}, true, true); } } diff --git a/src/qTbot/src/private/requests/telegrameditmessage.cpp b/src/qTbot/src/private/requests/telegrameditmessage.cpp index 34d07bf..af21069 100644 --- a/src/qTbot/src/private/requests/telegrameditmessage.cpp +++ b/src/qTbot/src/private/requests/telegrameditmessage.cpp @@ -10,24 +10,12 @@ namespace qTbot { TelegramEditMessage::TelegramEditMessage(const QVariant &idEditedMessage, - const QVariant& chatId, - const QString& newText, - const QString& parseMode, - bool disableWebPagePreview, - const QString& callBackQueryId, - const QHash > &extraObjects): - TelegramSendMsg(chatId, - newText, - extraObjects, - 0, - parseMode, - callBackQueryId, - disableWebPagePreview) { - + const TelegramArgs &args, + const ExtraJsonObjects &extraObjects): + TelegramSendMsg(args, extraObjects){ setRequest("editMessageText"); addArg("message_id", idEditedMessage); - +} } -} diff --git a/src/qTbot/src/private/requests/telegrameditmessage.h b/src/qTbot/src/private/requests/telegrameditmessage.h index b04787c..f246865 100644 --- a/src/qTbot/src/private/requests/telegrameditmessage.h +++ b/src/qTbot/src/private/requests/telegrameditmessage.h @@ -18,11 +18,7 @@ class TelegramEditMessage: public TelegramSendMsg { public: TelegramEditMessage(const QVariant& idEditedMessage, - const QVariant& chatId, - const QString& newText, - const QString& parseMode = "", - bool disableWebPagePreview = false, - const QString& callBackQueryId = "", + const TelegramArgs& args, const ExtraJsonObjects& extraObjects = {}); }; } diff --git a/src/qTbot/src/private/requests/telegrameditmessagereplymarkup.cpp b/src/qTbot/src/private/requests/telegrameditmessagereplymarkup.cpp index 8d659cc..995568d 100644 --- a/src/qTbot/src/private/requests/telegrameditmessagereplymarkup.cpp +++ b/src/qTbot/src/private/requests/telegrameditmessagereplymarkup.cpp @@ -9,11 +9,10 @@ namespace qTbot { -TelegramEditMessageReplyMarkup::TelegramEditMessageReplyMarkup(const QVariant &idEditedMessage, - const QVariant &chatId, - const QString &callBackQueryId, +TelegramEditMessageReplyMarkup::TelegramEditMessageReplyMarkup(const QVariant& idEditedMessage, + const TelegramArgs& args, const ExtraJsonObjects &extraObjects): - TelegramEditMessage(idEditedMessage, chatId, "", "", false, callBackQueryId, extraObjects) + TelegramEditMessage(idEditedMessage, args, extraObjects) { setRequest("editMessageReplyMarkup"); } diff --git a/src/qTbot/src/private/requests/telegrameditmessagereplymarkup.h b/src/qTbot/src/private/requests/telegrameditmessagereplymarkup.h index 776374f..93e4df0 100644 --- a/src/qTbot/src/private/requests/telegrameditmessagereplymarkup.h +++ b/src/qTbot/src/private/requests/telegrameditmessagereplymarkup.h @@ -19,8 +19,7 @@ class TelegramEditMessageReplyMarkup: public TelegramEditMessage { public: TelegramEditMessageReplyMarkup(const QVariant& idEditedMessage, - const QVariant& chatId, - const QString& callBackQueryId = "", + const TelegramArgs &args, const ExtraJsonObjects& extraObjects = {}); }; } diff --git a/src/qTbot/src/private/requests/telegramsenddocument.cpp b/src/qTbot/src/private/requests/telegramsenddocument.cpp index 6e8f459..2dd5df3 100644 --- a/src/qTbot/src/private/requests/telegramsenddocument.cpp +++ b/src/qTbot/src/private/requests/telegramsenddocument.cpp @@ -8,21 +8,15 @@ namespace qTbot { -TelegramSendDocument::TelegramSendDocument(const QVariant &chatId, - const QString &text, +TelegramSendDocument::TelegramSendDocument(const TelegramArgs &args, const QString& fileName, const QByteArray &data, - const QString &parseMode, - unsigned long long replyToMessageId, const ExtraJsonObjects &extraObjects): - TelegramSendFile("sendDocument", chatId, text, fileName, TELEGRAM_DOCUMENT, data, parseMode, replyToMessageId, extraObjects) {} + TelegramSendFile("sendDocument", fileName, TELEGRAM_DOCUMENT, data, args, extraObjects) {} -TelegramSendDocument::TelegramSendDocument(const QVariant &chatId, - const QString &text, +TelegramSendDocument::TelegramSendDocument(const TelegramArgs &args, const QFileInfo &file, - const QString &parseMode, - unsigned long long replyToMessageId, const ExtraJsonObjects &extraObjects): - TelegramSendFile("sendDocument", chatId, text, file, parseMode, replyToMessageId, extraObjects) {} + TelegramSendFile("sendDocument", file, args, extraObjects) {} } diff --git a/src/qTbot/src/private/requests/telegramsenddocument.h b/src/qTbot/src/private/requests/telegramsenddocument.h index 8972925..bc19420 100644 --- a/src/qTbot/src/private/requests/telegramsenddocument.h +++ b/src/qTbot/src/private/requests/telegramsenddocument.h @@ -19,19 +19,13 @@ namespace qTbot { class TelegramSendDocument: public TelegramSendFile { public: - TelegramSendDocument(const QVariant &chatId, - const QString &text, + TelegramSendDocument(const TelegramArgs &args, const QString &fileName, const QByteArray& data, - const QString& parseMode = "", - unsigned long long replyToMessageId = 0, const ExtraJsonObjects& extraObjects = {}); - TelegramSendDocument(const QVariant &chatId, - const QString &text, + TelegramSendDocument(const TelegramArgs &args, const QFileInfo &file, - const QString& parseMode = "", - unsigned long long replyToMessageId = 0, const ExtraJsonObjects& extraObjects = {}); }; diff --git a/src/qTbot/src/private/requests/telegramsendfile.cpp b/src/qTbot/src/private/requests/telegramsendfile.cpp index aa6ec5c..f9727f5 100644 --- a/src/qTbot/src/private/requests/telegramsendfile.cpp +++ b/src/qTbot/src/private/requests/telegramsendfile.cpp @@ -12,28 +12,14 @@ namespace qTbot { qTbot::TelegramSendFile::TelegramSendFile(const QString &request, - const QVariant &chatId, - const QString &text, const QString &fileName, const QString &fileType, - const QByteArray &data, - const QString& parseMode, - unsigned long long replyToMessageId, + const QByteArray& data, + const TelegramArgs &args, const ExtraJsonObjects& extraObjects ): - TelegramSingleRquest(request) { + TelegramSingleRquest(request, args.toMap(true)) { - addArg("chat_id", chatId); - if (text.size()) - addArg("caption", text); - - if (replyToMessageId > 0) { - addArg("reply_to_message_id", replyToMessageId); - } - - if (parseMode.size()) { - addArg("parse_mode", parseMode); - } for (auto it = extraObjects.begin(); it != extraObjects.end(); it = std::next(it)) { addArg(it.key(), QJsonDocument(*it.value()).toJson(QJsonDocument::Compact)); @@ -43,26 +29,10 @@ qTbot::TelegramSendFile::TelegramSendFile(const QString &request, } qTbot::TelegramSendFile::TelegramSendFile(const QString &request, - const QVariant &chatId, - const QString &text, const QFileInfo &file, - const QString &parseMode, - unsigned long long replyToMessageId, + const TelegramArgs &args, const QHash > &extraObjects): - TelegramSingleRquest(request) { - - addArg("chat_id", chatId); - - if (text.size()) - addArg("text", text); - - if (replyToMessageId > 0) { - addArg("reply_to_message_id", replyToMessageId); - } - - if (parseMode.size()) { - addArg("parse_mode", parseMode); - } + TelegramSingleRquest(request, args.toMap(true)) { QFile readFile(file.absoluteFilePath()); if (!readFile.open(QIODevice::ReadOnly)) { diff --git a/src/qTbot/src/private/requests/telegramsendfile.h b/src/qTbot/src/private/requests/telegramsendfile.h index a578f48..3752ea5 100644 --- a/src/qTbot/src/private/requests/telegramsendfile.h +++ b/src/qTbot/src/private/requests/telegramsendfile.h @@ -10,6 +10,8 @@ #include "qfileinfo.h" #include "requests/telegramsinglerquest.h" +#include + #define TELEGRAM_PHOTO "photo" #define TELEGRAM_DOCUMENT "document" @@ -23,22 +25,15 @@ class TelegramSendFile: public TelegramSingleRquest public: TelegramSendFile(const QString &request, - const QVariant &chatId, - const QString &text, const QString &fileName, const QString &fileType, const QByteArray& data, - const QString& parseMode = "", - unsigned long long replyToMessageId = 0, + const TelegramArgs &args, const QHash > &extraObjects = {}); TelegramSendFile(const QString &request, - const QVariant &chatId, - const QString &text, const QFileInfo &file, - const QString& parseMode = "", - - unsigned long long replyToMessageId = 0, + const TelegramArgs &args, const QHash > &extraObjects = {}); RequestMethod method() const override; diff --git a/src/qTbot/src/private/requests/telegramsendmsg.cpp b/src/qTbot/src/private/requests/telegramsendmsg.cpp index d777da7..0c897e0 100644 --- a/src/qTbot/src/private/requests/telegramsendmsg.cpp +++ b/src/qTbot/src/private/requests/telegramsendmsg.cpp @@ -11,7 +11,7 @@ #include namespace qTbot { -TelegramSendMsg::TelegramSendMsg(const TelegrammArgs& generalArgs, +TelegramSendMsg::TelegramSendMsg(const TelegramArgs& generalArgs, const QHash > &extraObjects) : TelegramSingleRquest("sendMessage") diff --git a/src/qTbot/src/private/requests/telegramsendmsg.h b/src/qTbot/src/private/requests/telegramsendmsg.h index 0e41081..cb996de 100644 --- a/src/qTbot/src/private/requests/telegramsendmsg.h +++ b/src/qTbot/src/private/requests/telegramsendmsg.h @@ -8,7 +8,7 @@ #ifndef TELEGRAMSENDMSG_H #define TELEGRAMSENDMSG_H -#include "qTbot/telegrammargs.h" +#include "qTbot/telegramargs.h" #include "telegramsinglerquest.h" namespace qTbot { @@ -20,11 +20,11 @@ class TelegramSendMsg: public TelegramSingleRquest public: /** - * @brief TelegramSendMsg - * @param args - * @param extraObjects + * @brief TelegramSendMsg This object sents msg to telegram server. + * @param args this is generatl arguments of request (include target chat id). + * @param extraObjects additional objects of the telegram messages (json). */ - TelegramSendMsg(const TelegrammArgs& args, + TelegramSendMsg(const TelegramArgs& args, const ExtraJsonObjects& extraObjects = {}); }; } diff --git a/src/qTbot/src/private/requests/telegramsendphoto.cpp b/src/qTbot/src/private/requests/telegramsendphoto.cpp index 464e331..ca8420a 100644 --- a/src/qTbot/src/private/requests/telegramsendphoto.cpp +++ b/src/qTbot/src/private/requests/telegramsendphoto.cpp @@ -11,26 +11,15 @@ namespace qTbot { -TelegramSendPhoto::TelegramSendPhoto(const QVariant &chatId, - const QString &text, +TelegramSendPhoto::TelegramSendPhoto(const TelegramArgs &args, const QString &fileName, const QByteArray &data, - const QString& parseMode, - unsigned long long replyToMessageId, - const QHash > &extraObjects - ): - TelegramSendFile("sendPhoto", chatId, text, fileName, TELEGRAM_PHOTO, data, parseMode, replyToMessageId, extraObjects) { + const ExtraJsonObjects &extraObjects): + TelegramSendFile("sendPhoto", fileName, TELEGRAM_PHOTO, data, args, extraObjects) {} -} - -TelegramSendPhoto::TelegramSendPhoto(const QVariant &chatId, - const QString &text, +TelegramSendPhoto::TelegramSendPhoto(const TelegramArgs &args, const QFileInfo &file, - const QString& parseMode, - unsigned long long replyToMessageId, - const QHash > &extraObjects): - TelegramSendFile("sendPhoto", chatId, text, file, parseMode, replyToMessageId, extraObjects) { - -} + const ExtraJsonObjects &extraObjects): + TelegramSendFile("sendPhoto", file, args, extraObjects){} } diff --git a/src/qTbot/src/private/requests/telegramsendphoto.h b/src/qTbot/src/private/requests/telegramsendphoto.h index df2a401..b9c5c50 100644 --- a/src/qTbot/src/private/requests/telegramsendphoto.h +++ b/src/qTbot/src/private/requests/telegramsendphoto.h @@ -19,20 +19,14 @@ namespace qTbot { class TelegramSendPhoto: public TelegramSendFile { public: - TelegramSendPhoto(const QVariant &chatId, - const QString &text, + TelegramSendPhoto(const TelegramArgs &args, const QString &fileName, const QByteArray& data, - const QString& parseMode = "", - unsigned long long replyToMessageId = 0, const ExtraJsonObjects& extraObjects = {} ); - TelegramSendPhoto(const QVariant &chatId, - const QString &text, + TelegramSendPhoto(const TelegramArgs &args, const QFileInfo &file, - const QString& parseMode = "", - unsigned long long replyToMessageId = 0, const ExtraJsonObjects& extraObjects = {} ); diff --git a/src/qTbot/src/private/requests/telegramsinglerquest.cpp b/src/qTbot/src/private/requests/telegramsinglerquest.cpp index 0dcac10..8bf9068 100644 --- a/src/qTbot/src/private/requests/telegramsinglerquest.cpp +++ b/src/qTbot/src/private/requests/telegramsinglerquest.cpp @@ -17,7 +17,8 @@ namespace qTbot { TelegramSingleRquest::TelegramSingleRquest(const QString& request, const QMap& args) { setRequest(request); - setArgs(args);} + setArgs(args); +} TelegramSingleRquest::TelegramSingleRquest(const QString &request) { setRequest(request); diff --git a/src/qTbot/src/public/qTbot/itelegrambot.cpp b/src/qTbot/src/public/qTbot/itelegrambot.cpp index 6dcc1b1..dd8f4a0 100644 --- a/src/qTbot/src/public/qTbot/itelegrambot.cpp +++ b/src/qTbot/src/public/qTbot/itelegrambot.cpp @@ -66,10 +66,10 @@ bool ITelegramBot::login(const QByteArray &token) { } bool ITelegramBot::sendMessage(const QVariant &chatId, const QString &text) { - return sendSpecificMessage(TelegrammArgs{chatId, text}); + return sendSpecificMessage(TelegramArgs{chatId, text}); } -bool ITelegramBot::sendSpecificMessage(const TelegrammArgs& args, +bool ITelegramBot::sendSpecificMessage(const TelegramArgs& args, const ExtraJsonObjects &extraObjects) { if (!args.chatId.isValid() || args.chatId.isNull()) @@ -79,39 +79,14 @@ bool ITelegramBot::sendSpecificMessage(const TelegrammArgs& args, return false; } - auto msg = QSharedPointer::create(chatId, - text, - extraObjects, - replyToMessageId, - parseMode, - callBackQueryId, - disableWebPagePreview); + auto msg = QSharedPointer::create(args, extraObjects); return sendMessageRequest(msg); } -bool ITelegramBot::sendSpecificMessageWithKeyboard(const QVariant &chatId, - const QString &text, - const QList> &keyboard, - const QString &callBackQueryId, - bool onTimeKeyboard, - bool autoResizeKeyboard, - unsigned long long replyToMessageId, - const QString &parseMode, - bool disableWebPagePreview) { - - if (!chatId.isValid() || chatId.isNull()) - return false; - - auto msg = QSharedPointer::create(chatId, - text, - prepareKeyboard(autoResizeKeyboard, onTimeKeyboard, keyboard), - replyToMessageId, - parseMode, - callBackQueryId, - disableWebPagePreview); - - return sendMessageRequest(msg); +bool ITelegramBot::sendSpecificMessageWithKeyboard(const TelegramArgs& args, + const KeyboardOnMessage &keyboard) { + return sendSpecificMessage(args, prepareInlineKeyBoard(keyboard)); } bool ITelegramBot::deleteMessage(const QVariant &chatId, const QVariant &messageId) { @@ -127,28 +102,20 @@ bool ITelegramBot::deleteMessage(const QVariant &chatId, const QVariant &message return sendMessageRequest(msg); } -bool ITelegramBot::editSpecificMessageWithKeyboard(const QVariant & messageId, - const QVariant &chatId, - const QString &newText, - const QString &parseMode, - bool disableWebPagePreview, - const QList> &keyboard, - const QString &callBackQueryId, +bool ITelegramBot::editSpecificMessageWithKeyboard(const QVariant &messageId, + const TelegramArgs& args, + const QList > &keyboard, bool onTimeKeyboard, bool autoResizeKeyboard) { - if (!chatId.isValid() || chatId.isNull()) + if (!args.chatId.isValid() || args.chatId.isNull()) return false; if (!messageId.isValid() || messageId.isNull()) return false; auto msg = QSharedPointer::create(messageId, - chatId, - newText, - parseMode, - disableWebPagePreview, - callBackQueryId, + args, prepareKeyboard(autoResizeKeyboard, onTimeKeyboard, keyboard)); @@ -209,26 +176,19 @@ qTbot::ITelegramBot::prepareKeyboard(bool autoResizeKeyboard, return extraObjects; } -bool ITelegramBot::editSpecificMessageWithKeyboard(const QVariant &messageId, - const QVariant &chatId, - const QString &text, - const QString &parseMode, - bool disableWebPagePreview, - const KeyboardOnMessage &keyboard, - const QString &callBackQueryId) { +bool ITelegramBot::editSpecificMessageWithKeyboard(const QVariant& messageId, + const TelegramArgs& args, - if (!chatId.isValid() || chatId.isNull()) + const KeyboardOnMessage &keyboard ) { + + if (!args.chatId.isValid() || args.chatId.isNull()) return false; if (!messageId.isValid() || messageId.isNull()) return false; auto msg = QSharedPointer::create(messageId, - chatId, - text, - parseMode, - disableWebPagePreview, - callBackQueryId, + args, prepareInlineKeyBoard(keyboard)); @@ -246,8 +206,7 @@ bool ITelegramBot::editMessageKeyboard(const QVariant &messageId, return false; auto msg = QSharedPointer::create(messageId, - chatId, - callBackQueryId, + TelegramArgs(chatId, "", 0, "html", false, callBackQueryId), prepareInlineKeyBoard(keyboard)); @@ -255,57 +214,38 @@ bool ITelegramBot::editMessageKeyboard(const QVariant &messageId, } bool ITelegramBot::editSpecificMessage(const QVariant &messageId, - const QVariant &chatId, - const QString& newText, - const QString &callBackQueryId, - const QString &parseMode, - bool disableWebPagePreview) { + const TelegramArgs& args) { - if (!chatId.isValid() || chatId.isNull()) + if (!args.chatId.isValid() || args.chatId.isNull()) return false; if (!messageId.isValid() || messageId.isNull()) return false; - if (newText.isEmpty()) + if (args.text.isEmpty()) return false; auto msg = QSharedPointer::create(messageId, - chatId, - newText, - parseMode, - disableWebPagePreview, - callBackQueryId + args ); return sendMessageRequest(msg); } -bool ITelegramBot::sendSpecificMessageWithKeyboard(const QVariant &chatId, - const QString &text, - const KeyboardOnMessage &keyboard, - const QString &callBackQueryId, - unsigned long long replyToMessageId, - const QString &parseMode, - bool disableWebPagePreview) { +bool ITelegramBot::sendSpecificMessageWithKeyboard(const TelegramArgs& args, + const QList > &keyboard, + bool onTimeKeyboard, + bool autoResizeKeyboard) { - if (!chatId.isValid() || chatId.isNull()) + if (!args.chatId.isValid() || args.chatId.isNull()) return false; - if (text.isEmpty()) { + if (args.text.isEmpty()) { return false; } - auto msg = QSharedPointer::create(chatId, - text, - prepareInlineKeyBoard(keyboard), - replyToMessageId, - parseMode, - callBackQueryId, - disableWebPagePreview); - - return sendMessageRequest(msg); + return sendSpecificMessage(args, prepareKeyboard(autoResizeKeyboard, onTimeKeyboard, keyboard)); } QSharedPointer ITelegramBot::getFile(const QString &fileId, iFile::Type fileType) { @@ -391,78 +331,28 @@ QSharedPointer ITelegramBot::getFileMeta(const QString &fileId, c } bool ITelegramBot::sendFile(const QFileInfo &file, const QVariant &chatId) { - return sendFileWithDescription(file, chatId, ""); + return sendFileMessage({chatId}, file); } bool ITelegramBot::sendFile(const QByteArray &file, const QString &fileName, const QVariant &chatId) { - return sendFileWithDescription(file, fileName, chatId, ""); + return sendFileMessage({chatId}, file, fileName); } -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, - const QString &parseMode, - unsigned long long replyToMessageId, - const KeyboardOnMessage &keyboard) { - if (!chatId.isValid() || chatId.isNull()) +bool ITelegramBot::sendFileMessage(const TelegramArgs &args, const QFileInfo &file) { + if (!args.chatId.isValid() || args.chatId.isNull()) return false; - if (!photo.isReadable()) { + if (!file.isReadable()) { return false; } return sendFileWithPrivate( - QSharedPointer::create(chatId, - description, - photo, - parseMode, - replyToMessageId, - prepareInlineKeyBoard(keyboard))); + QSharedPointer::create(args, + file)); } -bool ITelegramBot::sendPhoto(const QByteArray &photo, - const QString &fileName, - const QVariant &chatId, - const QString &description, - const QString &parseMode, - unsigned long long replyToMessageId, - const KeyboardOnMessage &keyboard) { - - if (!chatId.isValid() || chatId.isNull()) - return false; - - if (!fileName.size()) { - return false; - } - - if (!photo.size()) { - return false; - } - - return sendFileWithPrivate( - QSharedPointer::create(chatId, - description, - fileName, - photo, - parseMode, - replyToMessageId, - prepareInlineKeyBoard(keyboard))); -} - -bool ITelegramBot::sendFileWithDescription(const QByteArray &file, - const QString &fileName, - const QVariant &chatId, - const QString &description) { - - if (!chatId.isValid() || chatId.isNull()) +bool ITelegramBot::sendFileMessage(const TelegramArgs &args, const QByteArray &file, const QString &fileName) { + if (!args.chatId.isValid() || args.chatId.isNull()) return false; if (!fileName.size()) { @@ -473,20 +363,47 @@ bool ITelegramBot::sendFileWithDescription(const QByteArray &file, return false; } - return sendFileWithPrivate(QSharedPointer::create(chatId, description, fileName, file)); + return sendFileWithPrivate(QSharedPointer::create(args, fileName, file)); } -bool ITelegramBot::sendFileWithDescription(const QFileInfo &file, - const QVariant &chatId, - const QString &description) { - if (!chatId.isValid() || chatId.isNull()) +bool ITelegramBot::sendPhoto(const TelegramArgs &args, + const QFileInfo &photo, + const KeyboardOnMessage &keyboard) { + if (!args.chatId.isValid() || args.chatId.isNull()) return false; - if (!file.isReadable()) { + if (!photo.isReadable()) { return false; } - return sendFileWithPrivate(QSharedPointer::create(chatId, description, file)); + return sendFileWithPrivate( + QSharedPointer::create(args, + photo, + prepareInlineKeyBoard(keyboard))); +} + +bool ITelegramBot::sendPhoto(const TelegramArgs &args, + const QByteArray &photo, + const QString &fileName, + const KeyboardOnMessage &keyboard) { + + if (!args.chatId.isValid() || args.chatId.isNull()) { + return false; + } + + if (!fileName.size()) { + return false; + } + + if (!photo.size()) { + return false; + } + + return sendFileWithPrivate( + QSharedPointer::create(args, + fileName, + photo, + prepareInlineKeyBoard(keyboard))); } bool ITelegramBot::sendFileById(const QString &fileID, const QVariant &chatId) { diff --git a/src/qTbot/src/public/qTbot/itelegrambot.h b/src/qTbot/src/public/qTbot/itelegrambot.h index 6d0ef46..7056a2f 100644 --- a/src/qTbot/src/public/qTbot/itelegrambot.h +++ b/src/qTbot/src/public/qTbot/itelegrambot.h @@ -11,7 +11,7 @@ #define ITELEGRAMBOT_H #include "ibot.h" -#include "qTbot/telegrammargs.h" +#include "qTbot/telegramargs.h" #include class QNetworkAccessManager; @@ -54,7 +54,7 @@ public: * * @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). */ - bool sendSpecificMessage(const TelegrammArgs& args, + bool sendSpecificMessage(const TelegramArgs& args, const qTbot::ExtraJsonObjects &extraObjects = {}); /** @@ -68,7 +68,7 @@ public: * @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. */ - bool sendSpecificMessageWithKeyboard(const TelegrammArgs& args, + bool sendSpecificMessageWithKeyboard(const TelegramArgs& args, const KeyboardOnMessage &keyboard); /** @@ -86,7 +86,7 @@ public: * * @note By default, web page previews for links in the message are not disabled. */ - bool sendSpecificMessageWithKeyboard(const TelegrammArgs& args, + bool sendSpecificMessageWithKeyboard(const TelegramArgs& args, const QList > &keyboard, bool onTimeKeyboard = false, bool autoResizeKeyboard = true); @@ -109,7 +109,7 @@ public: * @note By default, web page previews for links in the new text are not disabled (optional). */ bool editSpecificMessageWithKeyboard(const QVariant &messageId, - const TelegrammArgs& args, + const TelegramArgs& args, const QList > &keyboard = {}, bool onTimeKeyboard = false, bool autoResizeKeyboard = false); @@ -129,8 +129,8 @@ public: * * @note By default, web page previews for links in the new text are not disabled (optional). */ - bool editSpecificMessageWithKeyboard(const TelegrammArgs& args, - const QVariant& messageId, + bool editSpecificMessageWithKeyboard(const QVariant& messageId, + const TelegramArgs& args, const KeyboardOnMessage &keyboard = {}); /** @@ -167,7 +167,7 @@ public: * @note By default, web page previews for links in the new text are not disabled (optional). */ bool editSpecificMessage(const QVariant &messageId, - const TelegrammArgs& args); + const TelegramArgs& 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; @@ -191,7 +191,7 @@ public: * @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); + bool sendFileMessage( const TelegramArgs& args, const QFileInfo& file); /** * @brief sendFileMessage This method sents a message with file. @@ -200,62 +200,34 @@ public: * @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); + bool sendFileMessage( const TelegramArgs& args, const QByteArray& file, const QString& fileName); /** * @brief sendPhoto This method will send image into chat with @a chatId + * @param args - This is structure with general arguments of the tellegram message. + * Use This structure for sets target and another arguments. * @param photo this is photo path. - * @param chatId target chat - * @param replyToMessageId The unique identifier of the message to reply to, if any. * @param keyboard A list of maps where each map represents a button with a callback function (optional). * @return true if photo will snt successful */ - bool sendPhoto(const QFileInfo& photo, - const QVariant& chatId, - const QString &description, - const QString &parseMode = "html", - unsigned long long replyToMessageId = 0, + bool sendPhoto(const TelegramArgs& args, + const QFileInfo& photo, const KeyboardOnMessage &keyboard = {}); /** * @brief sendPhoto This method will send image into chat with @a chatId * @param photo this is photo data. - * @param chatId target chat + * @param args - This is structure with general arguments of the tellegram message. + * Use This structure for sets target and another arguments. * @param fileName This is dispalyed name of photo. - * @param replyToMessageId The unique identifier of the message to reply to, if any. * @param keyboard A list of maps where each map represents a button with a callback function (optional). * @return true if photo will snt successful */ - bool sendPhoto(const QByteArray& photo, + bool sendPhoto(const TelegramArgs &args, + const QByteArray& photo, const QString& fileName, - const QVariant& chatId, - const QString &description, const QString &parseMode, - unsigned long long replyToMessageId = 0, const KeyboardOnMessage &keyboard = {}); - /** - * @brief sendFileWithDescription This method sents a byte array as a file into @a chatId with additional text @a description. - * @param file This is a file source - * @param fileName This is a dispalyed name of file. - * @param chatId This is distanation id chat. - * @param description additional text for message. - * @return true if the message sents successful else false. - */ - bool sendFileWithDescription(const QByteArray& file, - const QString& fileName, - const QVariant& chatId, - const QString& description); - - /** - * @brief sendFileWithDescription This method sents a byte array as a file into @a chatId with additional text @a description. - * @param file This is a file source - * @param chatId This is distanation id chat. - * @param description additional text for message. - * @return true if the message sents successful else false. - */ - bool sendFileWithDescription( const QFileInfo& file, - const QVariant& chatId, - const QString& description); /** * @brief sendFileById This is specific method of the telegram bot. sents file by id. * @param fileID This is file id. diff --git a/src/qTbot/src/public/qTbot/telegrammargs.cpp b/src/qTbot/src/public/qTbot/telegramargs.cpp similarity index 63% rename from src/qTbot/src/public/qTbot/telegrammargs.cpp rename to src/qTbot/src/public/qTbot/telegramargs.cpp index d848103..cfb20af 100644 --- a/src/qTbot/src/public/qTbot/telegrammargs.cpp +++ b/src/qTbot/src/public/qTbot/telegramargs.cpp @@ -5,15 +5,15 @@ //# of this license document, but changing it is not allowed. //# -#include "telegrammargs.h" +#include "telegramargs.h" namespace qTbot { -TelegrammArgs::TelegrammArgs(const QVariant &id, - const QString &text, - const QString &parseMode, - unsigned long long replyToMessageId, - bool disableWebPagePreview, - const QString &callBackQueryId) +TelegramArgs::TelegramArgs(const QVariant &id, + const QString &text, + unsigned long long replyToMessageId, + const QString &parseMode, + bool disableWebPagePreview, + const QString &callBackQueryId) { this->chatId = id; @@ -24,11 +24,16 @@ TelegrammArgs::TelegrammArgs(const QVariant &id, this->parseMode = parseMode; } -QMap TelegrammArgs::toMap() const { +QMap TelegramArgs::toMap(bool textAsCaption) const { QMap args {{"chat_id", chatId}}; if (text.size()) { - args["text"] = text; + if (textAsCaption) { + args["caption"] = text; + + } else { + args["text"] = text; + } } if (replyToMessageId) { diff --git a/src/qTbot/src/public/qTbot/telegrammargs.h b/src/qTbot/src/public/qTbot/telegramargs.h similarity index 60% rename from src/qTbot/src/public/qTbot/telegrammargs.h rename to src/qTbot/src/public/qTbot/telegramargs.h index 69deadc..6c93217 100644 --- a/src/qTbot/src/public/qTbot/telegrammargs.h +++ b/src/qTbot/src/public/qTbot/telegramargs.h @@ -5,25 +5,25 @@ //# of this license document, but changing it is not allowed. //# -#ifndef TELEGRAMMARGS_H -#define TELEGRAMMARGS_H +#ifndef TELEGRAMARGS_H +#define TELEGRAMARGS_H #include namespace qTbot { /** - * @brief The TelegrammArgs class is base structure for the all tellegram message arguments. + * @brief The TelegramArgs class is base structure for the all tellegram message arguments. */ -struct TelegrammArgs +struct TelegramArgs { - TelegrammArgs(const QVariant& id, - const QString& text = "", - const QString& parseMode = "", - unsigned long long replyToMessageId = 0, - bool disableWebPagePreview = false, - const QString& callBackQueryId = "" - ); + TelegramArgs(const QVariant& id, + const QString& text = "", + unsigned long long replyToMessageId = 0, + const QString& parseMode = "html", + bool disableWebPagePreview = false, + const QString& callBackQueryId = "" + ); /** * @brief Text of the message. @@ -63,10 +63,11 @@ struct TelegrammArgs /** * @brief toMap convert all arguments to the map. - * @return + * @param textAsCaption This option force to replace text key to caption. Used on some requests as a sendDocument + * @return list of arguments. */ - QMap toMap() const; + QMap toMap(bool textAsCaption = false) const; }; } -#endif // TELEGRAMMARGS_H +#endif // TELEGRAMARGS_H From d67c1b6cdd975ce4803d1b6c013f9bb1b49c18a2 Mon Sep 17 00:00:00 2001 From: EndrII Date: Sat, 18 Nov 2023 10:38:16 +0100 Subject: [PATCH 05/12] added link to test bot --- src/example/main.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/example/main.cpp b/src/example/main.cpp index 8e01b82..a5260de 100644 --- a/src/example/main.cpp +++ b/src/example/main.cpp @@ -12,6 +12,8 @@ #include #include +// link to test bot +// @quasarapp_test_bot (https://t.me/quasarapp_test_bot) int main(int argc, char *argv[]) { QCoreApplication::setOrganizationName("QuasarApp"); From 32e87db332a14dd2c9630e59992a88113b5149c0 Mon Sep 17 00:00:00 2001 From: EndrII Date: Sat, 18 Nov 2023 10:53:09 +0100 Subject: [PATCH 06/12] added support cb to get messages Ids --- src/qTbot/src/public/qTbot/itelegrambot.cpp | 34 +++++++++++---------- src/qTbot/src/public/qTbot/itelegrambot.h | 7 ++--- src/qTbot/src/public/qTbot/telegramargs.cpp | 3 +- src/qTbot/src/public/qTbot/telegramargs.h | 8 ++++- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/qTbot/src/public/qTbot/itelegrambot.cpp b/src/qTbot/src/public/qTbot/itelegrambot.cpp index dd8f4a0..4e0cd81 100644 --- a/src/qTbot/src/public/qTbot/itelegrambot.cpp +++ b/src/qTbot/src/public/qTbot/itelegrambot.cpp @@ -81,7 +81,7 @@ bool ITelegramBot::sendSpecificMessage(const TelegramArgs& args, auto msg = QSharedPointer::create(args, extraObjects); - return sendMessageRequest(msg); + return sendMessageRequest(msg, args.msgIdCB); } bool ITelegramBot::sendSpecificMessageWithKeyboard(const TelegramArgs& args, @@ -120,7 +120,7 @@ bool ITelegramBot::editSpecificMessageWithKeyboard(const QVariant &messageId, onTimeKeyboard, keyboard)); - return sendMessageRequest(msg); + return sendMessageRequest(msg, args.msgIdCB); } ExtraJsonObjects @@ -346,9 +346,9 @@ bool ITelegramBot::sendFileMessage(const TelegramArgs &args, const QFileInfo &fi return false; } - return sendFileWithPrivate( + return sendMessageRequest( QSharedPointer::create(args, - file)); + file), args.msgIdCB); } bool ITelegramBot::sendFileMessage(const TelegramArgs &args, const QByteArray &file, const QString &fileName) { @@ -363,7 +363,7 @@ bool ITelegramBot::sendFileMessage(const TelegramArgs &args, const QByteArray &f return false; } - return sendFileWithPrivate(QSharedPointer::create(args, fileName, file)); + return sendMessageRequest(QSharedPointer::create(args, fileName, file), args.msgIdCB); } bool ITelegramBot::sendPhoto(const TelegramArgs &args, @@ -376,10 +376,10 @@ bool ITelegramBot::sendPhoto(const TelegramArgs &args, return false; } - return sendFileWithPrivate( + return sendMessageRequest( QSharedPointer::create(args, photo, - prepareInlineKeyBoard(keyboard))); + prepareInlineKeyBoard(keyboard)), args.msgIdCB); } bool ITelegramBot::sendPhoto(const TelegramArgs &args, @@ -399,11 +399,11 @@ bool ITelegramBot::sendPhoto(const TelegramArgs &args, return false; } - return sendFileWithPrivate( + return sendMessageRequest( QSharedPointer::create(args, fileName, photo, - prepareInlineKeyBoard(keyboard))); + prepareInlineKeyBoard(keyboard)), args.msgIdCB); } bool ITelegramBot::sendFileById(const QString &fileID, const QVariant &chatId) { @@ -467,11 +467,12 @@ void ITelegramBot::handleIncomeNewUpdate(const QSharedPointer & update) } } -bool ITelegramBot::sendMessageRequest(const QSharedPointer &rquest) { +bool ITelegramBot::sendMessageRequest(const QSharedPointer &rquest, + const std::function &msgIdCB) { auto&& reply = IBot::sendRequest(rquest); if (reply) { connect(reply.get(), &QNetworkReply::finished, this, - [ reply, this]() { + [ reply, msgIdCB, this]() { if (reply->error() == QNetworkReply::NoError) { QByteArray&& responseData = reply->readAll(); @@ -480,8 +481,13 @@ bool ITelegramBot::sendMessageRequest(const QSharedPointer &rquest) { const QJsonObject&& obj = json.object(); if (obj.contains("result")) { unsigned long long chatId = obj["result"]["chat"]["id"].toInteger(); + int messageID = obj["result"]["message_id"].toInt(); if (chatId) { - _lastMessageId[chatId] = obj["result"]["message_id"].toInt(); + _lastMessageId[chatId] = messageID; + } + + if (msgIdCB) { + msgIdCB(messageID); } } } @@ -539,10 +545,6 @@ void ITelegramBot::handleFileHeader(const QWeakPointer &sender, } } -bool ITelegramBot::sendFileWithPrivate(const QSharedPointer &file) { - return sendMessageRequest(file); -} - QString ITelegramBot::findFileInlocatStorage(const QString &fileId) const { QDir defaultFileDir(defaultFileStorageLocation()); diff --git a/src/qTbot/src/public/qTbot/itelegrambot.h b/src/qTbot/src/public/qTbot/itelegrambot.h index 7056a2f..f62f7aa 100644 --- a/src/qTbot/src/public/qTbot/itelegrambot.h +++ b/src/qTbot/src/public/qTbot/itelegrambot.h @@ -353,9 +353,11 @@ protected: /** * @brief sendMessageRequest This method invoke when bot will be sent eny messages into chat. * @param rquest This is a message request. + * @param msgIdCB call back function for the get a sent message id * @return true if the message sent successful else false. */ - virtual bool sendMessageRequest(const QSharedPointer &rquest); + virtual bool sendMessageRequest(const QSharedPointer &rquest, + const std::function& msgIdCB = {}); private slots: void handleLogin(); @@ -365,9 +367,6 @@ private slots: private: - - bool sendFileWithPrivate(const QSharedPointer& file); - QString findFileInlocatStorage(const QString& fileId) const; QHash > prepareInlineKeyBoard(const KeyboardOnMessage &keyboard); diff --git a/src/qTbot/src/public/qTbot/telegramargs.cpp b/src/qTbot/src/public/qTbot/telegramargs.cpp index cfb20af..19dec64 100644 --- a/src/qTbot/src/public/qTbot/telegramargs.cpp +++ b/src/qTbot/src/public/qTbot/telegramargs.cpp @@ -13,7 +13,7 @@ TelegramArgs::TelegramArgs(const QVariant &id, unsigned long long replyToMessageId, const QString &parseMode, bool disableWebPagePreview, - const QString &callBackQueryId) + const QString &callBackQueryId, const std::function &msgIdCB) { this->chatId = id; @@ -22,6 +22,7 @@ TelegramArgs::TelegramArgs(const QVariant &id, this->disableWebPagePreview = disableWebPagePreview; this->replyToMessageId = replyToMessageId; this->parseMode = parseMode; + this->msgIdCB = msgIdCB; } QMap TelegramArgs::toMap(bool textAsCaption) const { diff --git a/src/qTbot/src/public/qTbot/telegramargs.h b/src/qTbot/src/public/qTbot/telegramargs.h index 6c93217..38eeb9a 100644 --- a/src/qTbot/src/public/qTbot/telegramargs.h +++ b/src/qTbot/src/public/qTbot/telegramargs.h @@ -22,7 +22,8 @@ struct TelegramArgs unsigned long long replyToMessageId = 0, const QString& parseMode = "html", bool disableWebPagePreview = false, - const QString& callBackQueryId = "" + const QString& callBackQueryId = "", + const std::function& msgIdCB = {} ); /** @@ -67,6 +68,11 @@ struct TelegramArgs * @return list of arguments. */ QMap toMap(bool textAsCaption = false) const; + + /** + * @brief msgIdCB This is id message call bak function. Will be inwoked when request finished successful. + */ + std::function msgIdCB = {}; }; } From e5a3babd039f5f0392a1cc0b78334b192475f8e8 Mon Sep 17 00:00:00 2001 From: EndrII Date: Sat, 18 Nov 2023 11:37:17 +0100 Subject: [PATCH 07/12] minor fixes --- src/qTbot/src/public/qTbot/itelegrambot.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/qTbot/src/public/qTbot/itelegrambot.cpp b/src/qTbot/src/public/qTbot/itelegrambot.cpp index 4e0cd81..a1e8fb4 100644 --- a/src/qTbot/src/public/qTbot/itelegrambot.cpp +++ b/src/qTbot/src/public/qTbot/itelegrambot.cpp @@ -482,13 +482,13 @@ bool ITelegramBot::sendMessageRequest(const QSharedPointer &rquest, if (obj.contains("result")) { unsigned long long chatId = obj["result"]["chat"]["id"].toInteger(); int messageID = obj["result"]["message_id"].toInt(); - if (chatId) { - _lastMessageId[chatId] = messageID; - } - if (msgIdCB) { msgIdCB(messageID); } + + if (chatId) { + _lastMessageId[chatId] = messageID; + } } } }); From 05ca0542e15bd9496bab3abc538d792a258064fc Mon Sep 17 00:00:00 2001 From: EndrII Date: Sat, 18 Nov 2023 17:19:48 +0100 Subject: [PATCH 08/12] add suppor qpoint as location --- src/qTbot/src/public/qTbot/messages/telegramlocation.cpp | 5 +++++ src/qTbot/src/public/qTbot/messages/telegramlocation.h | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/src/qTbot/src/public/qTbot/messages/telegramlocation.cpp b/src/qTbot/src/public/qTbot/messages/telegramlocation.cpp index ba05eb1..f8112b4 100644 --- a/src/qTbot/src/public/qTbot/messages/telegramlocation.cpp +++ b/src/qTbot/src/public/qTbot/messages/telegramlocation.cpp @@ -6,6 +6,7 @@ //# #include "telegramlocation.h" +#include "qpoint.h" namespace qTbot { @@ -28,4 +29,8 @@ double TelegramLocation::longitude() const { return rawJson()["latitude"].toDouble(); } + +QPointF TelegramLocation::toQPoint() const { + return QPointF(latitude(), longitude()); +} } diff --git a/src/qTbot/src/public/qTbot/messages/telegramlocation.h b/src/qTbot/src/public/qTbot/messages/telegramlocation.h index a790b82..fc0ab49 100644 --- a/src/qTbot/src/public/qTbot/messages/telegramlocation.h +++ b/src/qTbot/src/public/qTbot/messages/telegramlocation.h @@ -38,6 +38,12 @@ public: * @return float value of longitude */ double longitude() const; + + /** + * @brief toQPoint This method convert location data to general qt class. + * @return location as a qt point + */ + QPointF toQPoint() const; }; } #endif // TELEGRAMLOCATION_H From dc19e2c331190fdf26793371a86013a2d2536223 Mon Sep 17 00:00:00 2001 From: EndrII Date: Sat, 18 Nov 2023 17:21:18 +0100 Subject: [PATCH 09/12] windows build --- src/qTbot/src/public/qTbot/telegramargs.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/qTbot/src/public/qTbot/telegramargs.h b/src/qTbot/src/public/qTbot/telegramargs.h index 38eeb9a..8f2581f 100644 --- a/src/qTbot/src/public/qTbot/telegramargs.h +++ b/src/qTbot/src/public/qTbot/telegramargs.h @@ -9,13 +9,14 @@ #define TELEGRAMARGS_H #include +#include "global.h" namespace qTbot { /** * @brief The TelegramArgs class is base structure for the all tellegram message arguments. */ -struct TelegramArgs +struct QTBOT_EXPORT TelegramArgs { TelegramArgs(const QVariant& id, const QString& text = "", From 2e91f03365a25264dec77ba42b311a0a2ac88bd3 Mon Sep 17 00:00:00 2001 From: EndrII Date: Sun, 19 Nov 2023 12:34:21 +0100 Subject: [PATCH 10/12] fix update request --- .../src/private/requests/telegramgetupdate.cpp | 5 ++++- .../src/private/requests/telegramgetupdate.h | 6 +++++- src/qTbot/src/public/qTbot/ibot.h | 2 +- src/qTbot/src/public/qTbot/telegramrestbot.cpp | 18 +++++++++++++++--- src/qTbot/src/public/qTbot/telegramrestbot.h | 3 +++ 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/qTbot/src/private/requests/telegramgetupdate.cpp b/src/qTbot/src/private/requests/telegramgetupdate.cpp index 87b0f11..6c4455b 100644 --- a/src/qTbot/src/private/requests/telegramgetupdate.cpp +++ b/src/qTbot/src/private/requests/telegramgetupdate.cpp @@ -10,6 +10,9 @@ namespace qTbot { -TelegramGetUpdate::TelegramGetUpdate(): TelegramSingleRquest("getUpdates"){} +TelegramGetUpdate::TelegramGetUpdate(unsigned long long offset): TelegramSingleRquest("getUpdates"){ + addArg("offset", offset); + addArg("timeout", 30); } +} diff --git a/src/qTbot/src/private/requests/telegramgetupdate.h b/src/qTbot/src/private/requests/telegramgetupdate.h index a209eb3..752143b 100644 --- a/src/qTbot/src/private/requests/telegramgetupdate.h +++ b/src/qTbot/src/private/requests/telegramgetupdate.h @@ -20,7 +20,11 @@ namespace qTbot { class TelegramGetUpdate final: public TelegramSingleRquest { public: - TelegramGetUpdate(); + /** + * @brief TelegramGetUpdate send request for updates. You will get response on immediacy or after 60 seconds if bot does not have any updates. + * @param offset - last accepted update id. + */ + TelegramGetUpdate(unsigned long long offset = 0); }; } #endif // TELEGRAMGETUPDATE_H diff --git a/src/qTbot/src/public/qTbot/ibot.h b/src/qTbot/src/public/qTbot/ibot.h index c2cdf49..3288d5a 100644 --- a/src/qTbot/src/public/qTbot/ibot.h +++ b/src/qTbot/src/public/qTbot/ibot.h @@ -126,7 +126,7 @@ public: * @brief setProcessed This method sets new list of processed mesages. * @param newProcessed list of processed messagees. */ - void setProcessed(const QSet &newProcessed); + virtual void setProcessed(const QSet &newProcessed); protected: diff --git a/src/qTbot/src/public/qTbot/telegramrestbot.cpp b/src/qTbot/src/public/qTbot/telegramrestbot.cpp index 10d8fb9..6ec0ac5 100644 --- a/src/qTbot/src/public/qTbot/telegramrestbot.cpp +++ b/src/qTbot/src/public/qTbot/telegramrestbot.cpp @@ -39,7 +39,7 @@ void TelegramRestBot::startUpdates() { if (delta >= _updateDelay) { - auto&& replay = sendRequest(QSharedPointer::create()); + auto&& replay = sendRequest(QSharedPointer::create(_lanstUpdateid + 1)); connect(replay.get(), &QNetworkReply::finished, this, std::bind(&TelegramRestBot::handleReceiveUpdates, this, replay.toWeakRef()), @@ -50,9 +50,9 @@ void TelegramRestBot::startUpdates() { Qt::DirectConnection); return; + } else { + QTimer::singleShot( _updateDelay - delta, this, [this](){startUpdates();}); } - - QTimer::singleShot( _updateDelay - delta, this, [this](){startUpdates();}); } int TelegramRestBot::updateDelay() const { @@ -63,6 +63,15 @@ void TelegramRestBot::setUpdateDelay(int newUpdateDelay) { _updateDelay = newUpdateDelay; } +void TelegramRestBot::setProcessed(const QSet &newProcessed) { + auto&& it = std::min_element(newProcessed.begin(), newProcessed.end()); + if (it != newProcessed.end()) { + _lanstUpdateid = *it; + } + + IBot::setProcessed(newProcessed); +} + void TelegramRestBot::handleReceiveUpdates(const QWeakPointer &replay) { if (auto&& sharedReplay = replay.lock()) { @@ -75,6 +84,9 @@ void TelegramRestBot::handleReceiveUpdates(const QWeakPointer &re for (const auto& ref: resultArray) { auto&& update = IBot::makeMesasge(ref.toObject()); incomeNewUpdate(update); + if (_lanstUpdateid < update->updateId()) { + _lanstUpdateid = update->updateId(); + }; } } } diff --git a/src/qTbot/src/public/qTbot/telegramrestbot.h b/src/qTbot/src/public/qTbot/telegramrestbot.h index 15ad46b..624c2f4 100644 --- a/src/qTbot/src/public/qTbot/telegramrestbot.h +++ b/src/qTbot/src/public/qTbot/telegramrestbot.h @@ -41,6 +41,7 @@ public: */ void setUpdateDelay(int newUpdateDelay); + void setProcessed(const QSet &newProcessed) override; private slots: void handleReceiveUpdates(const QWeakPointer& replay); void handleReceiveUpdatesErr(QNetworkReply::NetworkError err); @@ -49,6 +50,8 @@ private: void startUpdates(); long long _lanstUpdateTime = 0; + unsigned long long _lanstUpdateid = 0; + int _updateDelay = 1000; }; From 33cc4f0989e486f9a1ac4ca78f72e2ac24f09999 Mon Sep 17 00:00:00 2001 From: EndrII Date: Sun, 19 Nov 2023 17:45:07 +0100 Subject: [PATCH 11/12] fix send location method --- .../private/requests/telegramsendlocation.cpp | 18 +++++++----------- .../private/requests/telegramsendlocation.h | 6 +++--- src/qTbot/src/public/qTbot/itelegrambot.cpp | 18 ++++++++++-------- src/qTbot/src/public/qTbot/itelegrambot.h | 10 +++++----- 4 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/qTbot/src/private/requests/telegramsendlocation.cpp b/src/qTbot/src/private/requests/telegramsendlocation.cpp index 3d68780..5433d94 100644 --- a/src/qTbot/src/private/requests/telegramsendlocation.cpp +++ b/src/qTbot/src/private/requests/telegramsendlocation.cpp @@ -6,25 +6,21 @@ //# #include "telegramsendlocation.h" + +#include namespace qTbot { -TelegramSendLocation::TelegramSendLocation(const QVariant &chatId, - const QString &text, +TelegramSendLocation::TelegramSendLocation(const TelegramArgs &args, float latitude, float longitude, - unsigned long long replyToMessageId - ): - TelegramSingleRquest("sendLocation") { - - addArg("chat_id", chatId); - if (text.size()) - addArg("caption", text); + const ExtraJsonObjects &extraObjects): + TelegramSingleRquest("sendLocation", args.toMap(true)) { addArg("latitude", latitude); addArg("longitude", longitude); - if (replyToMessageId) { - addArg("reply_to_message_id", replyToMessageId); + for (auto it = extraObjects.begin(); it != extraObjects.end(); it = std::next(it)) { + addArg(it.key(), QJsonDocument(*it.value()).toJson(QJsonDocument::Compact)); } } diff --git a/src/qTbot/src/private/requests/telegramsendlocation.h b/src/qTbot/src/private/requests/telegramsendlocation.h index f1f5627..e96d54c 100644 --- a/src/qTbot/src/private/requests/telegramsendlocation.h +++ b/src/qTbot/src/private/requests/telegramsendlocation.h @@ -8,6 +8,7 @@ #ifndef TELEGRAMSENDLOCATION_H #define TELEGRAMSENDLOCATION_H +#include "qTbot/telegramargs.h" #include "requests/telegramsinglerquest.h" namespace qTbot { @@ -17,11 +18,10 @@ namespace qTbot { class TelegramSendLocation: public TelegramSingleRquest { public: - TelegramSendLocation(const QVariant &chatId, - const QString &text, + TelegramSendLocation(const TelegramArgs &args, float latitude, float longitude, - unsigned long long replyToMessageId); + const ExtraJsonObjects& extraObjects = {}); }; } #endif // TELEGRAMSENDLOCATION_H diff --git a/src/qTbot/src/public/qTbot/itelegrambot.cpp b/src/qTbot/src/public/qTbot/itelegrambot.cpp index a1e8fb4..018fe83 100644 --- a/src/qTbot/src/public/qTbot/itelegrambot.cpp +++ b/src/qTbot/src/public/qTbot/itelegrambot.cpp @@ -403,7 +403,8 @@ bool ITelegramBot::sendPhoto(const TelegramArgs &args, QSharedPointer::create(args, fileName, photo, - prepareInlineKeyBoard(keyboard)), args.msgIdCB); + prepareInlineKeyBoard(keyboard)), + args.msgIdCB); } bool ITelegramBot::sendFileById(const QString &fileID, const QVariant &chatId) { @@ -416,21 +417,22 @@ bool ITelegramBot::sendFileById(const QString &fileID, const QVariant &chatId) { } -bool ITelegramBot::sendLocation(const QVariant &chatId, - const QString &text, +bool ITelegramBot::sendLocation(const TelegramArgs &args, float latitude, float longitude, - unsigned long long replyToMessageId) { - if (!chatId.isValid() || chatId.isNull()) + const KeyboardOnMessage &keyboard) { + + if (!args.chatId.isValid() || args.chatId.isNull()) return false; if (!(longitude && latitude)) { return false; } - auto&& msg = QSharedPointer::create(chatId, text, latitude, longitude, replyToMessageId); - - return sendMessageRequest(msg); + return sendMessageRequest(QSharedPointer::create(args, + latitude, + longitude, + prepareInlineKeyBoard(keyboard))); } int ITelegramBot::getFileSizeByUniqueId(const QString &id) const { diff --git a/src/qTbot/src/public/qTbot/itelegrambot.h b/src/qTbot/src/public/qTbot/itelegrambot.h index f62f7aa..0c4649e 100644 --- a/src/qTbot/src/public/qTbot/itelegrambot.h +++ b/src/qTbot/src/public/qTbot/itelegrambot.h @@ -238,17 +238,17 @@ public: /** * @brief sendLocation This method sents locatin to user. - * @param chatId This is distanation id chat. + * @param args - This is structure with general arguments of the tellegram message. + * Use This structure for sets target and another arguments. * @param latitude * @param longitude - * @param replyToMessageId The unique identifier of the message to reply to, skip if you want to sent new independet message. + * @param keyboard A list of maps where each map represents a button with a callback function (optional). * @return true if locations sent successful else false */ - bool sendLocation(const QVariant& chatId, - const QString& text, + bool sendLocation(const TelegramArgs &args, float latitude, float longitude, - unsigned long long replyToMessageId = 0); + const KeyboardOnMessage &keyboard = {}); // to do From 9d09c6b5c1de47a3bb53e338d2aed978b3399d59 Mon Sep 17 00:00:00 2001 From: EndrII Date: Sun, 19 Nov 2023 22:33:10 +0100 Subject: [PATCH 12/12] added sentContact method --- .../private/requests/telegramsendcontact.cpp | 25 +++++++++++++++ .../private/requests/telegramsendcontact.h | 31 +++++++++++++++++++ src/qTbot/src/public/qTbot/itelegrambot.cpp | 14 +++++++++ src/qTbot/src/public/qTbot/itelegrambot.h | 15 +++++++-- .../qTbot/messages/telegramlocation.cpp | 2 +- 5 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 src/qTbot/src/private/requests/telegramsendcontact.cpp create mode 100644 src/qTbot/src/private/requests/telegramsendcontact.h diff --git a/src/qTbot/src/private/requests/telegramsendcontact.cpp b/src/qTbot/src/private/requests/telegramsendcontact.cpp new file mode 100644 index 0000000..55e58aa --- /dev/null +++ b/src/qTbot/src/private/requests/telegramsendcontact.cpp @@ -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. +//# + +#include "telegramsendcontact.h" + +namespace qTbot { + + +TelegramSendContact::TelegramSendContact(const TelegramArgs &args, + const QString &firstName, + const QString &phone, + const QString &lastName): + TelegramSingleRquest("sendContact", args.toMap(true)) { + + addArg("first_name", firstName); + addArg("last_name", lastName); + addArg("phone_number", phone); + +} + +} diff --git a/src/qTbot/src/private/requests/telegramsendcontact.h b/src/qTbot/src/private/requests/telegramsendcontact.h new file mode 100644 index 0000000..5e7e5c0 --- /dev/null +++ b/src/qTbot/src/private/requests/telegramsendcontact.h @@ -0,0 +1,31 @@ +//# +//# 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 TELEGRAMSENDCONTACT_H +#define TELEGRAMSENDCONTACT_H + +#include "qTbot/telegramargs.h" +#include "telegramsinglerquest.h" + +namespace qTbot { + +/** + * @brief The TelegramSendContact class + */ +class TelegramSendContact: public TelegramSingleRquest +{ +public: + + TelegramSendContact(const TelegramArgs &args, + const QString &firstName, + const QString &phone, + const QString &lastName); +}; +} +#endif // TELEGRAMSENDCONTACT_H diff --git a/src/qTbot/src/public/qTbot/itelegrambot.cpp b/src/qTbot/src/public/qTbot/itelegrambot.cpp index 018fe83..535fef9 100644 --- a/src/qTbot/src/public/qTbot/itelegrambot.cpp +++ b/src/qTbot/src/public/qTbot/itelegrambot.cpp @@ -10,6 +10,7 @@ #include "file.h" #include "requests/telegrammdownloadfile.h" #include "qdir.h" +#include "requests/telegramsendcontact.h" #include "requests/telegramsenddocument.h" #include "virtualfile.h" #include @@ -435,6 +436,19 @@ bool ITelegramBot::sendLocation(const TelegramArgs &args, prepareInlineKeyBoard(keyboard))); } +bool ITelegramBot::sendContact(const TelegramArgs &args, + const QString &phone, + const QString &firstName, + const QString &secondName) { + if (!args.chatId.isValid() || args.chatId.isNull()) + return false; + + return sendMessageRequest(QSharedPointer::create(args, + firstName, + phone, + secondName)); +} + int ITelegramBot::getFileSizeByUniqueId(const QString &id) const { if (auto && file = _filesMetaInfo.value(id)) { return file->fileSize(); diff --git a/src/qTbot/src/public/qTbot/itelegrambot.h b/src/qTbot/src/public/qTbot/itelegrambot.h index 0c4649e..00270b1 100644 --- a/src/qTbot/src/public/qTbot/itelegrambot.h +++ b/src/qTbot/src/public/qTbot/itelegrambot.h @@ -250,15 +250,26 @@ public: float longitude, const KeyboardOnMessage &keyboard = {}); + /** + * @brief sendContact This method sents a contact data. + * @param args base arguments of message. + * @param firstName This is first name of contact. + * @param phone This is phone number of contact. + * @param secondName This is second name of user. + * @return true if contact sent successful + */ + bool sendContact(const TelegramArgs &args, + const QString& phone, + const QString& firstName, + const QString& secondName = ""); + // to do // * forwardMessage implementations // * copyMessage implementations - // * sendPhoto implementations // * sendAudio implementations // * sendVideo implementations // * sendVoice implementations - // * sendLocation implementations // * sendContact implementations // * sendPoll implementations // * sendDice implementations diff --git a/src/qTbot/src/public/qTbot/messages/telegramlocation.cpp b/src/qTbot/src/public/qTbot/messages/telegramlocation.cpp index f8112b4..0a7f38d 100644 --- a/src/qTbot/src/public/qTbot/messages/telegramlocation.cpp +++ b/src/qTbot/src/public/qTbot/messages/telegramlocation.cpp @@ -26,7 +26,7 @@ double TelegramLocation::latitude() const { } double TelegramLocation::longitude() const { - return rawJson()["latitude"].toDouble(); + return rawJson()["longitude"].toDouble(); }