qTbot 0.2.123.6f57966
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-2026 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:
41 IBot();
42
46 ~IBot();
47
51 enum FileType {
56
63 };
64
69 struct RequestData {
73 QSharedPointer<iRequest> request;
74
78 QString responceFilePath = "";
79
83 QSharedPointer<QPromise<QByteArray>> responce;
84 };
85
91 virtual bool login(const QByteArray& token) = 0;
92
96 virtual void logout();
97
107 virtual bool sendMessage(const QVariant& chatId, const QString& text, iRequest::RequestPriority priority = iRequest::NormalPriority) = 0;
108
115 virtual bool deleteMessage(const QVariant& chatId, const QVariant& messageId) = 0;
116
126 virtual QFuture<QByteArray> getFile(const QString& fileId, FileType fileType = Ram) = 0;
127
135 virtual bool sendFile( const QFileInfo& file, const QVariant& chatId) = 0;
136
144 virtual bool sendFile( const QByteArray& file, const QString& fileName, const QVariant& chatId) = 0;
145
150 const QByteArray &token() const;
151
156 const QString& name() const;
157
162 void setName(const QString &newName);
163
168 QSharedPointer<iUpdate> takeNextUnreadUpdate();
169
174 QSet<unsigned long long> processed() const;
175
180 virtual void setProcessed(const QSet<unsigned long long> &newProcessed);
181
186 int reqestLimitPerSecond() const;
187
192 void setReqestLimitPerSecond(int newReqestLimitPerSecond);
193
199
204 void setParallelActiveNetworkThreads(int newParallelActiveNetworkThreads);
205
211 unsigned long long totalSentRequests() const;
212
217 QDateTime startTime() const;
218
219protected:
220
227 template<class MessageType, class ... Args>
228 static QSharedPointer<MessageType> makeMesasge(const QByteArray& data, Args&& ...args) {
229 auto&& ptr = QSharedPointer<MessageType>(new MessageType(std::forward<Args>(args)...));
230 ptr->setRawData(data);
231
232 return ptr;
233 }
234
241 template<class MessageType, class ... Args>
242 static QSharedPointer<MessageType> makeMesasge(const QJsonObject& data, Args&& ...args) {
243 auto&& ptr = QSharedPointer<MessageType>(new MessageType(std::forward<Args>(args)...));
244 ptr->setRawJson(data);
245
246 return ptr;
247 }
248
254 virtual QString makeUrl(const QSharedPointer<iRequest>& request) const = 0;
255
261 QFuture<QByteArray>
262 sendRequest(const QSharedPointer<iRequest>& rquest);
263
272 QFuture<QByteArray>
273 sendRequest(const QSharedPointer<iRequest>& rquest, const QString& pathToResult);
274
279 void setToken(const QByteArray &newToken);
280
285 void incomeNewUpdate(const QSharedPointer<iUpdate>& message);
286
291 void markUpdateAsProcessed(const QSharedPointer<iUpdate>& message);
292
298 void markUpdateAsUnprocessed(const QSharedPointer<iUpdate>& message);
299
305 void markUpdateAsUnprocessed(unsigned long long messageID);
306
311 virtual QString defaultFileStorageLocation() const;
312
317 virtual void handleIncomeNewUpdate(const QSharedPointer<iUpdate>& );
318signals:
322 void sigReceiveUpdate(const QSharedPointer<qTbot::iUpdate>& );
323
328
329private slots:
330 void handleEcxecuteRequest();
331private:
332 unsigned long long makeKey(iRequest::RequestPriority priority);
333 void setCurrentParallelActiveNetworkThreads(int newParallelActiveNetworkThreads);
334
335 void sendRequestPrivate(const QSharedPointer<iRequest>& rquest,
336 const QSharedPointer<QPromise<QByteArray>> & promiseResult);
337
338 void sendRequestPrivate(const QSharedPointer<iRequest>& rquest,
339 const QString& pathToResult,
340 const QSharedPointer<QPromise<QByteArray> > &promiseResult);
341
342 QNetworkReply *sendRquestImpl(const QSharedPointer<iRequest> &rquest);
343
344 QByteArray _token;
345 QString _name;
346 QMap<unsigned long long, QSharedPointer<iUpdate>> _notProcessedUpdates;
347 QSet<unsigned long long> _processed;
348 QNetworkAccessManager *_manager = nullptr;
349 QTimer* _requestExecutor = nullptr;
350 unsigned long long _totalRequest = 0;
351 QDateTime _startTime;
352 QMap<unsigned long long, RequestData> _requestQueue;
353 int _currentParallelActiveNetworkThreads = 0;
354 int _parallelActiveNetworkThreads = 5;
355
356 QRecursiveMutex _mutex;
357
358
359
360};
361
362}
363#endif // IBOT_H
virtual QString makeUrl(const QSharedPointer< iRequest > &request) const =0
makeUrl This method prepares a prefix url for http requests.
void setName(const QString &newName)
setName This method sets new value for the IBot::name field.
Definition ibot.cpp:343
static QSharedPointer< MessageType > makeMesasge(const QByteArray &data, Args &&...args)
makeMesasge This is a factory method that can create a message type from raw byte data.
Definition ibot.h:228
void sigReceiveUpdate(const QSharedPointer< qTbot::iUpdate > &)
sigReceiveUpdate emitted when the bot receives any updates from users.
const QString & name() const
name This is the name of the bot. Usually this field is received from the server after authentication...
Definition ibot.cpp:339
void sigStopRequire()
sigStopRequire just a custom event to stop the bot if you use services.
FileType
The FileType enum is a list of file types. It defines how a file should be downloaded - as a local ob...
Definition ibot.h:51
@ Local
Local file type. The file will be saved in the internal file storage. This file type can use the file...
Definition ibot.h:62
@ Ram
Ram virtual file type. All file data will be saved into a QFuture bytes array.
Definition ibot.h:55
unsigned long long totalSentRequests() const
totalSentRequests This is the total count of prepared requests of the bot from the start.
Definition ibot.cpp:107
virtual bool sendFile(const QByteArray &file, const QString &fileName, const QVariant &chatId)=0
sendFile This method sends a file that is saved as a byte array.
void markUpdateAsUnprocessed(const QSharedPointer< iUpdate > &message)
markMessageAsUnprocessed This method adds the message into the not processed messages store.
Definition ibot.cpp:195
void markUpdateAsProcessed(const QSharedPointer< iUpdate > &message)
markMessageAsProcessed This method removes the message from the not processed messages store.
Definition ibot.cpp:191
const QByteArray & token() const
token This is the token value for authentication on the remote server (bot).
Definition ibot.cpp:36
int parallelActiveNetworkThreads() const
parallelActiveNetworkThreads This method returns the number of parallel active network threads.
Definition ibot.cpp:111
virtual bool sendFile(const QFileInfo &file, const QVariant &chatId)=0
sendFile This method sends the selected file to the chat.
virtual void handleIncomeNewUpdate(const QSharedPointer< iUpdate > &)
handleIncomeNewUpdate This method just emits the sigReceiveUpdate signal.
Definition ibot.cpp:207
virtual bool sendMessage(const QVariant &chatId, const QString &text, iRequest::RequestPriority priority=iRequest::NormalPriority)=0
sendMessage This method sends text to the selected chat.
int reqestLimitPerSecond() const
reqestLimitPerSecond This is the request performance limitation. By default it is 20 requests per sec...
Definition ibot.cpp:140
void setReqestLimitPerSecond(int newReqestLimitPerSecond)
setReqestLimitPerSecond This method sets a new limitation of the bot performance.
Definition ibot.cpp:144
virtual QString defaultFileStorageLocation() const
defaultFileStorageLocation This method returns the default file storage location.
Definition ibot.cpp:203
void incomeNewUpdate(const QSharedPointer< iUpdate > &message)
incomeNewUpdate This method saves incoming messages into the store.
Definition ibot.cpp:46
virtual bool deleteMessage(const QVariant &chatId, const QVariant &messageId)=0
deleteMessage This is the main method to delete messages.
void setParallelActiveNetworkThreads(int newParallelActiveNetworkThreads)
setParallelActiveNetworkThreads This method sets the number of parallel active network threads.
Definition ibot.cpp:115
QSet< unsigned long long > processed() const
processed This method returns the list of processed messages.
Definition ibot.cpp:331
QFuture< QByteArray > sendRequest(const QSharedPointer< iRequest > &rquest)
sendRequest This method sends a custom request to the server.
Definition ibot.cpp:149
virtual void setProcessed(const QSet< unsigned long long > &newProcessed)
setProcessed This method sets a new list of processed messages.
Definition ibot.cpp:335
virtual QFuture< QByteArray > getFile(const QString &fileId, FileType fileType=Ram)=0
Get a file by its ID.
void setToken(const QByteArray &newToken)
setToken This is the setter of the IBot::token value.
Definition ibot.cpp:40
virtual void logout()
logout This method removes the login token of the bot.
Definition ibot.cpp:32
virtual bool login(const QByteArray &token)=0
login This method gets basic information of the bot from the remote server.
static QSharedPointer< MessageType > makeMesasge(const QJsonObject &data, Args &&...args)
makeMesasge This is a factory method that can create a message type from raw json data.
Definition ibot.h:242
QDateTime startTime() const
startTime This is the time when the bot was started.
Definition ibot.cpp:103
IBot()
IBot default constructor. Initializes the network access manager and the request executor.
Definition ibot.cpp:17
QSharedPointer< iUpdate > takeNextUnreadUpdate()
takeNextUnreadUpdate This method takes an unread update and marks it as read.
Definition ibot.cpp:347
RequestPriority
The RequestPriority enum This is the list of request execution priorities on the client side.
Definition irequest.h:61
@ NormalPriority
NormalPriority the request has a normal priority. This is the default value.
Definition irequest.h:73
The RequestData class is a simple wrapper of a request object with the path of the response....
Definition ibot.h:69
QString responceFilePath
responceFilePath path to the response file.
Definition ibot.h:78
QSharedPointer< QPromise< QByteArray > > responce
responce this is a promise of the response of this request that will be set back.
Definition ibot.h:83
QSharedPointer< iRequest > request
request the saved request object.
Definition ibot.h:73