qTbot 0.89.ee6949a
qTbot is base back end library for your c++ Qt projects.
telegramrestbot.cpp
Go to the documentation of this file.
1//#
2//# Copyright (C) 2023-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#include "telegramrestbot.h"
12
13#include <QJsonArray>
14#include <QTimer>
16#include <limits>
17
18namespace qTbot {
19
22
25
26bool TelegramRestBot::login(const QByteArray &token) {
28 return false;
29 }
30
31 _lanstUpdateTime = QDateTime::currentMSecsSinceEpoch();
32 _run = true;
33
34 startUpdates();
35
36 return true;
37}
38
40 _run = false;
42}
43
44void TelegramRestBot::startUpdates() {
45 if (!_run)
46 return;
47
48 long long delta = QDateTime::currentMSecsSinceEpoch() - _lanstUpdateTime;
49
50
51 if (delta >= _updateDelay) {
52 auto&& replay = sendRequest(QSharedPointer<TelegramGetUpdate>::create(_lanstUpdateid + 1));
53
54 connect(replay.get(), &QNetworkReply::finished,
55 this, std::bind(&TelegramRestBot::handleReceiveUpdates, this, replay.toWeakRef()),
56 Qt::DirectConnection);
57
58 connect(replay.get(), &QNetworkReply::errorOccurred,
59 this, &TelegramRestBot::handleReceiveUpdatesErr,
60 Qt::DirectConnection);
61
62 return;
63 } else {
64 QTimer::singleShot( _updateDelay - delta, this, [this](){startUpdates();});
65 }
66}
67
69 return _updateDelay;
70}
71
72void TelegramRestBot::setUpdateDelay(int newUpdateDelay) {
73 _updateDelay = newUpdateDelay;
74}
75
76void TelegramRestBot::setProcessed(const QSet<unsigned long long> &newProcessed) {
77 auto&& it = std::min_element(newProcessed.begin(), newProcessed.end());
78 if (it != newProcessed.end()) {
79 _lanstUpdateid = *it;
80 }
81
83}
84
85void TelegramRestBot::handleReceiveUpdates(const QWeakPointer<QNetworkReply> &replay) {
86
87 if (auto&& sharedReplay = replay.lock()) {
89 if (telegramMsg->isValid()) {
90
91 _lanstUpdateTime = QDateTime::currentMSecsSinceEpoch();
92
93 auto && resultArray = telegramMsg->result().toArray();
94 for (const auto& ref: resultArray) {
95 auto&& update = IBot::makeMesasge<TelegramUpdate>(ref.toObject());
97 if (_lanstUpdateid < update->updateId()) {
98 _lanstUpdateid = update->updateId();
99 };
100 }
101 }
102 }
103
104 startUpdates();
105}
106
107void TelegramRestBot::handleReceiveUpdatesErr(QNetworkReply::NetworkError err) {
108 if (err) {
109 qDebug() << "Network error occured. code: " << err;
110 }
111
112 startUpdates();
113}
114
115
116}
static QSharedPointer< MessageType > makeMesasge(const QByteArray &data, Args &&...args)
makeMesasge This is factory method tha can create a messages types.
Definition ibot.h:145
const QByteArray & token() const
token This is token value for authication on the remote server (bot)
Definition ibot.cpp:28
void incomeNewUpdate(const QSharedPointer< iUpdate > &message)
incomeNewUpdate This method save incomed messages into store.
Definition ibot.cpp:36
virtual void setProcessed(const QSet< unsigned long long > &newProcessed)
setProcessed This method sets new list of processed mesages.
Definition ibot.cpp:154
virtual void logout()
login This method remove login token of bot.
Definition ibot.cpp:24
QSharedPointer< QNetworkReply > sendRequest(const QSharedPointer< iRequest > &rquest)
sendRequest This method sent custom requests to the server.
Definition ibot.cpp:52
bool login(const QByteArray &token) override
login This method get bae information of the bot from remote server.
void logout() override
login This method remove login token of bot.
void setUpdateDelay(int newUpdateDelay)
setUpdateDelay This method sets new value for the TelegramRestBot::updateDelay property.
void setProcessed(const QSet< unsigned long long > &newProcessed) override
setProcessed This method sets new list of processed mesages.
bool login(const QByteArray &token) override
login This method get bae information of the bot from remote server.
int updateDelay() const
updateDelay This is interval "how many msec bot will be wait for sent next request of updates" By def...