qTbot 0.2.123.6f57966
qTbot is base back end library for your c++ Qt projects.
itelegrambot.h
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
9
10#ifndef ITELEGRAMBOT_H
11#define ITELEGRAMBOT_H
12
13#include "ibot.h"
14#include "qTbot/telegramargs.h"
15#include <QObject>
16
17class QNetworkAccessManager;
18class QNetworkReply;
19
20namespace qTbot {
21
22class ITelegramMessage;
23class TelegramFile;
27
32typedef std::function<void(const QString& buttonKey, const QVariant& msgID)> ButtonCB;
33
38typedef QList<QHash<QString, ButtonCB >> KeyboardOnMessage;
39
43class QTBOT_EXPORT ITelegramBot : public IBot
44{
45 Q_OBJECT
46public:
51
56
62 bool login(const QByteArray &token) override;
63
71 bool sendMessage(const QVariant &chatId,
72 const QString& text,
74
83 bool sendLocationRequest(const QVariant &chatId, const QString& text, const QString &buttonText, bool onetimeKeyboard);
84
93 bool sendSelfContactRequest(const QVariant &chatId, const QString& text, const QString &buttonText, bool onetimeKeyboard);
94
107 bool sendSpecificMessage(const TelegramArgs& args,
108 const qTbot::ExtraJsonObjects &extraObjects = {});
109
122 const KeyboardOnMessage &keyboard);
123
140 const QList<QList<QString> > &keyboard,
141 bool onTimeKeyboard = false,
142 bool autoResizeKeyboard = true);
143
150 bool deleteMessage(const QVariant& chatId, const QVariant& messageId) override;
151
167 bool editSpecificMessageWithKeyboard(const QVariant &messageId,
168 const TelegramArgs& args,
169 const QList<QList<QString> > &keyboard = {},
170 bool onTimeKeyboard = false,
171 bool autoResizeKeyboard = false);
172
173
188 bool editSpecificMessageWithKeyboard(const QVariant& messageId,
189 const TelegramArgs& args,
190 const KeyboardOnMessage &keyboard = {});
191
208 bool editMessageKeyboard(const QVariant& messageId,
209 const QVariant &chatId,
210 const KeyboardOnMessage &keyboard = {},
211 const QString &callBackQueryId = "");
212
225 bool editSpecificMessage(const QVariant &messageId,
226 const TelegramArgs& args);
227
234 QFuture<QByteArray> getFile(const QString& fileId, FileType fileType = FileType::Ram) override;
235
241 QFuture<QByteArray> getFileMeta(const QString& fileId);
242
243 bool sendFile( const QFileInfo& file, const QVariant& chatId) override;
244
245 bool sendFile( const QByteArray& file, const QString& fileName, const QVariant& chatId) override;
246
253 bool sendFileMessage( const TelegramArgs& args, const QFileInfo& file);
254
262 bool sendFileMessage( const TelegramArgs& args, const QByteArray& file, const QString& fileName);
263
272 bool sendPhoto(const TelegramArgs& args,
273 const QFileInfo& photo,
274 const KeyboardOnMessage &keyboard = {});
275
285 bool sendPhoto(const TelegramArgs &args,
286 const QByteArray& photo,
287 const QString& fileName,
288 const KeyboardOnMessage &keyboard = {});
289
296 bool sendFileById(const QString& fileID, const QVariant& chatId);
297
307 bool sendLocation(const TelegramArgs &args,
308 float latitude,
309 float longitude,
310 const KeyboardOnMessage &keyboard = {});
311
320 bool sendContact(const TelegramArgs &args,
321 const QString& phone,
322 const QString& firstName,
323 const QString& secondName = "");
324
325 // to do
326
327 // * forwardMessage implementations
328 // * copyMessage implementations
329 // * sendAudio implementations
330 // * sendVideo implementations
331 // * sendVoice implementations
332 // * sendContact implementations
333 // * sendPoll implementations
334 // * sendDice implementations
335 // * sendChatAction implementations
336 // * getUserProfilePhotos implementations
337 // * banChatMember implementations
338 // * unbanChatMember implementations
339 // * restrictChatMember implementations
340 // * setChatAdministratorCustomTitle implementations
341 // * banChatSenderChat implementations
342 // * unbanChatSenderChat implementations
343 // * exportChatInviteLink implementations
344 // * approveChatJoinRequest implementations
345 // * declineChatJoinRequest implementations
346 // * setChatTitle implementations
347 // * pinChatMessage implementations
348 // * unpinChatMessage implementations
349 // * unpinAllChatMessages implementations
350 // * leaveChat implementations
351 // * getChat implementations
352 // * getChatAdministrators implementations
353 // * getChatMemberCount implementations
354 // * getChatMember implementations
355 // * answerCallbackQuery implementations
356 // * getMyCommands implementations
357 // * deleteMyCommands implementations
358 // * getMyCommands implementations
359 // * editMessageText implementations
360 // * editMessageReplyMarkup implementations
361 // * stopPoll implementations
362 // * deleteMessage implementations
363 // * sendSticker implementations
364
365
370 unsigned long long id() const;
371
376 const QString& username() const;
377
383 int gelLastMessageId(unsigned long long &chatId) const;
384
385protected:
386
391 void setId(unsigned long long newId);
392
397 void setUsername(const QString &newUsername);
398
399 QString makeUrl(const QSharedPointer<iRequest>& request) const override;
400
406 int getFileSizeByUniqueId(const QString& id) const;
407
413 QSharedPointer<TelegramFile> getFileInfoByUniqueId(const QString& id) const;
414
419 virtual void onRequestError(const QSharedPointer<TelegramUpdateAnswer>& ansverWithError) const;
420
421 void handleIncomeNewUpdate(const QSharedPointer<iUpdate> &) override;
422
429 virtual bool sendMessageRequest(const QSharedPointer<iRequest> &rquest,
430 const std::function<void(int msgId)>& msgIdCB = {});
431
432private slots:
433 void handleLogin(const QByteArray &ansver);
434 void handleLoginErr(QNetworkReply::NetworkError err);
435 void handleFileHeader(const QByteArray &header);
436
437private:
438
439 QString findFileInlocatStorage(const QString& fileId) const;
440 QHash<QString, QSharedPointer<QJsonObject> >
441 prepareInlineKeyBoard(const KeyboardOnMessage &keyboard);
442 ExtraJsonObjects prepareKeyboard(bool autoResizeKeyboard,
443 bool onTimeKeyboard,
444 const QList<QList<QString> > &keyboard);
445
446 unsigned long long _id = 0;
447 QString _username;
448 QMap<QString, std::function<void(const QString&, const QVariant&)>> _handleButtons;
449
450 QHash<unsigned long long, int> _lastMessageId;
451
452 QHash<QString, QSharedPointer<TelegramFile>> _filesMetaInfo;
453
454
455};
456}
457#endif // ITELEGRAMBOT_H
FileType
The FileType enum is a list of file types. It defines how a file should be downloaded - as a local ob...
Definition ibot.h:51
@ Ram
Ram virtual file type. All file data will be saved into a QFuture bytes array.
Definition ibot.h:55
const QByteArray & token() const
token This is the token value for authentication on the remote server (bot).
Definition ibot.cpp:36
IBot()
IBot default constructor. Initializes the network access manager and the request executor.
Definition ibot.cpp:17
bool sendFile(const QFileInfo &file, const QVariant &chatId) override
sendFile This method sends the selected file to the chat.
bool sendFileById(const QString &fileID, const QVariant &chatId)
sendFileById This is specific method of the telegram bot. sends a file by id.
int getFileSizeByUniqueId(const QString &id) const
getFileSizeByUniqueId This method return size of the file by id
bool editMessageKeyboard(const QVariant &messageId, const QVariant &chatId, const KeyboardOnMessage &keyboard={}, const QString &callBackQueryId="")
Edits a keyboard of message in a chat.
const QString & username() const
username This is the bot login
void setId(unsigned long long newId)
setId This method sets new value for the ITelegramBot::id property.
int gelLastMessageId(unsigned long long &chatId) const
gelLastMessageId this method returns last sent message id.
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...
QSharedPointer< TelegramFile > getFileInfoByUniqueId(const QString &id) const
getFileInfoByUniqueId return a local saved meta information about the file.
QFuture< QByteArray > getFileMeta(const QString &fileId)
getFileMeta This method receives the meta information of the file.
bool sendFileMessage(const TelegramArgs &args, const QFileInfo &file)
sendFileMessage This method sends a message with a file.
virtual void onRequestError(const QSharedPointer< TelegramUpdateAnswer > &ansverWithError) const
onRequestError This method invoked when the telegram server sends an error response....
bool editSpecificMessage(const QVariant &messageId, const TelegramArgs &args)
Edits a specific message in a chat.
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 ...
void handleIncomeNewUpdate(const QSharedPointer< iUpdate > &) override
handleIncomeNewUpdate This method just emits the sigReceiveUpdate signal.
ITelegramBot()
ITelegramBot default constructor.
bool sendLocation(const TelegramArgs &args, float latitude, float longitude, const KeyboardOnMessage &keyboard={})
sendLocation This method sends a location to the user.
unsigned long long id() const
id This method returns the bot id number.
bool sendLocationRequest(const QVariant &chatId, const QString &text, const QString &buttonText, bool onetimeKeyboard)
sendLocationRequest This method sends into the chat a button that will automatically send the geo loc...
bool sendSelfContactRequest(const QVariant &chatId, const QString &text, const QString &buttonText, bool onetimeKeyboard)
sendSelfContactRequest This method sends into the chat a button that will automatically send the user...
bool sendMessage(const QVariant &chatId, const QString &text, iRequest::RequestPriority priority=iRequest::NormalPriority) override
sendMessage This method sends text to the selected chat.
virtual bool sendMessageRequest(const QSharedPointer< iRequest > &rquest, const std::function< void(int msgId)> &msgIdCB={})
sendMessageRequest This method invoke when bot will be sends any messages into the chat.
void setUsername(const QString &newUsername)
setUsername This method sets new value for the ITelegramBot::username property.
QString makeUrl(const QSharedPointer< iRequest > &request) const override
makeUrl This method prepares a prefix url for http requests.
bool sendContact(const TelegramArgs &args, const QString &phone, const QString &firstName, const QString &secondName="")
sendContact This method sends a contact data.
bool sendPhoto(const TelegramArgs &args, const QFileInfo &photo, const KeyboardOnMessage &keyboard={})
sendPhoto This method will send image into chat with chatId
bool login(const QByteArray &token) override
login This method gets basic information of the bot from the remote server.
bool sendSpecificMessage(const TelegramArgs &args, const qTbot::ExtraJsonObjects &extraObjects={})
Sends a specific message to a chat.
bool deleteMessage(const QVariant &chatId, const QVariant &messageId) override
deleteMessage This is the main method to delete messages.
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 TelegramFile class is base class for all files.
The TelegramSendFile class is the base class for uploading files to the server.
The TelegramSingleRquest class Is the base class for all single request commands with arguments.
The TelegramUpdateAnswer class This is a base message from the telegram server after an update reques...
RequestPriority
The RequestPriority enum This is the list of request execution priorities on the client side.
Definition irequest.h:61
@ NormalPriority
NormalPriority the request has a normal priority. This is the default value.
Definition irequest.h:73
std::function< void(const QString &buttonKey, const QVariant &msgID)> ButtonCB
ButtonCB callback type for inline keyboard buttons. It is invoked with the button key and the id of t...
QHash< QString, QSharedPointer< QJsonObject > > ExtraJsonObjects
ExtraJsonObjects hash map of the extra objects of the message.
Definition irequest.h:26
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.