From f5862a83a6deabc91b704bdef2b1386c768388b0 Mon Sep 17 00:00:00 2001 From: EndrII Date: Wed, 15 Nov 2023 23:52:25 +0100 Subject: [PATCH] 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