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<QString, QSharedPointer<QJsonObject> > &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<QString, QSharedPointer<QJsonObject> > &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 <qTbot/telegramargs.h> + #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<QString, QSharedPointer<QJsonObject> > &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<QString, QSharedPointer<QJsonObject> > &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 <QJsonObject> namespace qTbot { -TelegramSendMsg::TelegramSendMsg(const TelegrammArgs& generalArgs, +TelegramSendMsg::TelegramSendMsg(const TelegramArgs& generalArgs, const QHash<QString, QSharedPointer<QJsonObject> > &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<QString, QSharedPointer<QJsonObject> > &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<QString, QSharedPointer<QJsonObject> > &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<QString, QVariant>& 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<TelegramSendMsg>::create(chatId, - text, - extraObjects, - replyToMessageId, - parseMode, - callBackQueryId, - disableWebPagePreview); + auto msg = QSharedPointer<TelegramSendMsg>::create(args, extraObjects); return sendMessageRequest(msg); } -bool ITelegramBot::sendSpecificMessageWithKeyboard(const QVariant &chatId, - const QString &text, - const QList<QList<QString>> &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<TelegramSendMsg>::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<QList<QString>> &keyboard, - const QString &callBackQueryId, +bool ITelegramBot::editSpecificMessageWithKeyboard(const QVariant &messageId, + const TelegramArgs& args, + const QList<QList<QString> > &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<TelegramEditMessage>::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<TelegramEditMessage>::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<TelegramEditMessageReplyMarkup>::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<TelegramEditMessage>::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<QList<QString> > &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<TelegramSendMsg>::create(chatId, - text, - prepareInlineKeyBoard(keyboard), - replyToMessageId, - parseMode, - callBackQueryId, - disableWebPagePreview); - - return sendMessageRequest(msg); + return sendSpecificMessage(args, prepareKeyboard(autoResizeKeyboard, onTimeKeyboard, keyboard)); } QSharedPointer<iFile> ITelegramBot::getFile(const QString &fileId, iFile::Type fileType) { @@ -391,78 +331,28 @@ QSharedPointer<QNetworkReply> 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<TelegramSendPhoto>::create(chatId, - description, - photo, - parseMode, - replyToMessageId, - prepareInlineKeyBoard(keyboard))); + QSharedPointer<TelegramSendPhoto>::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<TelegramSendPhoto>::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<TelegramSendDocument>::create(chatId, description, fileName, file)); + return sendFileWithPrivate(QSharedPointer<TelegramSendDocument>::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<TelegramSendDocument>::create(chatId, description, file)); + return sendFileWithPrivate( + QSharedPointer<TelegramSendPhoto>::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<TelegramSendPhoto>::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 <QObject> 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<QList<QString> > &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<QList<QString> > &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<iFile> 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<QString, QVariant> TelegrammArgs::toMap() const { +QMap<QString, QVariant> TelegramArgs::toMap(bool textAsCaption) const { QMap<QString, QVariant> 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 <QVariant> 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<QString, QVariant> toMap() const; + QMap<QString, QVariant> toMap(bool textAsCaption = false) const; }; } -#endif // TELEGRAMMARGS_H +#endif // TELEGRAMARGS_H