12#include "qTbot/global.h"
22#include <QNetworkReply>
24#include <QSharedPointer>
34class QTBOT_EXPORT
IBot:
public QObject
115 virtual bool deleteMessage(
const QVariant& chatId,
const QVariant& messageId) = 0;
135 virtual bool sendFile(
const QFileInfo& file,
const QVariant& chatId) = 0;
144 virtual bool sendFile(
const QByteArray& file,
const QString& fileName,
const QVariant& chatId) = 0;
150 const QByteArray &
token()
const;
156 const QString&
name()
const;
162 void setName(
const QString &newName);
174 QSet<unsigned long long>
processed()
const;
180 virtual void setProcessed(
const QSet<unsigned long long> &newProcessed);
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);
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);
254 virtual QString
makeUrl(
const QSharedPointer<iRequest>& request)
const = 0;
262 sendRequest(
const QSharedPointer<iRequest>& rquest);
273 sendRequest(
const QSharedPointer<iRequest>& rquest,
const QString& pathToResult);
279 void setToken(
const QByteArray &newToken);
330 void handleEcxecuteRequest();
333 void setCurrentParallelActiveNetworkThreads(
int newParallelActiveNetworkThreads);
335 void sendRequestPrivate(
const QSharedPointer<iRequest>& rquest,
336 const QSharedPointer<QPromise<QByteArray>> & promiseResult);
338 void sendRequestPrivate(
const QSharedPointer<iRequest>& rquest,
339 const QString& pathToResult,
340 const QSharedPointer<QPromise<QByteArray> > &promiseResult);
342 QNetworkReply *sendRquestImpl(
const QSharedPointer<iRequest> &rquest);
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;
356 QRecursiveMutex _mutex;
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.
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.
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...
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...
@ Local
Local file type. The file will be saved in the internal file storage. This file type can use the file...
@ Ram
Ram virtual file type. All file data will be saved into a QFuture bytes array.
unsigned long long totalSentRequests() const
totalSentRequests This is the total count of prepared requests of the bot from the start.
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.
void markUpdateAsProcessed(const QSharedPointer< iUpdate > &message)
markMessageAsProcessed This method removes the message from the not processed messages store.
const QByteArray & token() const
token This is the token value for authentication on the remote server (bot).
int parallelActiveNetworkThreads() const
parallelActiveNetworkThreads This method returns the number of parallel active network threads.
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.
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...
void setReqestLimitPerSecond(int newReqestLimitPerSecond)
setReqestLimitPerSecond This method sets a new limitation of the bot performance.
virtual QString defaultFileStorageLocation() const
defaultFileStorageLocation This method returns the default file storage location.
void incomeNewUpdate(const QSharedPointer< iUpdate > &message)
incomeNewUpdate This method saves incoming messages into the store.
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.
QSet< unsigned long long > processed() const
processed This method returns the list of processed messages.
QFuture< QByteArray > sendRequest(const QSharedPointer< iRequest > &rquest)
sendRequest This method sends a custom request to the server.
virtual void setProcessed(const QSet< unsigned long long > &newProcessed)
setProcessed This method sets a new list of processed messages.
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.
virtual void logout()
logout This method removes the login token of the bot.
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.
QDateTime startTime() const
startTime This is the time when the bot was started.
IBot()
IBot default constructor. Initializes the network access manager and the request executor.
QSharedPointer< iUpdate > takeNextUnreadUpdate()
takeNextUnreadUpdate This method takes an unread update and marks it as read.
RequestPriority
The RequestPriority enum This is the list of request execution priorities on the client side.
@ NormalPriority
NormalPriority the request has a normal priority. This is the default value.
The RequestData class is a simple wrapper of a request object with the path of the response....
QString responceFilePath
responceFilePath path to the response file.
QSharedPointer< QPromise< QByteArray > > responce
responce this is a promise of the response of this request that will be set back.
QSharedPointer< iRequest > request
request the saved request object.