Skip to content

Commit

Permalink
Merge pull request #19 from QuasarApp/requestPerformanceLimit
Browse files Browse the repository at this point in the history
Request performance limit
  • Loading branch information
EndrII authored Dec 15, 2024
2 parents ee6949a + bb22a69 commit 24c8b4e
Show file tree
Hide file tree
Showing 26 changed files with 689 additions and 793 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ find_package(Qt6 COMPONENTS Test QUIET)
include(submodules/CMake/QuasarApp.cmake)

updateGitVars()
set(QTBOT_VERSION "0.${GIT_COMMIT_COUNT}.${GIT_COMMIT_HASH}")
set(QTBOT_VERSION "0.2.${GIT_COMMIT_COUNT}.${GIT_COMMIT_HASH}")
set(QTBOT_PACKAGE_ID "quasarapp.core.qTbot")

option(QTBOT_TESTS "This option disables or enables tests of the ${PROJECT_NAME} project" ON)
Expand Down
87 changes: 57 additions & 30 deletions src/example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <qTbot/telegramrestbot.h>

#include <QCoreApplication>
#include <qTbot/httpexception.h>
#include <qTbot/messages/telegrammsg.h>
#include <qTbot/messages/telegramupdate.h>

Expand All @@ -23,11 +24,11 @@ int main(int argc, char *argv[]) {
QCoreApplication app(argc, argv);

qTbot::TelegramRestBot bot;
bot.setReqestLimitPerSecond(10);

srand(time(0));

QList<QSharedPointer<qTbot::iFile> > filesStack;
QObject::connect(&bot, &qTbot::TelegramRestBot::sigReceiveUpdate, [&bot, &filesStack](auto){
QObject::connect(&bot, &qTbot::TelegramRestBot::sigReceiveUpdate, [&bot](auto){
while(auto&& update = bot.takeNextUnreadUpdate()) {

if (auto&& tupdate = update.dynamicCast<qTbot::TelegramUpdate>()) {
Expand All @@ -36,48 +37,74 @@ int main(int argc, char *argv[]) {

if (auto&& tmsg = tupdate->message()) {
if (tmsg->contains(tmsg->Document)) {
filesStack.push_back(bot.getFile(tmsg->documents()->fileId(), qTbot::iFile::Local));
bot.getFile(tmsg->documents()->fileId(), qTbot::ITelegramBot::Local).then([](const QByteArray& path){
qInfo() << "file save into " << path;
}).onFailed([](const std::exception& exception){

qCritical() << "exception :" << exception.what();
});
}

if (tmsg->contains(tmsg->Image)) {
filesStack.push_back(bot.getFile(tmsg->image()->fileId(), qTbot::iFile::Local));
bot.getFile(tmsg->image()->fileId(), qTbot::ITelegramBot::Local).then([](const QByteArray& path){
qInfo() << "file save into " << path;
}).onFailed([](const std::exception& exception){

qCritical() << "exception :" << exception.what();
});;
}

if (tmsg->contains(tmsg->Audio)) {
filesStack.push_back(bot.getFile(tmsg->audio()->fileId(), qTbot::iFile::Local));
bot.getFile(tmsg->audio()->fileId(), qTbot::ITelegramBot::Local).then([](const QByteArray& path){
qInfo() << "file save into " << path;
}).onFailed([](const std::exception& exception){

qCritical() << "exception :" << exception.what();
});;
}

bot.sendSpecificMessageWithKeyboard(qTbot::TelegramArgs{tmsg->chatId(), "I see it", tmsg->messageId()},
{{{"test_button", [tmsg, &bot](const QString& queryId, const QVariant& msgId){
static int index = 0;

auto&& args = qTbot::TelegramArgs{tmsg->chatId(),
"I see it. Presed count: " + QString::number(index++),
tmsg->messageId(),
"",
false,
queryId};

auto&& keyboard = qTbot::KeyboardOnMessage{
{{"test_button", [](auto , auto ){}},
{"test_button 2", [](auto , auto ){}}}};

bot.editSpecificMessageWithKeyboard(msgId,
args,
keyboard
);
}}}});

bot.sendSpecificMessageWithKeyboard(qTbot::TelegramArgs{tmsg->chatId(), "I see it", tmsg->messageId()},
{{{"test_button"},
{"test_button"},}}, true, true);
if (tmsg->text() == "spam") {
for (int i = 0 ; i < 1000; i++) {
bot.sendMessage(tmsg->chatId(), QString(" message N %0").arg(i), qTbot::iRequest::LowPriority);
}
} else {
bot.sendSpecificMessageWithKeyboard(qTbot::TelegramArgs{tmsg->chatId(), "I see it", tmsg->messageId()},
{{{"test_button", [tmsg, &bot](const QString& queryId, const QVariant& msgId){
static int index = 0;

auto&& args = qTbot::TelegramArgs{tmsg->chatId(),
"I see it. Presed count: " + QString::number(index++),
tmsg->messageId(),
"",
false,
queryId};

auto&& keyboard = qTbot::KeyboardOnMessage{
{{"test_button", [](auto , auto ){}},
{"test_button 2", [](auto , auto ){}}}};

bot.editSpecificMessageWithKeyboard(msgId,
args,
keyboard
);
}}}});

bot.sendSpecificMessageWithKeyboard(qTbot::TelegramArgs{tmsg->chatId(), "I see it", tmsg->messageId()},
{{{"test_button"},
{"test_button"},}}, true, true);
}


}

}
}
}
});

bot.login("6349356184:AAFotw9EC46sgAQrkGQ_jeHPyv3EAapZXcM");
if (!bot.login("6349356184:AAFotw9EC46sgAQrkGQ_jeHPyv3EAapZXcM")) {
qCritical() << "failed to login!";
return 1;
}
return app.exec();
}
1 change: 1 addition & 0 deletions src/qTbot/src/private/requests/telegramgetupdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace qTbot {
TelegramGetUpdate::TelegramGetUpdate(unsigned long long offset): TelegramSingleRquest("getUpdates"){
addArg("offset", offset);
addArg("timeout", 30);
setPriority(UngeredPriority);

}
}
1 change: 1 addition & 0 deletions src/qTbot/src/private/requests/telegramsendmsg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ TelegramSendMsg::TelegramSendMsg(const TelegramArgs& generalArgs,
{

QMap<QString, QVariant>&& args = generalArgs.toMap();
setPriority(generalArgs.requestPriority);

for (auto it = extraObjects.begin(); it != extraObjects.end(); it = std::next(it)) {
args[it.key()] = QJsonDocument(*it.value()).toJson(QJsonDocument::Compact);
Expand Down
56 changes: 0 additions & 56 deletions src/qTbot/src/public/qTbot/file.cpp

This file was deleted.

40 changes: 0 additions & 40 deletions src/qTbot/src/public/qTbot/file.h

This file was deleted.

34 changes: 0 additions & 34 deletions src/qTbot/src/public/qTbot/filewaiter.cpp

This file was deleted.

43 changes: 0 additions & 43 deletions src/qTbot/src/public/qTbot/filewaiter.h

This file was deleted.

42 changes: 42 additions & 0 deletions src/qTbot/src/public/qTbot/httpexception.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//#
//# Copyright (C) 2023-2024 QuasarApp.
//# Distributed under the GPLv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#

#include "httpexception.h"

namespace qTbot {

HttpException::HttpException(QNetworkReply::NetworkError code,
const QByteArray &erroString) {

_code = code;

if (erroString.size()) {
_errText = erroString;
} else {

_errText = QByteArray("Http request finished with code: ").
append(QString::number(code).toLatin1());
}
}

const char *HttpException::what() const noexcept {
return _errText.constData();
}

void HttpException::raise() const {
throw *this;
}

QException *HttpException::clone() const {
return new HttpException(QNetworkReply::NetworkError(0),
_errText);
}

QNetworkReply::NetworkError HttpException::code() const {
return _code;
}
}
Loading

0 comments on commit 24c8b4e

Please sign in to comment.