mirror of
https://github.com/QuasarApp/qTbot.git
synced 2025-04-26 22:04:33 +00:00
handle last message id
This commit is contained in:
parent
66aac2a5d4
commit
f5862a83a6
@ -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<TelegramDeleteMessage>::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<iFile> ITelegramBot::getFile(const QString &fileId, iFile::Type fileType) {
|
||||
@ -508,9 +508,9 @@ bool ITelegramBot::sendLocation(const QVariant &chatId,
|
||||
return false;
|
||||
}
|
||||
|
||||
auto&& request = QSharedPointer<TelegramSendLocation>::create(chatId, text, latitude, longitude, replyToMessageId);
|
||||
auto&& msg = QSharedPointer<TelegramSendLocation>::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<iUpdate> & update)
|
||||
}
|
||||
}
|
||||
|
||||
bool ITelegramBot::sendMessageRequest(const QSharedPointer<iRequest> &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<QNetworkReply> &sender,
|
||||
}
|
||||
|
||||
bool ITelegramBot::sendFileWithPrivate(const QSharedPointer<TelegramSendFile> &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;
|
||||
}
|
||||
|
@ -22,6 +22,7 @@ class ITelegramMessage;
|
||||
class TelegramFile;
|
||||
class TelegramUpdateAnswer;
|
||||
class TelegramSendFile;
|
||||
class TelegramSingleRquest;
|
||||
|
||||
typedef std::function<void(const QString& buttonKey, const QVariant& msgID)> ButtonCB;
|
||||
typedef QList<QHash<QString, ButtonCB >> 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<iUpdate> &) 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<iRequest> &rquest);
|
||||
|
||||
private slots:
|
||||
void handleLogin();
|
||||
void handleLoginErr(QNetworkReply::NetworkError err);
|
||||
@ -441,14 +456,16 @@ private:
|
||||
bool onTimeKeyboard,
|
||||
const QList<QList<QString> > &keyboard);
|
||||
|
||||
|
||||
unsigned long long _id = 0;
|
||||
QString _username;
|
||||
QSharedPointer<QNetworkReply> _loginReplay;
|
||||
QMap<QString, std::function<void(const QString&, const QVariant&)>> _handleButtons;
|
||||
|
||||
QHash<unsigned long long, int> _lastMessageId;
|
||||
|
||||
QHash<QString, QSharedPointer<TelegramFile>> _filesMetaInfo;
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
#endif // ITELEGRAMBOT_H
|
||||
|
Loading…
x
Reference in New Issue
Block a user