qTbot 0.2.106.03782ba
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 <QMap>
17#include <QHash>
18#include <QSet>
19#include <QFileInfo>
20#include <QTimer>
21
22#include <QNetworkReply>
23#include <QObject>
24#include <QSharedPointer>
25#include <qfuture.h>
26
27namespace qTbot {
28
29class ReplayData;
30
34class QTBOT_EXPORT IBot: public QObject
35{
36 Q_OBJECT
37public:
38 IBot();
39 ~IBot();
40
44 enum FileType {
47
51 Local
52 };
53
58 struct RequestData {
62 QSharedPointer<iRequest> request;
63
67 QString responceFilePath = "";
68
72 QSharedPointer<QPromise<QByteArray>> responce;
73 };
74
80 virtual bool login(const QByteArray& token) = 0;
81
85 virtual void logout();
86
95 virtual bool sendMessage(const QVariant& chatId, const QString& text, iRequest::RequestPriority priority = iRequest::NormalPriority) = 0;
96
103 virtual bool deleteMessage(const QVariant& chatId, const QVariant& messageId) = 0;
104
114 virtual QFuture<QByteArray> getFile(const QString& fileId, FileType fileType = Ram) = 0;
115
123 virtual bool sendFile( const QFileInfo& file, const QVariant& chatId) = 0;
124
132 virtual bool sendFile( const QByteArray& file, const QString& fileName, const QVariant& chatId) = 0;
133
138 const QByteArray &token() const;
139
144 const QString& name() const;
145
150 void setName(const QString &newName);
151
156 QSharedPointer<iUpdate> takeNextUnreadUpdate();
157
162 QSet<unsigned long long> processed() const;
163
168 virtual void setProcessed(const QSet<unsigned long long> &newProcessed);
169
174 int reqestLimitPerSecond() const;
175
180 void setReqestLimitPerSecond(int newReqestLimitPerSecond);
181
186 int parallelActiveNetworkThreads() const;
187
192 void setParallelActiveNetworkThreads(int newParallelActiveNetworkThreads);
193
199 unsigned long long totalSentRequests() const;
200
205 QDateTime startTime() const;
206
207protected:
208
215 template<class MessageType, class ... Args>
216 static QSharedPointer<MessageType> makeMesasge(const QByteArray& data, Args&& ...args) {
217 auto&& ptr = QSharedPointer<MessageType>(new MessageType(std::forward<Args>(args)...));
218 ptr->setRawData(data);
219
220 return ptr;
221 }
222
229 template<class MessageType, class ... Args>
230 static QSharedPointer<MessageType> makeMesasge(const QJsonObject& data, Args&& ...args) {
231 auto&& ptr = QSharedPointer<MessageType>(new MessageType(std::forward<Args>(args)...));
232 ptr->setRawJson(data);
233
234 return ptr;
235 }
236
242 virtual QString makeUrl(const QSharedPointer<iRequest>& request) const = 0;
243
249 QFuture<QByteArray>
250 sendRequest(const QSharedPointer<iRequest>& rquest);
251
259 QFuture<QByteArray>
260 sendRequest(const QSharedPointer<iRequest>& rquest, const QString& pathToResult);
261
266 void setToken(const QByteArray &newToken);
267
271 void incomeNewUpdate(const QSharedPointer<iUpdate>& message);
272
277 void markUpdateAsProcessed(const QSharedPointer<iUpdate>& message);
278
284 void markUpdateAsUnprocessed(const QSharedPointer<iUpdate>& message);
285
291 void markUpdateAsUnprocessed(unsigned long long messageID);
292
297 virtual QString defaultFileStorageLocation() const;
298
303 virtual void handleIncomeNewUpdate(const QSharedPointer<iUpdate>& );
304signals:
308 void sigReceiveUpdate(const QSharedPointer<iUpdate>& );
309
314
315private slots:
316 void handleEcxecuteRequest();
317private:
318 unsigned long long makeKey(iRequest::RequestPriority priority);
319 void setCurrentParallelActiveNetworkThreads(int newParallelActiveNetworkThreads);
320
321 void sendRequestPrivate(const QSharedPointer<iRequest>& rquest,
322 const QSharedPointer<QPromise<QByteArray>> & promiseResult);
323
324 void sendRequestPrivate(const QSharedPointer<iRequest>& rquest,
325 const QString& pathToResult,
326 const QSharedPointer<QPromise<QByteArray> > &promiseResult);
327
328 QNetworkReply *sendRquestImpl(const QSharedPointer<iRequest> &rquest);
329
330 QByteArray _token;
331 QString _name;
332 QMap<unsigned long long, QSharedPointer<iUpdate>> _notProcessedUpdates;
333 QSet<unsigned long long> _processed;
334 QNetworkAccessManager *_manager = nullptr;
335 QTimer* _requestExecutor = nullptr;
336 unsigned long long _totalRequest = 0;
337 QDateTime _startTime;
338 QMap<unsigned long long, RequestData> _requestQueue;
339 int _currentParallelActiveNetworkThreads = 0;
340 int _parallelActiveNetworkThreads = 5;
341
342
343
344};
345
346}
347#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:216
void sigStopRequire()
sigStopRequire just custm event for stop bot if tou use services.
FileType
The FileType enum is is file types, deffine how we should download a file - as a local object in file...
Definition ibot.h:44
@ Ram
The Ram is a Virtual type of download files will save all file data into QFuture bytes array.
Definition ibot.h:46
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 sendMessage(const QVariant &chatId, const QString &text, iRequest::RequestPriority priority=iRequest::NormalPriority)=0
sendMessage This method sents text to the selected chat.
virtual bool deleteMessage(const QVariant &chatId, const QVariant &messageId)=0
deleteMessage This is main method to delete messages.
virtual QFuture< QByteArray > getFile(const QString &fileId, FileType fileType=Ram)=0
Get a file by its ID.
virtual bool login(const QByteArray &token)=0
login This method get bae information of the bot from remote server.
void sigReceiveUpdate(const QSharedPointer< iUpdate > &)
sigReceiveUpdate emit when but receive any updates from users.
static QSharedPointer< MessageType > makeMesasge(const QJsonObject &data, Args &&...args)
makeMesasge This is factory method tha can create a messages types.
Definition ibot.h:230
RequestPriority
The RequestPriority enum.
Definition irequest.h:52
#define QTBOT_EXPORT
Definition global.h:18
The RequestData class is simple wrapper of request object with path of responce. If Path of responce ...
Definition ibot.h:58
QSharedPointer< QPromise< QByteArray > > responce
responce This is promise to responce of this requests that will sets back.
Definition ibot.h:72
QSharedPointer< iRequest > request
request saved request object.
Definition ibot.h:62