use update instant messages

This commit is contained in:
Andrei Yankovich 2023-09-27 21:56:54 +02:00
parent 79b33b3995
commit fa99f10788
22 changed files with 135 additions and 120 deletions

View File

@ -21,7 +21,7 @@
],
"extraLib": "crypto",
"targetDir": "/media/D/builds/qTbot/Distro",
"deployVersion": "0.24.f96a9a8",
"deployVersion": "0.27.79b33b3",
}

View File

@ -10,6 +10,7 @@
#include <QCoreApplication>
#include <qTbot/messages/telegrammsg.h>
#include <qTbot/messages/telegramupdate.h>
int main(int argc, char *argv[]) {
@ -22,25 +23,29 @@ int main(int argc, char *argv[]) {
qTbot::TelegramRestBot bot;
QList<QSharedPointer<qTbot::iFile> > filesStack;
QObject::connect(&bot, &qTbot::TelegramRestBot::sigReceiveMessage, [&bot, &filesStack](auto){
while(auto&& msg = bot.takeNextUnreadMessage()) {
QObject::connect(&bot, &qTbot::TelegramRestBot::sigReceiveUpdate, [&bot, &filesStack](auto){
while(auto&& update = bot.takeNextUnreadUpdate()) {
if (auto&& tmsg = msg.dynamicCast<qTbot::TelegramMsg>()) {
if (auto&& tupdate = update.dynamicCast<qTbot::TelegramUpdate>()) {
if (tupdate->contains(tupdate->MessageUpdate)) {
if (auto&& tmsg = tupdate->message()) {
if (tmsg->contains(tmsg->Document)) {
filesStack.push_back(bot.getFile(tmsg->documents()->fileId(), qTbot::iFile::Local));
}
if (tmsg->contains(tmsg->Image)) {
filesStack.push_back(bot.getFile(tmsg->image()->fileId(), qTbot::iFile::Local));
}
if (tmsg->contains(tmsg->Audio)) {
filesStack.push_back(bot.getFile(tmsg->audio()->fileId(), qTbot::iFile::Local));
}
bot.sendSpecificMessage(tmsg->chatId(), "I see it - я вижу это", tmsg->messageId());
}
if (tmsg->contains(tmsg->Document)) {
filesStack.push_back(bot.getFile(tmsg->documents()->fileId(), qTbot::iFile::Local));
}
if (tmsg->contains(tmsg->Image)) {
filesStack.push_back(bot.getFile(tmsg->image()->fileId(), qTbot::iFile::Local));
}
if (tmsg->contains(tmsg->Audio)) {
filesStack.push_back(bot.getFile(tmsg->audio()->fileId(), qTbot::iFile::Local));
}
bot.sendSpecificMessage(tmsg->chatId(), "I see it - я вижу это", tmsg->messageId());
}
}
});

View File

@ -36,6 +36,7 @@ void File::handleReadReady() {
if (bytes.size()) {
_localFile.write(bytes);
_localFile.flush();
}
}

View File

@ -10,7 +10,7 @@
#include <QtCore/qglobal.h>
#define QTBOT_VERSION "0.24.f96a9a8"
#define QTBOT_VERSION "0.27.79b33b3"
#if defined(QTBOT_LIBRARY)
# define QTBOT_EXPORT Q_DECL_EXPORT

View File

@ -27,18 +27,18 @@ void IBot::setToken(const QByteArray &newToken) {
_token = newToken;
}
void IBot::incomeNewMessage(const QSharedPointer<iMessage> &message) {
void IBot::incomeNewUpdate(const QSharedPointer<iUpdate> &message) {
if (!message->isValid())
return;
auto id = message->messageId();
auto id = message->updateId();
if (!_processed.contains(id)) {
_processed.insert(id);
_notProcessedMessages[message->messageId()] = message;
_notProcessedUpdates[id] = message;
emit sigReceiveMessage(message);
emit sigReceiveUpdate(message);
}
}
@ -68,15 +68,15 @@ QSharedPointer<QNetworkReply> IBot::sendRequest(const QSharedPointer<iRequest> &
return networkReplay;
}
void IBot::markMessageAsProcessed(const QSharedPointer<iMessage> &message) {
_notProcessedMessages.remove(message->messageId());
void IBot::markUpdateAsProcessed(const QSharedPointer<iUpdate> &message) {
_notProcessedUpdates.remove(message->updateId());
}
void IBot::markMessageAsUnprocessed(const QSharedPointer<iMessage> &message) {
return markMessageAsUnprocessed(message->messageId());
void IBot::markUpdateAsUnprocessed(const QSharedPointer<iUpdate> &message) {
return markUpdateAsUnprocessed(message->updateId());
}
void IBot::markMessageAsUnprocessed(unsigned long long messageID) {
void IBot::markUpdateAsUnprocessed(unsigned long long messageID) {
_processed.remove(messageID);
}
@ -108,10 +108,10 @@ void IBot::setName(const QString &newName) {
_name = newName;
}
QSharedPointer<iMessage> IBot::takeNextUnreadMessage() {
if (_notProcessedMessages.size()) {
auto toRemove = std::move(*_notProcessedMessages.begin());
_notProcessedMessages.erase(_notProcessedMessages.cbegin());
QSharedPointer<iUpdate> IBot::takeNextUnreadUpdate() {
if (_notProcessedUpdates.size()) {
auto toRemove = std::move(*_notProcessedUpdates.begin());
_notProcessedUpdates.erase(_notProcessedUpdates.cbegin());
return toRemove;
}

View File

@ -10,7 +10,7 @@
#define IBOT_H
#include "qTbot/global.h"
#include "qTbot/imessage.h"
#include "qTbot/iupdate.h"
#include "qTbot/irequest.h"
#include "ifile.h"
@ -85,10 +85,10 @@ public:
void setName(const QString &newName);
/**
* @brief takeNextUnreadMessage This method take a unread message and mark them as read.
* @brief takeNextUnreadUpdate This method take a unread update and mark them as read.
* @return unread message object. If all messages is readed the return nullptr.
*/
QSharedPointer<iMessage> takeNextUnreadMessage();
QSharedPointer<iUpdate> takeNextUnreadUpdate();
/**
* @brief processed This method return list of processed mesages.
@ -155,29 +155,29 @@ protected:
void setToken(const QByteArray &newToken);
/**
* @brief incomeNewMessage This method save income message into store.
* @brief incomeNewUpdate This method save incomed messages into store.
*/
virtual void incomeNewMessage(const QSharedPointer<iMessage>& message);
virtual void incomeNewUpdate(const QSharedPointer<iUpdate>& message);
/**
* @brief markMessageAsProcessed This method remove message from the not processed messages store.
* @param message This is message that need to be processed.
*/
void markMessageAsProcessed(const QSharedPointer<iMessage>& message);
void markUpdateAsProcessed(const QSharedPointer<iUpdate>& message);
/**
* @brief markMessageAsUnprocessed This method add the message into a not processed messages store.
* @param message This is message that need to be unprocessed.
* @note this may be useful for the process edited old messages. Just call this method bofore IBot::incomeNewMessage.
*/
void markMessageAsUnprocessed(const QSharedPointer<iMessage>& message);
void markUpdateAsUnprocessed(const QSharedPointer<iUpdate>& message);
/**
* @brief markMessageAsUnprocessed This method add the message into a not processed messages store.
* @param message This is message that need to be unprocessed.
* @note this may be useful for the process edited old messages. Just call this method bofore IBot::incomeNewMessage.
*/
void markMessageAsUnprocessed(unsigned long long messageID);
void markUpdateAsUnprocessed(unsigned long long messageID);
/**
* @brief defaultFileStorageLocation This method return default file storage location.
@ -186,16 +186,16 @@ protected:
virtual QString defaultFileStorageLocation() const;
signals:
/**
* @brief sigReceiveMessage emit when but receive any updates from users.
* @brief sigReceiveUpdate emit when but receive any updates from users.
*/
void sigReceiveMessage(const QSharedPointer<iMessage>& );
void sigReceiveUpdate(const QSharedPointer<iUpdate>& );
private:
void doRemoveFinishedRequests();
QByteArray _token;
QString _name;
QMap<unsigned long long, QSharedPointer<iMessage>> _notProcessedMessages;
QMap<unsigned long long, QSharedPointer<iUpdate>> _notProcessedUpdates;
QSet<unsigned long long> _processed;
QNetworkAccessManager *_manager = nullptr;

View File

@ -6,21 +6,21 @@
//#
#include "ijsonbasedmessage.h"
#include "ijsonbasedupdate.h"
#include "qjsondocument.h"
namespace qTbot {
IJsonBasedMessage::IJsonBasedMessage() {
IJsonBasedUpdate::IJsonBasedUpdate() {
}
bool IJsonBasedMessage::isValid() const {
bool IJsonBasedUpdate::isValid() const {
return !rawJson().empty();
}
void IJsonBasedMessage::setRawData(const QByteArray &newRawData) {
iMessage::setRawData(newRawData);
void IJsonBasedUpdate::setRawData(const QByteArray &newRawData) {
iUpdate::setRawData(newRawData);
auto doc = QJsonDocument::fromJson(newRawData);
if (!doc.isObject()) {

View File

@ -6,10 +6,10 @@
//#
#ifndef IJSONBASEDMESSAGE_H
#define IJSONBASEDMESSAGE_H
#ifndef IJSONBASEDUPDATE_H
#define IJSONBASEDUPDATE_H
#include "imessage.h"
#include "iupdate.h"
#include "qTbot/ijsonbasedobject.h"
#include <QJsonObject>
@ -17,16 +17,16 @@
namespace qTbot {
/**
* @brief The IJsonBasedMessage class This is message that have a json sructure
* @brief The IJsonBasedUpdate class This is updates that have a json sructure
*/
class QTBOT_EXPORT IJsonBasedMessage: public iMessage, public IJsonBasedObject
class QTBOT_EXPORT IJsonBasedUpdate: public iUpdate, public IJsonBasedObject
{
public:
IJsonBasedMessage();
IJsonBasedUpdate();
// iMessage interface
bool isValid() const override;
void setRawData(const QByteArray &newRawData) override;
};
}
#endif // IJSONBASEDMESSAGE_H
#endif // IJSONBASEDUPDATE_H

View File

@ -6,7 +6,6 @@
//#
#include "itelegrambot.h"
#include "qTbot/messages/telegrammsg.h"
#include "qTbot/messages/telegramupdateanswer.h"
#include "file.h"
#include "qTbot/requests/telegrammdownloadfile.h"
@ -24,6 +23,7 @@
#include <qTbot/messages/telegramfile.h>
#include <qTbot/messages/telegramfile.h>
#include <qTbot/messages/telegramupdate.h>
namespace qTbot {

View File

@ -11,6 +11,7 @@
#define ITELEGRAMBOT_H
#include "ibot.h"
#include "qTbot/messages/telegrammsg.h"
#include <QObject>
class QNetworkAccessManager;
@ -144,6 +145,7 @@ public:
const QString& username() const;
protected:
/**
* @brief setId This method sets new value for the ITelegramBot::id property.
* @param newId this is new value of the ITelegramBot::id property.
@ -193,6 +195,7 @@ private:
QHash<QString, QSharedPointer<TelegramFile>> _filesMetaInfo;
};
}
#endif // ITELEGRAMBOT_H

View File

@ -5,23 +5,23 @@
//# of this license document, but changing it is not allowed.
//#
#include "imessage.h"
#include "iupdate.h"
namespace qTbot {
iMessage::iMessage() {
iUpdate::iUpdate() {
}
const QByteArray& iMessage::rawData() const {
const QByteArray& iUpdate::rawData() const {
return _rawData;
}
void iMessage::setRawData(const QByteArray &newRawData) {
void iUpdate::setRawData(const QByteArray &newRawData) {
_rawData = newRawData;
}
bool iMessage::isValid() const {
bool iUpdate::isValid() const {
return _rawData.size();
}

View File

@ -7,8 +7,8 @@
#ifndef IMESSAGE_H
#define IMESSAGE_H
#ifndef IUPDATE_H
#define IUPDATE_H
#include "qTbot/global.h"
#include <QByteArray>
@ -25,10 +25,10 @@ namespace qTbot {
* @see IBot::takeNextUnreadMessage
* @see IBot::makeMesasge
*/
class QTBOT_EXPORT iMessage
class QTBOT_EXPORT iUpdate
{
public:
iMessage();
iUpdate();
/**
* @brief rawData returns raw data of the message.
@ -62,14 +62,14 @@ public:
virtual QVariant chatId() const = 0;
/**
* @brief messageId This method returns numeric id of the message.
* @return numeric id of the message.
* @brief updateId This method returns numeric id of the update.
* @return numeric id of the update.
*/
virtual unsigned long long messageId() const = 0;
virtual unsigned long long updateId() const = 0;
private:
QByteArray _rawData;
};
}
#endif // IMESSAGE_H
#endif // IUPDATE_H

View File

@ -5,12 +5,10 @@
//# of this license document, but changing it is not allowed.
//#
#include "imessage.h"
#include "itelegrammessage.h"
#include <QJsonDocument>
namespace qTbot {
ITelegramMessage::ITelegramMessage():IJsonBasedMessage(){}
qTbot::iMessage::iMessage()
{
}

View File

@ -0,0 +1,29 @@
//#
//# 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 IMESSAGE_H
#define IMESSAGE_H
#include "qTbot/ijsonbasedupdate.h"
namespace qTbot {
/**
* @brief The iMessage class This is main interface for the all messages.
*/
class QTBOT_EXPORT iMessage: public IJsonBasedUpdate
{
public:
iMessage();
/**
* @brief updateId This method returns numeric id of the message.
* @return numeric id of the message.
*/
virtual unsigned long long messageId() const = 0;
};
}
#endif // IMESSAGE_H

View File

@ -1,26 +0,0 @@
//#
//# 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 ITELEGRAMMESSAGE_H
#define ITELEGRAMMESSAGE_H
#include "qTbot/ijsonbasedmessage.h"
namespace qTbot {
/**
* @brief The ItelegramMessage class This is base interface of the all telegram messages.
*/
class QTBOT_EXPORT ITelegramMessage: public IJsonBasedMessage
{
public:
ITelegramMessage();
};
}
#endif // ITELEGRAMMESSAGE_H

View File

@ -163,4 +163,8 @@ QSharedPointer<TelegramAudio> TelegramMsg::audio() const {
return QSharedPointer<TelegramAudio>::create(rawJson()[Audio].toObject());
}
unsigned long long TelegramMsg::updateId() const {
return 0;
}
}

View File

@ -8,7 +8,7 @@
#ifndef TELEGRAMMSG_H
#define TELEGRAMMSG_H
#include "itelegrammessage.h"
#include "imessage.h"
#include "telegramaudio.h"
#include "telegramdocument.h"
#include "qTbot/messages/telegramimage.h"
@ -19,7 +19,7 @@ namespace qTbot {
* @brief The TelegramMsg class This class provide general mesasges of telegram.
* The message object can contains text, geo or link to video,image. all this dates can be contains in one object.
*/
class QTBOT_EXPORT TelegramMsg: public ITelegramMessage
class QTBOT_EXPORT TelegramMsg: public iMessage
{
public:
@ -200,7 +200,7 @@ public:
*/
QSharedPointer<TelegramAudio> audio() const;
unsigned long long updateId() const override;
};
}

View File

@ -16,12 +16,13 @@ bool TelegramUpdate::contains(const Type &type) {
return rawJson().contains(type);
}
QJsonObject TelegramUpdate::message() const {
return rawJson().value(MessageUpdate).toObject();
QSharedPointer<TelegramMsg> TelegramUpdate::message() const {
return QSharedPointer<TelegramMsg>::create(rawJson()[MessageUpdate].toObject());
}
QJsonObject TelegramUpdate::editedMessage() const {
return rawJson().value(EditedMessageUpdate).toObject();
QSharedPointer<TelegramMsg> TelegramUpdate::editedMessage() const {
return QSharedPointer<TelegramMsg>::create(rawJson()[EditedMessageUpdate].toObject());
}
QJsonObject TelegramUpdate::channelPost() const {
@ -88,7 +89,4 @@ QVariant TelegramUpdate::chatId() const {
return "";
}
unsigned long long TelegramUpdate::messageId() const {
return 0;
}
}

View File

@ -8,13 +8,16 @@
#ifndef TELEGRAMUPDATE_H
#define TELEGRAMUPDATE_H
#include "qTbot/messages/itelegrammessage.h"
#include <qTbot/ijsonbasedupdate.h>
#include "telegrammsg.h"
namespace qTbot {
/**
* @brief The TelegramUpdate class contains base information about updates from telegram
*/
class QTBOT_EXPORT TelegramUpdate: public ITelegramMessage
class QTBOT_EXPORT TelegramUpdate: public IJsonBasedUpdate
{
public:
TelegramUpdate();
@ -50,13 +53,13 @@ public:
* @brief message returns the array of the updates messages.
* @return list of updates.
*/
QJsonObject message() const;
QSharedPointer<TelegramMsg> message() const;
/**
* @brief editedMessage returns the edited message update.
* @return QJsonObject containing the edited message update.
*/
QJsonObject editedMessage() const;
QSharedPointer<TelegramMsg> editedMessage() const;
/**
* @brief channelPost returns the channel post update.
@ -134,13 +137,12 @@ public:
* @brief updateId returns the update ID.
* @return The update ID.
*/
unsigned long long updateId() const;
unsigned long long updateId() const override;
// iMessage interface
public:
bool isValid() const override;
QString from() const override;
QVariant chatId() const override;
unsigned long long messageId() const override;
};
}
#endif // TELEGRAMUPDATE_H

View File

@ -34,7 +34,7 @@ QVariant TelegramUpdateAnswer::chatId() const {
return {};
}
unsigned long long TelegramUpdateAnswer::messageId() const {
unsigned long long TelegramUpdateAnswer::updateId() const {
return 0;
}

View File

@ -6,17 +6,18 @@
//#
#ifndef TELEGRAMUPDATEANSVER_H
#define TELEGRAMUPDATEANSVER_H
#include "itelegrammessage.h"
#include <qTbot/ijsonbasedupdate.h>
namespace qTbot {
/**
* @brief The TelegramUpdateAnswer class This is base message from the telegram server after update request.
*/
class TelegramUpdateAnswer: public ITelegramMessage
class TelegramUpdateAnswer: public IJsonBasedUpdate
{
public:
TelegramUpdateAnswer();
@ -45,7 +46,7 @@ public:
QString from() const override;
QVariant chatId() const override;
unsigned long long messageId() const override;
unsigned long long updateId() const override;
};

View File

@ -74,7 +74,7 @@ void TelegramRestBot::handleReceiveUpdates(const QWeakPointer<QNetworkReply> &re
auto && resultArray = telegramMsg->result().toArray();
for (const auto& ref: resultArray) {
auto&& update = IBot::makeMesasge<TelegramUpdate>(ref.toObject());
incomeNewMessage(IBot::makeMesasge<TelegramMsg>(update->message()));
incomeNewUpdate(update);
}
}
}