qTbot 0.2.123.6f57966
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
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 sigReceiveUpdate(const QSharedPointer< qTbot::iUpdate > &)
sigReceiveUpdate emitted when the bot receives any updates from users.
@ Local
Local file type. The file will be saved in the internal file storage. This file type can use the file...
Definition ibot.h:62
void setReqestLimitPerSecond(int newReqestLimitPerSecond)
setReqestLimitPerSecond This method sets a new limitation of the bot performance.
Definition ibot.cpp:144
QSharedPointer< iUpdate > takeNextUnreadUpdate()
takeNextUnreadUpdate This method takes an unread update and marks it as read.
Definition ibot.cpp:347
QFuture< QByteArray > getFile(const QString &fileId, FileType fileType=FileType::Ram) override
getFile This method sends a request to get a file by id. The files can be saved into local storage if...
bool sendSpecificMessageWithKeyboard(const TelegramArgs &args, const KeyboardOnMessage &keyboard)
Sends a specific message with a custom keyboard to a chat. This function sends a specific message to ...
bool sendMessage(const QVariant &chatId, const QString &text, iRequest::RequestPriority priority=iRequest::NormalPriority) override
sendMessage This method sends text to the selected chat.
bool editSpecificMessageWithKeyboard(const QVariant &messageId, const TelegramArgs &args, const QList< QList< QString > > &keyboard={}, bool onTimeKeyboard=false, bool autoResizeKeyboard=false)
Edits a specific message with a custom keyboard in a chat.
The TelegramRestBot class Is the rest implementation base on the getUpdates (long polling) telegram m...
bool login(const QByteArray &token) override
login This method gets basic information of the bot from the remote server and starts the long pollin...
The TelegramUpdate class contains base information about updates from telegram.
@ LowPriority
LowPriority the request has a low priority.
Definition irequest.h:69
int main(int argc, char *argv[])
Definition main.cpp:18
QList< QHash< QString, ButtonCB > > KeyboardOnMessage
KeyboardOnMessage type. A list of rows where each row is a map of button keys to the callback functio...
The TelegramArgs class is the base structure for all telegram message arguments.