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<TelegramSendMsg>::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<TelegramSendPhoto>::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<TelegramSendDocument>::create(args, fileName, file)); + return sendMessageRequest(QSharedPointer<TelegramSendDocument>::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<TelegramSendPhoto>::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<TelegramSendPhoto>::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<iUpdate> & update) } } -bool ITelegramBot::sendMessageRequest(const QSharedPointer<iRequest> &rquest) { +bool ITelegramBot::sendMessageRequest(const QSharedPointer<iRequest> &rquest, + const std::function<void (int)> &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<iRequest> &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<QNetworkReply> &sender, } } -bool ITelegramBot::sendFileWithPrivate(const QSharedPointer<TelegramSendFile> &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<iRequest> &rquest); + virtual bool sendMessageRequest(const QSharedPointer<iRequest> &rquest, + const std::function<void(int msgId)>& msgIdCB = {}); private slots: void handleLogin(); @@ -365,9 +367,6 @@ private slots: private: - - bool sendFileWithPrivate(const QSharedPointer<TelegramSendFile>& file); - QString findFileInlocatStorage(const QString& fileId) const; QHash<QString, QSharedPointer<QJsonObject> > 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<void (int)> &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<QString, QVariant> 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<void(int msgId)>& msgIdCB = {} ); /** @@ -67,6 +68,11 @@ struct TelegramArgs * @return list of arguments. */ QMap<QString, QVariant> toMap(bool textAsCaption = false) const; + + /** + * @brief msgIdCB This is id message call bak function. Will be inwoked when request finished successful. + */ + std::function<void(int msgId)> msgIdCB = {}; }; }