qTbot 0.87.9547b0c
qTbot is base back end library for your c++ Qt projects.
ibot.h
Go to the documentation of this file.
1//#
2//# Copyright (C) 2021-2024 QuasarApp.
3//# Distributed under the GPLv3 software license, see the accompanying
4//# Everyone is permitted to copy and distribute verbatim copies
5//# of this license document, but changing it is not allowed.
6//#
7
8
9#ifndef IBOT_H
10#define IBOT_H
11
12#include "qTbot/global.h"
13#include "qTbot/iupdate.h"
14#include "qTbot/irequest.h"
15
16#include "ifile.h"
17#include "qfileinfo.h"
18
19#include <QMap>
20#include <QHash>
21#include <QSet>
22
23#include <QNetworkReply>
24#include <QObject>
25#include <QSharedPointer>
26
27namespace qTbot {
28
29class ReplayData;
30
34class QTBOT_EXPORT IBot: public QObject
35{
36 Q_OBJECT
37public:
38 IBot();
39 ~IBot();
40
46 virtual bool login(const QByteArray& token) = 0;
47
51 virtual void logout();
52
61 virtual bool sendMessage(const QVariant& chatId, const QString& text) = 0;
62
69 virtual bool deleteMessage(const QVariant& chatId, const QVariant& messageId) = 0;
70
80 virtual QSharedPointer<iFile> getFile(const QString& fileId, iFile::Type fileType = iFile::Type::Ram) = 0;
81
89 virtual bool sendFile( const QFileInfo& file, const QVariant& chatId) = 0;
90
98 virtual bool sendFile( const QByteArray& file, const QString& fileName, const QVariant& chatId) = 0;
99
104 const QByteArray &token() const;
105
110 const QString& name() const;
111
116 void setName(const QString &newName);
117
122 QSharedPointer<iUpdate> takeNextUnreadUpdate();
123
128 QSet<unsigned long long> processed() const;
129
134 virtual void setProcessed(const QSet<unsigned long long> &newProcessed);
135
136protected:
137
144 template<class MessageType, class ... Args>
145 static QSharedPointer<MessageType> makeMesasge(const QByteArray& data, Args&& ...args) {
146 auto&& ptr = QSharedPointer<MessageType>(new MessageType(std::forward<Args>(args)...));
147 ptr->setRawData(data);
148
149 return ptr;
150 }
151
158 template<class MessageType, class ... Args>
159 static QSharedPointer<MessageType> makeMesasge(const QJsonObject& data, Args&& ...args) {
160 auto&& ptr = QSharedPointer<MessageType>(new MessageType(std::forward<Args>(args)...));
161 ptr->setRawJson(data);
162
163 return ptr;
164 }
165
166
172 virtual QString makeUrl(const QSharedPointer<iRequest>& request) const = 0;
173
180 QSharedPointer<QNetworkReply>
181 sendRequest(const QSharedPointer<iRequest>& rquest);
182
187 void setToken(const QByteArray &newToken);
188
192 void incomeNewUpdate(const QSharedPointer<iUpdate>& message);
193
198 void markUpdateAsProcessed(const QSharedPointer<iUpdate>& message);
199
205 void markUpdateAsUnprocessed(const QSharedPointer<iUpdate>& message);
206
212 void markUpdateAsUnprocessed(unsigned long long messageID);
213
218 virtual QString defaultFileStorageLocation() const;
219
224 virtual void handleIncomeNewUpdate(const QSharedPointer<iUpdate>& );
225signals:
229 void sigReceiveUpdate(const QSharedPointer<iUpdate>& );
230
235
236private:
237 void doRemoveFinishedRequests();
238
239 QByteArray _token;
240 QString _name;
241 QMap<unsigned long long, QSharedPointer<iUpdate>> _notProcessedUpdates;
242 QSet<unsigned long long> _processed;
243 QNetworkAccessManager *_manager = nullptr;
244
245 QMap<size_t,QSharedPointer<QNetworkReply>> _replayStorage;
246 QList<size_t> _toRemove;
247
248};
249
250}
251#endif // IBOT_H
The IBot class Base interface for all chat-bots objcts.
Definition ibot.h:35
virtual QString makeUrl(const QSharedPointer< iRequest > &request) const =0
makeUrl This method prepare a prefix url for http requests.
static QSharedPointer< MessageType > makeMesasge(const QByteArray &data, Args &&...args)
makeMesasge This is factory method tha can create a messages types.
Definition ibot.h:145
void sigStopRequire()
sigStopRequire just custm event for stop bot if tou use services.
virtual bool sendFile(const QByteArray &file, const QString &fileName, const QVariant &chatId)=0
sendFile This method setns a file that saved as a bytearray.
virtual bool sendFile(const QFileInfo &file, const QVariant &chatId)=0
send file .
virtual bool deleteMessage(const QVariant &chatId, const QVariant &messageId)=0
deleteMessage This is main method to delete messages.
virtual bool login(const QByteArray &token)=0
login This method get bae information of the bot from remote server.
virtual QSharedPointer< iFile > getFile(const QString &fileId, iFile::Type fileType=iFile::Type::Ram)=0
Get a file by its ID.
void sigReceiveUpdate(const QSharedPointer< iUpdate > &)
sigReceiveUpdate emit when but receive any updates from users.
virtual bool sendMessage(const QVariant &chatId, const QString &text)=0
sendMessage This method sents text to the selected chat.
static QSharedPointer< MessageType > makeMesasge(const QJsonObject &data, Args &&...args)
makeMesasge This is factory method tha can create a messages types.
Definition ibot.h:159
Type
The Type enum is type of the file object.
Definition ifile.h:28
#define QTBOT_EXPORT
Definition global.h:18