qTbot 0.2.102.bb22a69
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 "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
82}
83
84void TelegramRestBot::handleReceiveUpdates(const QByteArray &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());
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 factory method tha can create a messages types.
Definition ibot.h:216
const QByteArray & token() const
token This is token value for authication on the remote server (bot)
Definition ibot.cpp:34
void incomeNewUpdate(const QSharedPointer< iUpdate > &message)
incomeNewUpdate This method save incomed messages into store.
Definition ibot.cpp:44
QFuture< QByteArray > sendRequest(const QSharedPointer< iRequest > &rquest)
sendRequest This method sent custom requests to the server.
Definition ibot.cpp:131
virtual void setProcessed(const QSet< unsigned long long > &newProcessed)
setProcessed This method sets new list of processed mesages.
Definition ibot.cpp:291
virtual void logout()
login This method remove login token of bot.
Definition ibot.cpp:30
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...