qTbot 0.2.107.d8fc923
qTbot is base back end library for your c++ Qt projects.
main.cpp
Go to the documentation of this file.
1//#
2//# Copyright (C) 2021-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 "qvariant.h"
10
11#include <QCoreApplication>
12#include <qTbot/httpexception.h>
15
16// link to test bot
17// @quasarapp_test_bot (https://t.me/quasarapp_test_bot)
18int main(int argc, char *argv[]) {
19
20 QCoreApplication::setOrganizationName("QuasarApp");
21 QCoreApplication::setOrganizationDomain("https://github.com/QuasarApp");
22 QCoreApplication::setApplicationName("qTbotExample");
23
24 QCoreApplication app(argc, argv);
25
28
29 srand(time(0));
30
31 QObject::connect(&bot, &qTbot::TelegramRestBot::sigReceiveUpdate, [&bot](auto){
32 while(auto&& update = bot.takeNextUnreadUpdate()) {
33
34 if (auto&& tupdate = update.dynamicCast<qTbot::TelegramUpdate>()) {
35
36 if (tupdate->contains(tupdate->MessageUpdate)) {
37
38 if (auto&& tmsg = tupdate->message()) {
39 if (tmsg->contains(tmsg->Document)) {
40 bot.getFile(tmsg->documents()->fileId(), qTbot::ITelegramBot::Local).then([](const QByteArray& path){
41 qInfo() << "file save into " << path;
42 }).onFailed([](const std::exception& exception){
43
44 qCritical() << "exception :" << exception.what();
45 });
46 }
47
48 if (tmsg->contains(tmsg->Image)) {
49 bot.getFile(tmsg->image()->fileId(), qTbot::ITelegramBot::Local).then([](const QByteArray& path){
50 qInfo() << "file save into " << path;
51 }).onFailed([](const std::exception& exception){
52
53 qCritical() << "exception :" << exception.what();
54 });;
55 }
56
57 if (tmsg->contains(tmsg->Audio)) {
58 bot.getFile(tmsg->audio()->fileId(), qTbot::ITelegramBot::Local).then([](const QByteArray& path){
59 qInfo() << "file save into " << path;
60 }).onFailed([](const std::exception& exception){
61
62 qCritical() << "exception :" << exception.what();
63 });;
64 }
65
66 if (tmsg->text() == "spam") {
67 for (int i = 0 ; i < 1000; i++) {
68 bot.sendMessage(tmsg->chatId(), QString(" message N %0").arg(i), qTbot::iRequest::LowPriority);
69 }
70 } else {
71 bot.sendSpecificMessageWithKeyboard(qTbot::TelegramArgs{tmsg->chatId(), "I see it", tmsg->messageId()},
72 {{{"test_button", [tmsg, &bot](const QString& queryId, const QVariant& msgId){
73 static int index = 0;
74
75 auto&& args = qTbot::TelegramArgs{tmsg->chatId(),
76 "I see it. Presed count: " + QString::number(index++),
77 tmsg->messageId(),
78 "",
79 false,
80 queryId};
81
82 auto&& keyboard = qTbot::KeyboardOnMessage{
83 {{"test_button", [](auto , auto ){}},
84 {"test_button 2", [](auto , auto ){}}}};
85
86 bot.editSpecificMessageWithKeyboard(msgId,
87 args,
88 keyboard
89 );
90 }}}});
91
92 bot.sendSpecificMessageWithKeyboard(qTbot::TelegramArgs{tmsg->chatId(), "I see it", tmsg->messageId()},
93 {{{"test_button"},
94 {"test_button"},}}, true, true);
95 }
96
97
98 }
99
100 }
101 }
102 }
103 });
104
105 if (!bot.login("6349356184:AAFotw9EC46sgAQrkGQ_jeHPyv3EAapZXcM")) {
106 qCritical() << "failed to login!";
107 return 1;
108 }
109 return app.exec();
110}
void setReqestLimitPerSecond(int newReqestLimitPerSecond)
setReqestLimitPerSecond this method sets new limitation of bot performance.
Definition ibot.cpp:125
void sigReceiveUpdate(const QSharedPointer< iUpdate > &)
sigReceiveUpdate emit when but receive any updates from users.
QSharedPointer< iUpdate > takeNextUnreadUpdate()
takeNextUnreadUpdate This method take a unread update and mark them as read.
Definition ibot.cpp:320
The TelegramRestBot class Is Rest implementation base on the Update API telegram method.
int main(int argc, char *argv[])
Definition main.cpp:18