qTbot 0.2.123.6f57966
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-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#include "httpexception.h"
9#include "telegramrestbot.h"
13
14#include <QJsonArray>
15#include <QTimer>
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 replay.then([this](const QByteArray &result){
55 handleReceiveUpdates(result);
56 }).onFailed([this](const HttpException &e){
57 handleReceiveUpdatesErr(e.code());
58
59 } );
60
61 return;
62 } else {
63 QTimer::singleShot( _updateDelay - delta, this, [this](){startUpdates();});
64 }
65}
66
68 return _updateDelay;
69}
70
71void TelegramRestBot::setUpdateDelay(int newUpdateDelay) {
72 _updateDelay = newUpdateDelay;
73}
74
75void TelegramRestBot::setProcessed(const QSet<unsigned long long> &newProcessed) {
76 auto&& it = std::min_element(newProcessed.begin(), newProcessed.end());
77 if (it != newProcessed.end()) {
78 _lanstUpdateid = *it;
79 }
80
81 IBot::setProcessed(newProcessed);
82}
83
84void TelegramRestBot::handleReceiveUpdates(const QByteArray &replay) {
85 auto&& telegramMsg = makeMesasge<TelegramUpdateAnswer>(replay);
86 if (telegramMsg->isValid()) {
87
88 _lanstUpdateTime = QDateTime::currentMSecsSinceEpoch();
89
90 auto && resultArray = telegramMsg->result().toArray();
91 for (const auto& ref: resultArray) {
92 auto&& update = IBot::makeMesasge<TelegramUpdate>(ref.toObject());
93 incomeNewUpdate(update);
94 if (_lanstUpdateid < update->updateId()) {
95 _lanstUpdateid = update->updateId();
96 };
97 }
98 }
99
100 startUpdates();
101}
102
103void TelegramRestBot::handleReceiveUpdatesErr(QNetworkReply::NetworkError err) {
104 if (err) {
105 qDebug() << "Network error occured. code: " << err;
106 }
107
108 startUpdates();
109}
110
111
112}
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
const QByteArray & token() const
token This is the token value for authentication on the remote server (bot).
Definition ibot.cpp:36
void incomeNewUpdate(const QSharedPointer< iUpdate > &message)
incomeNewUpdate This method saves incoming messages into the store.
Definition ibot.cpp:46
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 void logout()
logout This method removes the login token of the bot.
Definition ibot.cpp:32
bool login(const QByteArray &token) override
login This method gets basic information of the bot from the remote server.
void logout() override
logout This method stops the long polling of the updates and removes the login token of the bot.
void setUpdateDelay(int newUpdateDelay)
setUpdateDelay This method sets a new value for the TelegramRestBot::updateDelay property.
void setProcessed(const QSet< unsigned long long > &newProcessed) override
setProcessed This method sets a new list of processed updates. The bot will start to receive the upda...
bool login(const QByteArray &token) override
login This method gets basic information of the bot from the remote server and starts the long pollin...
int updateDelay() const
updateDelay This is the interval "how many msecs the bot will wait to send the next request for updat...
TelegramRestBot()
TelegramRestBot default constructor.
~TelegramRestBot()
~TelegramRestBot destructor. Stops the update polling.