qTbot 0.87.9547b0c
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-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
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;
24class TelegramUpdateAnswer;
25class TelegramSendFile;
26class TelegramSingleRquest;
27
28typedef std::function<void(const QString& buttonKey, const QVariant& msgID)> ButtonCB;
29typedef QList<QHash<QString, ButtonCB >> KeyboardOnMessage;
30
35{
36 Q_OBJECT
37public:
40
41 bool login(const QByteArray &token) override;
42
43 bool sendMessage(const QVariant &chatId, const QString& text) override;
44
52 bool sendLocationRequest(const QVariant &chatId, const QString& text, const QString &buttonText, bool onetimeKeyboard);
53
61 bool sendSelfContactRequest(const QVariant &chatId, const QString& text, const QString &buttonText, bool onetimeKeyboard);
62
75 bool sendSpecificMessage(const TelegramArgs& args,
76 const qTbot::ExtraJsonObjects &extraObjects = {});
77
89 bool sendSpecificMessageWithKeyboard(const TelegramArgs& args,
90 const KeyboardOnMessage &keyboard);
91
107 bool sendSpecificMessageWithKeyboard(const TelegramArgs& args,
108 const QList<QList<QString> > &keyboard,
109 bool onTimeKeyboard = false,
110 bool autoResizeKeyboard = true);
111
112 bool deleteMessage(const QVariant& chatId, const QVariant& messageId) override;
113
129 bool editSpecificMessageWithKeyboard(const QVariant &messageId,
130 const TelegramArgs& args,
131 const QList<QList<QString> > &keyboard = {},
132 bool onTimeKeyboard = false,
133 bool autoResizeKeyboard = false);
134
135
150 bool editSpecificMessageWithKeyboard(const QVariant& messageId,
151 const TelegramArgs& args,
152 const KeyboardOnMessage &keyboard = {});
153
170 bool editMessageKeyboard(const QVariant& messageId,
171 const QVariant &chatId,
172 const KeyboardOnMessage &keyboard = {},
173 const QString &callBackQueryId = "");
174
187 bool editSpecificMessage(const QVariant &messageId,
188 const TelegramArgs& args);
189
190 [[nodiscard("do not forget to save shared pointer of file handler, because it's will not save inner bot object.")]]
191 QSharedPointer<iFile> getFile(const QString& fileId, iFile::Type fileType = iFile::Type::Ram) override;
192
199 QSharedPointer<QNetworkReply> getFileMeta(const QString& fileId,
200 const QWeakPointer<iFile> &receiver = {nullptr});
201
202 bool sendFile( const QFileInfo& file, const QVariant& chatId) override;
203
204 bool sendFile( const QByteArray& file, const QString& fileName, const QVariant& chatId) override;
205
212 bool sendFileMessage( const TelegramArgs& args, const QFileInfo& file);
213
221 bool sendFileMessage( const TelegramArgs& args, const QByteArray& file, const QString& fileName);
222
231 bool sendPhoto(const TelegramArgs& args,
232 const QFileInfo& photo,
233 const KeyboardOnMessage &keyboard = {});
234
244 bool sendPhoto(const TelegramArgs &args,
245 const QByteArray& photo,
246 const QString& fileName,
247 const KeyboardOnMessage &keyboard = {});
248
255 bool sendFileById(const QString& fileID, const QVariant& chatId);
256
266 bool sendLocation(const TelegramArgs &args,
267 float latitude,
268 float longitude,
269 const KeyboardOnMessage &keyboard = {});
270
279 bool sendContact(const TelegramArgs &args,
280 const QString& phone,
281 const QString& firstName,
282 const QString& secondName = "");
283
284 // to do
285
286 // * forwardMessage implementations
287 // * copyMessage implementations
288 // * sendAudio implementations
289 // * sendVideo implementations
290 // * sendVoice implementations
291 // * sendContact implementations
292 // * sendPoll implementations
293 // * sendDice implementations
294 // * sendChatAction implementations
295 // * getUserProfilePhotos implementations
296 // * banChatMember implementations
297 // * unbanChatMember implementations
298 // * restrictChatMember implementations
299 // * setChatAdministratorCustomTitle implementations
300 // * banChatSenderChat implementations
301 // * unbanChatSenderChat implementations
302 // * exportChatInviteLink implementations
303 // * approveChatJoinRequest implementations
304 // * declineChatJoinRequest implementations
305 // * setChatTitle implementations
306 // * pinChatMessage implementations
307 // * unpinChatMessage implementations
308 // * unpinAllChatMessages implementations
309 // * leaveChat implementations
310 // * getChat implementations
311 // * getChatAdministrators implementations
312 // * getChatMemberCount implementations
313 // * getChatMember implementations
314 // * answerCallbackQuery implementations
315 // * getMyCommands implementations
316 // * deleteMyCommands implementations
317 // * getMyCommands implementations
318 // * editMessageText implementations
319 // * editMessageReplyMarkup implementations
320 // * stopPoll implementations
321 // * deleteMessage implementations
322 // * sendSticker implementations
323
324
329 unsigned long long id() const;
330
335 const QString& username() const;
336
342 int gelLastMessageId(unsigned long long &chatId) const;
343
344protected:
345
350 void setId(unsigned long long newId);
351
356 void setUsername(const QString &newUsername);
357
358 QString makeUrl(const QSharedPointer<iRequest>& request) const override;
359
365 int getFileSizeByUniqueId(const QString& id) const;
366
372 QSharedPointer<TelegramFile> getFileInfoByUniqueId(const QString& id) const;
373
378 virtual void onRequestError(const QSharedPointer<TelegramUpdateAnswer>& ansverWithError) const;
379
380 void handleIncomeNewUpdate(const QSharedPointer<iUpdate> &) override;
381
388 virtual bool sendMessageRequest(const QSharedPointer<iRequest> &rquest,
389 const std::function<void(int msgId)>& msgIdCB = {});
390
391private slots:
392 void handleLogin();
393 void handleLoginErr(QNetworkReply::NetworkError err);
394 void handleFileHeader(const QWeakPointer<QNetworkReply>& sender,
395 const QWeakPointer<iFile> &receiver);
396
397private:
398
399 QString findFileInlocatStorage(const QString& fileId) const;
400 QHash<QString, QSharedPointer<QJsonObject> >
401 prepareInlineKeyBoard(const KeyboardOnMessage &keyboard);
402 ExtraJsonObjects prepareKeyboard(bool autoResizeKeyboard,
403 bool onTimeKeyboard,
404 const QList<QList<QString> > &keyboard);
405
406 unsigned long long _id = 0;
407 QString _username;
408 QSharedPointer<QNetworkReply> _loginReplay;
409 QMap<QString, std::function<void(const QString&, const QVariant&)>> _handleButtons;
410
411 QHash<unsigned long long, int> _lastMessageId;
412
413 QHash<QString, QSharedPointer<TelegramFile>> _filesMetaInfo;
414
415
416};
417}
418#endif // ITELEGRAMBOT_H
The IBot class Base interface for all chat-bots objcts.
Definition ibot.h:35
The ITelegramBot class This is base implementation of the all telegramm bots.
Type
The Type enum is type of the file object.
Definition ifile.h:28
#define QTBOT_EXPORT
Definition global.h:18
std::function< void(const QString &buttonKey, const QVariant &msgID)> ButtonCB
QHash< QString, QSharedPointer< QJsonObject > > ExtraJsonObjects
ExtraJsonObjects hash map of the extra objects of the message.
Definition irequest.h:26
QList< QHash< QString, ButtonCB > > KeyboardOnMessage
The TelegramArgs class is base structure for the all tellegram message arguments.