Merge pull request #12 from QuasarApp/edit_keyboard_functions

added method to edit keyboard
This commit is contained in:
Andrei Yankovich 2023-11-19 22:40:07 +01:00 committed by GitHub
commit 7958f23519
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
30 changed files with 654 additions and 472 deletions

View File

@ -12,6 +12,8 @@
#include <qTbot/messages/telegrammsg.h>
#include <qTbot/messages/telegramupdate.h>
// link to test bot
// @quasarapp_test_bot (https://t.me/quasarapp_test_bot)
int main(int argc, char *argv[]) {
QCoreApplication::setOrganizationName("QuasarApp");
@ -44,21 +46,31 @@ int main(int argc, char *argv[]) {
if (tmsg->contains(tmsg->Audio)) {
filesStack.push_back(bot.getFile(tmsg->audio()->fileId(), qTbot::iFile::Local));
}
bot.sendSpecificMessageWithKeyboard(tmsg->chatId(),
"I see it - я вижу это",
{{{"test_button", [tmsg, &bot](const QString& queryId, const QVariant& msgId){
static int index = 0;
bot.editSpecificMessageWithKeyboard(msgId,
tmsg->chatId(),
"I see it - я вижу это. Presedd count: " + QString::number(index++), true, false,
{{{"test_button", [](auto , auto ){}}, {"test_button 2", [](auto , auto ){}}}},
queryId);
}}}}, {}, false, false, tmsg->messageId());
bot.sendSpecificMessageWithKeyboard(tmsg->chatId(),
"I see it - я вижу это (интерактивная клавиатура)",
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, tmsg->messageId());
{"test_button"},}}, true, true);
}
}

View File

@ -10,24 +10,12 @@
namespace qTbot {
TelegramEditMessage::TelegramEditMessage(const QVariant &idEditedMessage,
const QVariant& chatId,
const QString& newText,
bool markdown,
bool disableWebPagePreview,
const QString& callBackQueryId,
const QMap<QString, QSharedPointer<QJsonObject>>& extraObjects):
TelegramSendMsg(chatId,
newText,
extraObjects,
0,
markdown,
callBackQueryId,
disableWebPagePreview) {
setRequest("editMessageText");
addArg("message_id", idEditedMessage);
const TelegramArgs &args,
const ExtraJsonObjects &extraObjects):
TelegramSendMsg(args, extraObjects){
setRequest("editMessageText");
addArg("message_id", idEditedMessage);
}
}
}

View File

@ -18,12 +18,8 @@ class TelegramEditMessage: public TelegramSendMsg
{
public:
TelegramEditMessage(const QVariant& idEditedMessage,
const QVariant& chatId,
const QString& newText,
bool markdown = true,
bool disableWebPagePreview = false,
const QString& callBackQueryId = "",
const QMap<QString, QSharedPointer<QJsonObject>>& extraObjects = {});
const TelegramArgs& args,
const ExtraJsonObjects& extraObjects = {});
};
}
#endif // TELEGRAMEDITMESSAGE_H

View File

@ -0,0 +1,19 @@
//#
//# Copyright (C) 2023-2023 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 "telegrameditmessagereplymarkup.h"
namespace qTbot {
TelegramEditMessageReplyMarkup::TelegramEditMessageReplyMarkup(const QVariant& idEditedMessage,
const TelegramArgs& args,
const ExtraJsonObjects &extraObjects):
TelegramEditMessage(idEditedMessage, args, extraObjects)
{
setRequest("editMessageReplyMarkup");
}
}

View File

@ -0,0 +1,26 @@
//#
//# Copyright (C) 2023-2023 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.
//#
#ifndef TELEGRAMEDITMESSAGEREPLYMARKUP_H
#define TELEGRAMEDITMESSAGEREPLYMARKUP_H
#include "telegrameditmessage.h"
namespace qTbot {
/**
* @brief The TelegramEditMessageReplyMarkup class just edit alredy created Markups.
*/
class TelegramEditMessageReplyMarkup: public TelegramEditMessage
{
public:
TelegramEditMessageReplyMarkup(const QVariant& idEditedMessage,
const TelegramArgs &args,
const ExtraJsonObjects& extraObjects = {});
};
}
#endif // TELEGRAMEDITMESSAGEREPLYMARKUP_H

View File

@ -10,6 +10,9 @@
namespace qTbot {
TelegramGetUpdate::TelegramGetUpdate(): TelegramSingleRquest("getUpdates"){}
TelegramGetUpdate::TelegramGetUpdate(unsigned long long offset): TelegramSingleRquest("getUpdates"){
addArg("offset", offset);
addArg("timeout", 30);
}
}

View File

@ -20,7 +20,11 @@ namespace qTbot {
class TelegramGetUpdate final: public TelegramSingleRquest
{
public:
TelegramGetUpdate();
/**
* @brief TelegramGetUpdate send request for updates. You will get response on immediacy or after 60 seconds if bot does not have any updates.
* @param offset - last accepted update id.
*/
TelegramGetUpdate(unsigned long long offset = 0);
};
}
#endif // TELEGRAMGETUPDATE_H

View File

@ -0,0 +1,25 @@
//#
//# Copyright (C) 2023-2023 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 "telegramsendcontact.h"
namespace qTbot {
TelegramSendContact::TelegramSendContact(const TelegramArgs &args,
const QString &firstName,
const QString &phone,
const QString &lastName):
TelegramSingleRquest("sendContact", args.toMap(true)) {
addArg("first_name", firstName);
addArg("last_name", lastName);
addArg("phone_number", phone);
}
}

View File

@ -0,0 +1,31 @@
//#
//# Copyright (C) 2023-2023 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.
//#
#ifndef TELEGRAMSENDCONTACT_H
#define TELEGRAMSENDCONTACT_H
#include "qTbot/telegramargs.h"
#include "telegramsinglerquest.h"
namespace qTbot {
/**
* @brief The TelegramSendContact class
*/
class TelegramSendContact: public TelegramSingleRquest
{
public:
TelegramSendContact(const TelegramArgs &args,
const QString &firstName,
const QString &phone,
const QString &lastName);
};
}
#endif // TELEGRAMSENDCONTACT_H

View File

@ -8,15 +8,15 @@
namespace qTbot {
TelegramSendDocument::TelegramSendDocument(const QVariant &chatId,
const QString &text,
TelegramSendDocument::TelegramSendDocument(const TelegramArgs &args,
const QString& fileName,
const QByteArray &data):
TelegramSendFile("sendDocument", chatId, text, fileName, TELEGRAM_DOCUMENT, data) {}
const QByteArray &data,
const ExtraJsonObjects &extraObjects):
TelegramSendFile("sendDocument", fileName, TELEGRAM_DOCUMENT, data, args, extraObjects) {}
TelegramSendDocument::TelegramSendDocument(const QVariant &chatId,
const QString &text,
const QFileInfo &file):
TelegramSendFile("sendDocument", chatId, text, file) {}
TelegramSendDocument::TelegramSendDocument(const TelegramArgs &args,
const QFileInfo &file,
const ExtraJsonObjects &extraObjects):
TelegramSendFile("sendDocument", file, args, extraObjects) {}
}

View File

@ -19,14 +19,14 @@ namespace qTbot {
class TelegramSendDocument: public TelegramSendFile
{
public:
TelegramSendDocument(const QVariant &chatId,
const QString &text,
TelegramSendDocument(const TelegramArgs &args,
const QString &fileName,
const QByteArray& data);
const QByteArray& data,
const ExtraJsonObjects& extraObjects = {});
TelegramSendDocument(const QVariant &chatId,
const QString &text,
const QFileInfo &file);
TelegramSendDocument(const TelegramArgs &args,
const QFileInfo &file,
const ExtraJsonObjects& extraObjects = {});
};
}

View File

@ -12,23 +12,14 @@ namespace qTbot {
qTbot::TelegramSendFile::TelegramSendFile(const QString &request,
const QVariant &chatId,
const QString &text,
const QString &fileName,
const QString &fileType,
const QByteArray &data,
unsigned long long replyToMessageId,
const QMap<QString, QSharedPointer<QJsonObject>>& extraObjects
const QByteArray& data,
const TelegramArgs &args,
const ExtraJsonObjects& extraObjects
):
TelegramSingleRquest(request) {
TelegramSingleRquest(request, args.toMap(true)) {
addArg("chat_id", chatId);
if (text.size())
addArg("caption", text);
if (replyToMessageId > 0) {
addArg("reply_to_message_id", replyToMessageId);
}
for (auto it = extraObjects.begin(); it != extraObjects.end(); it = std::next(it)) {
addArg(it.key(), QJsonDocument(*it.value()).toJson(QJsonDocument::Compact));
@ -38,21 +29,10 @@ qTbot::TelegramSendFile::TelegramSendFile(const QString &request,
}
qTbot::TelegramSendFile::TelegramSendFile(const QString &request,
const QVariant &chatId,
const QString &text,
const QFileInfo &file,
unsigned long long replyToMessageId,
const QMap<QString, QSharedPointer<QJsonObject> > &extraObjects):
TelegramSingleRquest(request) {
addArg("chat_id", chatId);
if (text.size())
addArg("text", text);
if (replyToMessageId > 0) {
addArg("reply_to_message_id", replyToMessageId);
}
const TelegramArgs &args,
const QHash<QString, QSharedPointer<QJsonObject> > &extraObjects):
TelegramSingleRquest(request, args.toMap(true)) {
QFile readFile(file.absoluteFilePath());
if (!readFile.open(QIODevice::ReadOnly)) {

View File

@ -10,6 +10,8 @@
#include "qfileinfo.h"
#include "requests/telegramsinglerquest.h"
#include <qTbot/telegramargs.h>
#define TELEGRAM_PHOTO "photo"
#define TELEGRAM_DOCUMENT "document"
@ -23,20 +25,16 @@ class TelegramSendFile: public TelegramSingleRquest
public:
TelegramSendFile(const QString &request,
const QVariant &chatId,
const QString &text,
const QString &fileName,
const QString &fileType,
const QByteArray& data,
unsigned long long replyToMessageId = 0,
const QMap<QString, QSharedPointer<QJsonObject> > &extraObjects = {});
const TelegramArgs &args,
const QHash<QString, QSharedPointer<QJsonObject> > &extraObjects = {});
TelegramSendFile(const QString &request,
const QVariant &chatId,
const QString &text,
const QFileInfo &file,
unsigned long long replyToMessageId = 0,
const QMap<QString, QSharedPointer<QJsonObject> > &extraObjects = {});
const TelegramArgs &args,
const QHash<QString, QSharedPointer<QJsonObject> > &extraObjects = {});
RequestMethod method() const override;

View File

@ -6,25 +6,21 @@
//#
#include "telegramsendlocation.h"
#include <QJsonDocument>
namespace qTbot {
TelegramSendLocation::TelegramSendLocation(const QVariant &chatId,
const QString &text,
TelegramSendLocation::TelegramSendLocation(const TelegramArgs &args,
float latitude,
float longitude,
unsigned long long replyToMessageId
):
TelegramSingleRquest("sendLocation") {
addArg("chat_id", chatId);
if (text.size())
addArg("caption", text);
const ExtraJsonObjects &extraObjects):
TelegramSingleRquest("sendLocation", args.toMap(true)) {
addArg("latitude", latitude);
addArg("longitude", longitude);
if (replyToMessageId) {
addArg("reply_to_message_id", replyToMessageId);
for (auto it = extraObjects.begin(); it != extraObjects.end(); it = std::next(it)) {
addArg(it.key(), QJsonDocument(*it.value()).toJson(QJsonDocument::Compact));
}
}

View File

@ -8,6 +8,7 @@
#ifndef TELEGRAMSENDLOCATION_H
#define TELEGRAMSENDLOCATION_H
#include "qTbot/telegramargs.h"
#include "requests/telegramsinglerquest.h"
namespace qTbot {
@ -17,11 +18,10 @@ namespace qTbot {
class TelegramSendLocation: public TelegramSingleRquest
{
public:
TelegramSendLocation(const QVariant &chatId,
const QString &text,
TelegramSendLocation(const TelegramArgs &args,
float latitude,
float longitude,
unsigned long long replyToMessageId);
const ExtraJsonObjects& extraObjects = {});
};
}
#endif // TELEGRAMSENDLOCATION_H

View File

@ -11,38 +11,13 @@
#include <QJsonObject>
namespace qTbot {
TelegramSendMsg::TelegramSendMsg(const QVariant &chatId,
const QString &text,
const QMap<QString, QSharedPointer<QJsonObject> > &extraObjects,
unsigned long long replyToMessageId,
bool markdown,
const QString &callBackQueryId,
bool disableWebPagePreview)
TelegramSendMsg::TelegramSendMsg(const TelegramArgs& generalArgs,
const QHash<QString, QSharedPointer<QJsonObject> > &extraObjects)
:
TelegramSingleRquest("sendMessage")
{
QMap<QString, QVariant> args {{"chat_id", chatId}};
if (text.size()) {
args["text"] = text;
}
if (replyToMessageId) {
args["reply_to_message_id"] = replyToMessageId;
}
if (markdown) {
args["parse_mode"] = "Markdown";
}
if (disableWebPagePreview) {
args["disable_web_page_preview"] = disableWebPagePreview;
}
if (callBackQueryId.size()) {
args["callback_query_id"] = callBackQueryId;
}
QMap<QString, QVariant>&& args = generalArgs.toMap();
for (auto it = extraObjects.begin(); it != extraObjects.end(); it = std::next(it)) {
args[it.key()] = QJsonDocument(*it.value()).toJson(QJsonDocument::Compact);

View File

@ -8,6 +8,7 @@
#ifndef TELEGRAMSENDMSG_H
#define TELEGRAMSENDMSG_H
#include "qTbot/telegramargs.h"
#include "telegramsinglerquest.h"
namespace qTbot {
@ -17,14 +18,14 @@ namespace qTbot {
class TelegramSendMsg: public TelegramSingleRquest
{
public:
TelegramSendMsg(const QVariant& chatId,
const QString& text,
const QMap<QString, QSharedPointer<QJsonObject>>& extraObjects = {},
unsigned long long replyToMessageId = 0,
bool markdown = true,
const QString& callBackQueryId = "",
bool disableWebPagePreview = false
);
/**
* @brief TelegramSendMsg This object sents msg to telegram server.
* @param args this is generatl arguments of request (include target chat id).
* @param extraObjects additional objects of the telegram messages (json).
*/
TelegramSendMsg(const TelegramArgs& args,
const ExtraJsonObjects& extraObjects = {});
};
}
#endif // TELEGRAMSENDMSG_H

View File

@ -11,24 +11,15 @@
namespace qTbot {
TelegramSendPhoto::TelegramSendPhoto(const QVariant &chatId,
const QString &text,
TelegramSendPhoto::TelegramSendPhoto(const TelegramArgs &args,
const QString &fileName,
const QByteArray &data,
unsigned long long replyToMessageId,
const QMap<QString, QSharedPointer<QJsonObject>>& extraObjects
):
TelegramSendFile("sendPhoto", chatId, text, fileName, TELEGRAM_PHOTO, data, replyToMessageId, extraObjects) {
const ExtraJsonObjects &extraObjects):
TelegramSendFile("sendPhoto", fileName, TELEGRAM_PHOTO, data, args, extraObjects) {}
}
TelegramSendPhoto::TelegramSendPhoto(const QVariant &chatId,
const QString &text,
TelegramSendPhoto::TelegramSendPhoto(const TelegramArgs &args,
const QFileInfo &file,
unsigned long long replyToMessageId,
const QMap<QString, QSharedPointer<QJsonObject> > &extraObjects):
TelegramSendFile("sendPhoto", chatId, text, file, replyToMessageId, extraObjects) {
}
const ExtraJsonObjects &extraObjects):
TelegramSendFile("sendPhoto", file, args, extraObjects){}
}

View File

@ -19,19 +19,15 @@ namespace qTbot {
class TelegramSendPhoto: public TelegramSendFile
{
public:
TelegramSendPhoto(const QVariant &chatId,
const QString &text,
TelegramSendPhoto(const TelegramArgs &args,
const QString &fileName,
const QByteArray& data,
unsigned long long replyToMessageId = 0,
const QMap<QString, QSharedPointer<QJsonObject>>& extraObjects = {}
const ExtraJsonObjects& extraObjects = {}
);
TelegramSendPhoto(const QVariant &chatId,
const QString &text,
TelegramSendPhoto(const TelegramArgs &args,
const QFileInfo &file,
unsigned long long replyToMessageId = 0,
const QMap<QString, QSharedPointer<QJsonObject>>& extraObjects = {}
const ExtraJsonObjects& extraObjects = {}
);
};

View File

@ -17,7 +17,8 @@ namespace qTbot {
TelegramSingleRquest::TelegramSingleRquest(const QString& request,
const QMap<QString, QVariant>& args) {
setRequest(request);
setArgs(args);}
setArgs(args);
}
TelegramSingleRquest::TelegramSingleRquest(const QString &request) {
setRequest(request);

View File

@ -126,7 +126,7 @@ public:
* @brief setProcessed This method sets new list of processed mesages.
* @param newProcessed list of processed messagees.
*/
void setProcessed(const QSet<unsigned long long> &newProcessed);
virtual void setProcessed(const QSet<unsigned long long> &newProcessed);
protected:

View File

@ -20,6 +20,11 @@
namespace qTbot {
/**
* @brief ExtraJsonObjects hash map of the extra objects of the message.
*/
typedef QHash<QString, QSharedPointer<QJsonObject>> ExtraJsonObjects;
/**
* @brief The iRequest class Is main interface for all custom requests.
* @see IBot::sendRequest

View File

@ -10,6 +10,7 @@
#include "file.h"
#include "requests/telegrammdownloadfile.h"
#include "qdir.h"
#include "requests/telegramsendcontact.h"
#include "requests/telegramsenddocument.h"
#include "virtualfile.h"
#include <QNetworkAccessManager>
@ -21,6 +22,7 @@
#include <requests/telegrameditmessage.h>
#include <requests/telegramsendlocation.h>
#include <requests/telegramsendphoto.h>
#include <requests/telegrameditmessagereplymarkup.h>
#include <QNetworkReply>
#include <QSharedPointer>
@ -65,57 +67,27 @@ bool ITelegramBot::login(const QByteArray &token) {
}
bool ITelegramBot::sendMessage(const QVariant &chatId, const QString &text) {
return sendSpecificMessage(chatId.toLongLong(), text);
return sendSpecificMessage(TelegramArgs{chatId, text});
}
bool ITelegramBot::sendSpecificMessage(const QVariant & chatId,
const QString &text,
const QMap<QString, QSharedPointer<QJsonObject>> &extraObjects,
const QString &callBackQueryId,
unsigned long long replyToMessageId,
bool markdown,
bool disableWebPagePreview) {
bool ITelegramBot::sendSpecificMessage(const TelegramArgs& args,
const ExtraJsonObjects &extraObjects) {
if (!chatId.isValid() || chatId.isNull())
if (!args.chatId.isValid() || args.chatId.isNull())
return false;
if (text.isEmpty()) {
if (args.text.isEmpty()) {
return false;
}
auto msg = QSharedPointer<TelegramSendMsg>::create(chatId,
text,
extraObjects,
replyToMessageId,
markdown,
callBackQueryId,
disableWebPagePreview);
auto msg = QSharedPointer<TelegramSendMsg>::create(args, extraObjects);
return bool(sendRequest(msg));
return sendMessageRequest(msg, args.msgIdCB);
}
bool ITelegramBot::sendSpecificMessageWithKeyboard(const QVariant &chatId,
const QString &text,
const QList<QList<QString>> &keyboard,
const QString &callBackQueryId,
bool onTimeKeyboard,
bool autoResizeKeyboard,
unsigned long long replyToMessageId,
bool markdown,
bool disableWebPagePreview) {
if (!chatId.isValid() || chatId.isNull())
return false;
auto msg = QSharedPointer<TelegramSendMsg>::create(chatId,
text,
prepareKeyboard(autoResizeKeyboard, onTimeKeyboard, keyboard),
replyToMessageId,
markdown,
callBackQueryId,
disableWebPagePreview);
return bool(sendRequest(msg));
bool ITelegramBot::sendSpecificMessageWithKeyboard(const TelegramArgs& args,
const KeyboardOnMessage &keyboard) {
return sendSpecificMessage(args, prepareInlineKeyBoard(keyboard));
}
bool ITelegramBot::deleteMessage(const QVariant &chatId, const QVariant &messageId) {
@ -128,42 +100,34 @@ bool ITelegramBot::deleteMessage(const QVariant &chatId, const QVariant &message
auto msg = QSharedPointer<TelegramDeleteMessage>::create(chatId,
messageId);
return bool(sendRequest(msg));
return sendMessageRequest(msg);
}
bool ITelegramBot::editSpecificMessageWithKeyboard(const QVariant & messageId,
const QVariant &chatId,
const QString &newText,
bool markdown,
bool disableWebPagePreview,
const QList<QList<QString>> &keyboard,
const QString &callBackQueryId,
bool ITelegramBot::editSpecificMessageWithKeyboard(const QVariant &messageId,
const TelegramArgs& args,
const QList<QList<QString> > &keyboard,
bool onTimeKeyboard,
bool autoResizeKeyboard) {
if (!chatId.isValid() || chatId.isNull())
if (!args.chatId.isValid() || args.chatId.isNull())
return false;
if (!messageId.isValid() || messageId.isNull())
return false;
auto msg = QSharedPointer<TelegramEditMessage>::create(messageId,
chatId,
newText,
markdown,
disableWebPagePreview,
callBackQueryId,
args,
prepareKeyboard(autoResizeKeyboard,
onTimeKeyboard,
keyboard));
return bool(sendRequest(msg));
return sendMessageRequest(msg, args.msgIdCB);
}
QMap<QString, QSharedPointer<QJsonObject>>
qTbot::ITelegramBot::prepareInlineKeyBoard(const QList<QMap<QString, std::function<void (const QString &, const QVariant &)> > > &keyboard)
ExtraJsonObjects
qTbot::ITelegramBot::prepareInlineKeyBoard(const KeyboardOnMessage &keyboard)
{
QMap<QString, QSharedPointer<QJsonObject>> extraObjects;
ExtraJsonObjects extraObjects;
auto&& keyboardJson = QSharedPointer<QJsonObject>::create();
QJsonArray keyboardArray;
@ -185,11 +149,11 @@ qTbot::ITelegramBot::prepareInlineKeyBoard(const QList<QMap<QString, std::functi
return extraObjects;
}
QMap<QString, QSharedPointer<QJsonObject>>
ExtraJsonObjects
qTbot::ITelegramBot::prepareKeyboard(bool autoResizeKeyboard,
bool onTimeKeyboard,
const QList<QList<QString>> &keyboard) {
QMap<QString, QSharedPointer<QJsonObject>> extraObjects;
ExtraJsonObjects extraObjects;
auto&& keyboardJson = QSharedPointer<QJsonObject>::create();
QJsonArray keyboardArray;
@ -213,84 +177,76 @@ qTbot::ITelegramBot::prepareKeyboard(bool autoResizeKeyboard,
return extraObjects;
}
bool ITelegramBot::editSpecificMessageWithKeyboard(const QVariant &messageId,
const QVariant &chatId,
const QString &text,
bool markdown,
bool disableWebPagePreview,
const KeyboardOnMessage &keyboard,
const QString &callBackQueryId) {
bool ITelegramBot::editSpecificMessageWithKeyboard(const QVariant& messageId,
const TelegramArgs& args,
if (!chatId.isValid() || chatId.isNull())
const KeyboardOnMessage &keyboard ) {
if (!args.chatId.isValid() || args.chatId.isNull())
return false;
if (!messageId.isValid() || messageId.isNull())
return false;
auto msg = QSharedPointer<TelegramEditMessage>::create(messageId,
chatId,
text,
markdown,
disableWebPagePreview,
callBackQueryId,
args,
prepareInlineKeyBoard(keyboard));
return bool(sendRequest(msg));
return sendMessageRequest(msg);
}
bool ITelegramBot::editSpecificMessage(const QVariant &messageId,
bool ITelegramBot::editMessageKeyboard(const QVariant &messageId,
const QVariant &chatId,
const QString& newText,
const QString &callBackQueryId,
bool markdown,
bool disableWebPagePreview) {
const KeyboardOnMessage &keyboard,
const QString &callBackQueryId) {
if (!chatId.isValid() || chatId.isNull())
return false;
if (!messageId.isValid() || messageId.isNull())
return false;
if (newText.isEmpty())
auto msg = QSharedPointer<TelegramEditMessageReplyMarkup>::create(messageId,
TelegramArgs(chatId, "", 0, "html", false, callBackQueryId),
prepareInlineKeyBoard(keyboard));
return sendMessageRequest(msg);
}
bool ITelegramBot::editSpecificMessage(const QVariant &messageId,
const TelegramArgs& args) {
if (!args.chatId.isValid() || args.chatId.isNull())
return false;
if (!messageId.isValid() || messageId.isNull())
return false;
if (args.text.isEmpty())
return false;
auto msg = QSharedPointer<TelegramEditMessage>::create(messageId,
chatId,
newText,
markdown,
disableWebPagePreview,
callBackQueryId
args
);
return bool(sendRequest(msg));
return sendMessageRequest(msg);
}
bool ITelegramBot::sendSpecificMessageWithKeyboard(const QVariant &chatId,
const QString &text,
const KeyboardOnMessage &keyboard,
const QString &callBackQueryId,
unsigned long long replyToMessageId,
bool markdown,
bool disableWebPagePreview) {
bool ITelegramBot::sendSpecificMessageWithKeyboard(const TelegramArgs& args,
const QList<QList<QString> > &keyboard,
bool onTimeKeyboard,
bool autoResizeKeyboard) {
if (!chatId.isValid() || chatId.isNull())
if (!args.chatId.isValid() || args.chatId.isNull())
return false;
if (text.isEmpty()) {
if (args.text.isEmpty()) {
return false;
}
auto msg = QSharedPointer<TelegramSendMsg>::create(chatId,
text,
prepareInlineKeyBoard(keyboard),
replyToMessageId,
markdown,
callBackQueryId,
disableWebPagePreview);
return bool(sendRequest(msg));
return sendSpecificMessage(args, prepareKeyboard(autoResizeKeyboard, onTimeKeyboard, keyboard));
}
QSharedPointer<iFile> ITelegramBot::getFile(const QString &fileId, iFile::Type fileType) {
@ -376,57 +332,28 @@ QSharedPointer<QNetworkReply> ITelegramBot::getFileMeta(const QString &fileId, c
}
bool ITelegramBot::sendFile(const QFileInfo &file, const QVariant &chatId) {
return sendFileWithDescription(file, chatId, "");
return sendFileMessage({chatId}, file);
}
bool ITelegramBot::sendFile(const QByteArray &file, const QString &fileName, const QVariant &chatId) {
return sendFileWithDescription(file, fileName, chatId, "");
return sendFileMessage({chatId}, file, fileName);
}
bool ITelegramBot::sendPhoto(const QFileInfo &photo,
const QVariant &chatId,
const QString &description,
unsigned long long replyToMessageId,
const KeyboardOnMessage &keyboard) {
if (!chatId.isValid() || chatId.isNull())
bool ITelegramBot::sendFileMessage(const TelegramArgs &args, const QFileInfo &file) {
if (!args.chatId.isValid() || args.chatId.isNull())
return false;
if (!photo.isReadable()) {
if (!file.isReadable()) {
return false;
}
return sendFileWithPrivate(QSharedPointer<TelegramSendPhoto>::create(
chatId, description, photo, replyToMessageId, prepareInlineKeyBoard(keyboard)));
return sendMessageRequest(
QSharedPointer<TelegramSendPhoto>::create(args,
file), args.msgIdCB);
}
bool ITelegramBot::sendPhoto(const QByteArray &photo,
const QString &fileName,
const QVariant &chatId,
const QString &description,
unsigned long long replyToMessageId,
const KeyboardOnMessage &keyboard) {
if (!chatId.isValid() || chatId.isNull())
return false;
if (!fileName.size()) {
return false;
}
if (!photo.size()) {
return false;
}
return sendFileWithPrivate(QSharedPointer<TelegramSendPhoto>::create(
chatId, description, fileName, photo, replyToMessageId, prepareInlineKeyBoard(keyboard)));
}
bool ITelegramBot::sendFileWithDescription(const QByteArray &file,
const QString &fileName,
const QVariant &chatId,
const QString &description) {
if (!chatId.isValid() || chatId.isNull())
bool ITelegramBot::sendFileMessage(const TelegramArgs &args, const QByteArray &file, const QString &fileName) {
if (!args.chatId.isValid() || args.chatId.isNull())
return false;
if (!fileName.size()) {
@ -437,20 +364,48 @@ bool ITelegramBot::sendFileWithDescription(const QByteArray &file,
return false;
}
return sendFileWithPrivate(QSharedPointer<TelegramSendDocument>::create(chatId, description, fileName, file));
return sendMessageRequest(QSharedPointer<TelegramSendDocument>::create(args, fileName, file), args.msgIdCB);
}
bool ITelegramBot::sendFileWithDescription(const QFileInfo &file,
const QVariant &chatId,
const QString &description) {
if (!chatId.isValid() || chatId.isNull())
bool ITelegramBot::sendPhoto(const TelegramArgs &args,
const QFileInfo &photo,
const KeyboardOnMessage &keyboard) {
if (!args.chatId.isValid() || args.chatId.isNull())
return false;
if (!file.isReadable()) {
if (!photo.isReadable()) {
return false;
}
return sendFileWithPrivate(QSharedPointer<TelegramSendDocument>::create(chatId, description, file));
return sendMessageRequest(
QSharedPointer<TelegramSendPhoto>::create(args,
photo,
prepareInlineKeyBoard(keyboard)), args.msgIdCB);
}
bool ITelegramBot::sendPhoto(const TelegramArgs &args,
const QByteArray &photo,
const QString &fileName,
const KeyboardOnMessage &keyboard) {
if (!args.chatId.isValid() || args.chatId.isNull()) {
return false;
}
if (!fileName.size()) {
return false;
}
if (!photo.size()) {
return false;
}
return sendMessageRequest(
QSharedPointer<TelegramSendPhoto>::create(args,
fileName,
photo,
prepareInlineKeyBoard(keyboard)),
args.msgIdCB);
}
bool ITelegramBot::sendFileById(const QString &fileID, const QVariant &chatId) {
@ -463,21 +418,35 @@ bool ITelegramBot::sendFileById(const QString &fileID, const QVariant &chatId) {
}
bool ITelegramBot::sendLocation(const QVariant &chatId,
const QString &text,
bool ITelegramBot::sendLocation(const TelegramArgs &args,
float latitude,
float longitude,
unsigned long long replyToMessageId) {
if (!chatId.isValid() || chatId.isNull())
const KeyboardOnMessage &keyboard) {
if (!args.chatId.isValid() || args.chatId.isNull())
return false;
if (!(longitude && latitude)) {
return false;
}
auto&& request = QSharedPointer<TelegramSendLocation>::create(chatId, text, latitude, longitude, replyToMessageId);
return sendMessageRequest(QSharedPointer<TelegramSendLocation>::create(args,
latitude,
longitude,
prepareInlineKeyBoard(keyboard)));
}
return bool(sendRequest(request));
bool ITelegramBot::sendContact(const TelegramArgs &args,
const QString &phone,
const QString &firstName,
const QString &secondName) {
if (!args.chatId.isValid() || args.chatId.isNull())
return false;
return sendMessageRequest(QSharedPointer<TelegramSendContact>::create(args,
firstName,
phone,
secondName));
}
int ITelegramBot::getFileSizeByUniqueId(const QString &id) const {
@ -514,6 +483,36 @@ void ITelegramBot::handleIncomeNewUpdate(const QSharedPointer<iUpdate> & update)
}
}
bool ITelegramBot::sendMessageRequest(const QSharedPointer<iRequest> &rquest,
const std::function<void (int)> &msgIdCB) {
auto&& reply = IBot::sendRequest(rquest);
if (reply) {
connect(reply.get(), &QNetworkReply::finished, this,
[ reply, msgIdCB, this]() {
if (reply->error() == QNetworkReply::NoError) {
QByteArray&& responseData = reply->readAll();
QJsonDocument json = QJsonDocument::fromJson(responseData);
const QJsonObject&& obj = json.object();
if (obj.contains("result")) {
unsigned long long chatId = obj["result"]["chat"]["id"].toInteger();
int messageID = obj["result"]["message_id"].toInt();
if (msgIdCB) {
msgIdCB(messageID);
}
if (chatId) {
_lastMessageId[chatId] = messageID;
}
}
}
});
}
return bool(reply);
}
void ITelegramBot::handleLogin() {
if (_loginReplay) {
@ -562,10 +561,6 @@ void ITelegramBot::handleFileHeader(const QWeakPointer<QNetworkReply> &sender,
}
}
bool ITelegramBot::sendFileWithPrivate(const QSharedPointer<TelegramSendFile> &file) {
return bool(sendRequest(file));
}
QString ITelegramBot::findFileInlocatStorage(const QString &fileId) const {
QDir defaultFileDir(defaultFileStorageLocation());
@ -596,6 +591,10 @@ const QString &ITelegramBot::username() const {
return _username;
}
int ITelegramBot::gelLastMessageId(unsigned long long &chatId) const {
return _lastMessageId.value(chatId, 0);
}
unsigned long long ITelegramBot::id() const {
return _id;
}

View File

@ -11,6 +11,7 @@
#define ITELEGRAMBOT_H
#include "ibot.h"
#include "qTbot/telegramargs.h"
#include <QObject>
class QNetworkAccessManager;
@ -22,9 +23,10 @@ class ITelegramMessage;
class TelegramFile;
class TelegramUpdateAnswer;
class TelegramSendFile;
class TelegramSingleRquest;
typedef std::function<void(const QString& buttonKey, const QVariant& msgID)> ButtonCB;
typedef QList<QMap<QString, ButtonCB >> KeyboardOnMessage;
typedef QList<QHash<QString, ButtonCB >> KeyboardOnMessage;
/**
* @brief The ITelegramBot class This is base implementation of the all telegramm bots.
@ -44,89 +46,50 @@ public:
* @brief Sends a specific message to a chat.
*
* This function sends a specific message to a chat. You can include text and additional objects as needed to customize the message's content and behavior.
*
* @param chatId The unique identifier of the chat to send the message to.
* @param text The text content of the message to be sent.
* @param args - This is structure with general arguments of the tellegram message.
* Use This structure for sets target and another arguments.
* @param extraObjects A map containing additional objects associated with the message (optional).
* @param callBackQueryId The unique identifier for callback queries triggered by the message (optional).
* @param replyToMessageId The unique identifier of the message to reply to, if any (optional).
* @param markdown Set to true to enable Markdown formatting for the text (optional).
* @param disableWebPagePreview Set to true to disable web page previews for links in the message (optional).
*
* @return Returns true if the message was sent successfully, false otherwise.
*
* @note The extraObjects parameter is a map where each key represents the object's name, and the associated value is a shared pointer to a JSON object (optional).
* @note The callBackQueryId parameter is used to handle callback queries when applicable (optional).
* @note By default, the message will be sent with Markdown formatting enabled (optional).
* @note By default, web page previews for links in the message are not disabled (optional).
*/
bool sendSpecificMessage(const QVariant &chatId,
const QString& text,
const QMap<QString, QSharedPointer<QJsonObject> > &extraObjects = {},
const QString &callBackQueryId = {},
unsigned long long replyToMessageId = 0,
bool markdown = true,
bool disableWebPagePreview = false);
bool sendSpecificMessage(const TelegramArgs& args,
const qTbot::ExtraJsonObjects &extraObjects = {});
/**
* @brief Sends a specific message with a custom keyboard to a chat.
*
* This function sends a specific message to a chat with a custom keyboard. The message can contain text and additional settings to customize its behavior.
*
* @param chatId The unique identifier of the chat to send the message to.
* @param text The text content of the message to be sent.
* @param args - This is structure with general arguments of the tellegram message.
* Use This structure for sets target and another arguments.
* @param keyboard A list of maps where each map represents a button with a callback function.
* @param callBackQueryId The unique identifier for callback queries triggered by the message (optional).
* @param replyToMessageId The unique identifier of the message to reply to, if any (optional).
* @param markdown Set to true to enable Markdown formatting for the text (optional).
* @param disableWebPagePreview Set to true to disable web page previews for links in the message (optional).
*
* @return Returns true if the message was sent successfully, false otherwise.
*
* @note The keyboard parameter should be a list of maps where each map represents a button. The button's label is the map key, and the associated callback function is the map value.
* @note The callBackQueryId parameter is used to handle callback queries when buttons are pressed (optional).
* @note By default, the message will be sent with Markdown formatting enabled (optional).
* @note By default, web page previews for links in the message are not disabled (optional).
* @note The keyboard parameter should be a list of maps where each map represents a button.
* The button's label is the map key, and the associated callback function is the map value.
*/
bool sendSpecificMessageWithKeyboard(const QVariant &chatId,
const QString& text,
const KeyboardOnMessage &keyboard,
const QString &callBackQueryId = "",
unsigned long long replyToMessageId = 0,
bool markdown = true,
bool disableWebPagePreview = false);
bool sendSpecificMessageWithKeyboard(const TelegramArgs& args,
const KeyboardOnMessage &keyboard);
/**
* @brief Sends a specific message with a custom keyboard to a chat.
*
* This function sends a specific message to a chat with a custom keyboard. The message can contain text and additional settings to customize its behavior.
*
* @param chatId The unique identifier of the chat to send the message to.
* @param text The text content of the message to be sent.
* This function sends a specific message to a chat with a custom keyboard.
* The message can contain text and additional settings to customize its behavior.
* @param args - This is structure with general arguments of the tellegram message.
* Use This structure for sets target and another arguments.
* @param keyboard A list of lists containing the keyboard buttons to display.
* @param callBackQueryId The unique identifier for callback queries triggered by the message.
* @param onTimeKeyboard Set to true to display the keyboard only once.
* @param autoResizeKeyboard Set to true to automatically resize the keyboard.
* @param replyToMessageId The unique identifier of the message to reply to, if any.
* @param markdown Set to true to enable Markdown formatting for the text.
* @param disableWebPagePreview Set to true to disable web page previews for links in the message.
*
* @return Returns true if the message was sent successfully, false otherwise.
*
* @note The keyboard parameter should be a list of lists of strings representing keyboard buttons.
* @note The callBackQueryId parameter is used to handle callback queries when buttons are pressed.
* @note By default, the message will be sent with Markdown formatting enabled.
* @note By default, web page previews for links in the message are not disabled.
*/
bool sendSpecificMessageWithKeyboard(const QVariant &chatId,
const QString& text,
bool sendSpecificMessageWithKeyboard(const TelegramArgs& args,
const QList<QList<QString> > &keyboard,
const QString &callBackQueryId = "",
bool onTimeKeyboard = false,
bool autoResizeKeyboard = true,
unsigned long long replyToMessageId = 0,
bool markdown = true,
bool disableWebPagePreview = false);
bool autoResizeKeyboard = true);
bool deleteMessage(const QVariant& chatId, const QVariant& messageId) override;
@ -134,44 +97,49 @@ public:
* @brief Edits a specific message with a custom keyboard in a chat.
*
* This function allows you to edit a specific message in a chat with a custom keyboard. You can update the message's text and customize its behavior as needed.
*
* @param args - This is structure with general arguments of the tellegram message.
* Use This structure for sets target and another arguments.
* @param messageId The unique identifier of the message to edit.
* @param chatId The unique identifier of the chat containing the message.
* @param newText The new text content for the message.
* @param markdown Set to true to enable Markdown formatting for the new text (optional).
* @param disableWebPagePreview Set to true to disable web page previews for links in the new text (optional).
* @param keyboard A list of lists containing the new keyboard buttons to display (optional).
* @param callBackQueryId The unique identifier for callback queries triggered by the edited message (optional).
* @param onTimeKeyboard Set to true to display the keyboard only once (optional).
* @param autoResizeKeyboard Set to true to automatically resize the keyboard (optional).
*
* @return Returns true if the message was successfully edited, false otherwise.
*
* @note The keyboard parameter should be a list of lists of strings representing keyboard buttons (optional).
* @note The callBackQueryId parameter is used to handle callback queries when buttons are pressed (optional).
* @note By default, the new text will be displayed with Markdown formatting enabled (optional).
* @note By default, web page previews for links in the new text are not disabled (optional).
*/
bool editSpecificMessageWithKeyboard(const QVariant &messageId,
const QVariant &chatId,
const QString &newText,
bool markdown = true,
bool disableWebPagePreview = false,
const TelegramArgs& args,
const QList<QList<QString> > &keyboard = {},
const QString &callBackQueryId = "",
bool onTimeKeyboard = false,
bool autoResizeKeyboard = false);
/**
* @brief Edits a specific message with a custom keyboard in a chat.
*
* This function allows you to edit a specific message in a chat with a custom keyboard. You can update the message's text and customize its behavior as needed.
*
* @param messageId The unique identifier of the message to edit.
* @param args - This is structure with general arguments of the tellegram message.
* Use This structure for sets target and another arguments.
* @param keyboard A list of maps where each map represents a button with a callback function (optional).
*
* @return Returns true if the message was successfully edited, false otherwise.
*
* @note By default, web page previews for links in the new text are not disabled (optional).
*/
bool editSpecificMessageWithKeyboard(const QVariant& messageId,
const TelegramArgs& args,
const KeyboardOnMessage &keyboard = {});
/**
* @brief Edits a keyboard of message in a chat.
*
* This function allows you to edit a keyboard of specific message.
*
* @param messageId The unique identifier of the message to edit.
* @param chatId The unique identifier of the chat containing the message.
* @param text The new text content for the message.
* @param markdown Set to true to enable Markdown formatting for the new text (optional).
* @param disableWebPagePreview Set to true to disable web page previews for links in the new text (optional).
* @param keyboard A list of maps where each map represents a button with a callback function (optional).
* @param callBackQueryId The unique identifier for callback queries triggered by the edited message (optional).
*
@ -179,16 +147,12 @@ public:
*
* @note The keyboard parameter should be a list of maps where each map represents a button. The button's label is the map key, and the associated callback function is the map value (optional).
* @note The callBackQueryId parameter is used to handle callback queries when buttons are pressed (optional).
* @note By default, the new text will be displayed with Markdown formatting enabled (optional).
* @note By default, web page previews for links in the new text are not disabled (optional).
*/
bool editSpecificMessageWithKeyboard(const QVariant& messageId,
const QVariant &chatId,
const QString &text,
bool markdown = true,
bool disableWebPagePreview = false,
const KeyboardOnMessage &keyboard = {},
const QString &callBackQueryId = "");
bool editMessageKeyboard(const QVariant& messageId,
const QVariant &chatId,
const KeyboardOnMessage &keyboard = {},
const QString &callBackQueryId = "");
/**
* @brief Edits a specific message in a chat.
@ -196,24 +160,14 @@ public:
* This function allows you to edit a specific message in a chat. You can update the message's text and customize its behavior as needed.
*
* @param messageId The unique identifier of the message to edit.
* @param chatId The unique identifier of the chat containing the message.
* @param newText The new text content for the message.
* @param callBackQueryId The unique identifier for callback queries triggered by the edited message (optional).
* @param markdown Set to true to enable Markdown formatting for the new text (optional).
* @param disableWebPagePreview Set to true to disable web page previews for links in the new text (optional).
*
* @param args - This is structure with general arguments of the tellegram message.
* Use This structure for sets target and another arguments.
* @return Returns true if the message was successfully edited, false otherwise.
*
* @note The callBackQueryId parameter is used to handle callback queries when applicable (optional).
* @note By default, the new text will be displayed with Markdown formatting enabled (optional).
* @note By default, web page previews for links in the new text are not disabled (optional).
*/
bool editSpecificMessage(const QVariant &messageId,
const QVariant &chatId,
const QString &newText,
const QString &callBackQueryId = {},
bool markdown = true,
bool disableWebPagePreview = false);
const TelegramArgs& args);
[[nodiscard("do not forget to save shared pointer of file handler, because it's will not save inner bot object.")]]
QSharedPointer<iFile> getFile(const QString& fileId, iFile::Type fileType = iFile::Type::Ram) override;
@ -231,59 +185,49 @@ public:
bool sendFile( const QByteArray& file, const QString& fileName, const QVariant& chatId) override;
/**
* @brief sendFileMessage This method sents a message with file.
* @param file This is path to sents file.
* @param args This is general arguments of the message, include a chatID.
* @return true if the message sents successful else false
*/
bool sendFileMessage( const TelegramArgs& args, const QFileInfo& file);
/**
* @brief sendFileMessage This method sents a message with file.
* @param file This is data that will be sents as a file with name @a fileName.
* @param fileName This is name of the file.
* @param args This is general arguments of the message, include a chatID.
* @return true if the message sents successful else false
*/
bool sendFileMessage( const TelegramArgs& args, const QByteArray& file, const QString& fileName);
/**
* @brief sendPhoto This method will send image into chat with @a chatId
* @param args - This is structure with general arguments of the tellegram message.
* Use This structure for sets target and another arguments.
* @param photo this is photo path.
* @param chatId target chat
* @param replyToMessageId The unique identifier of the message to reply to, if any.
* @param keyboard A list of maps where each map represents a button with a callback function (optional).
* @return true if photo will snt successful
*/
bool sendPhoto(const QFileInfo& photo,
const QVariant& chatId,
const QString &description,
unsigned long long replyToMessageId = 0,
bool sendPhoto(const TelegramArgs& args,
const QFileInfo& photo,
const KeyboardOnMessage &keyboard = {});
/**
* @brief sendPhoto This method will send image into chat with @a chatId
* @param photo this is photo data.
* @param chatId target chat
* @param args - This is structure with general arguments of the tellegram message.
* Use This structure for sets target and another arguments.
* @param fileName This is dispalyed name of photo.
* @param replyToMessageId The unique identifier of the message to reply to, if any.
* @param keyboard A list of maps where each map represents a button with a callback function (optional).
* @return true if photo will snt successful
*/
bool sendPhoto(const QByteArray& photo,
bool sendPhoto(const TelegramArgs &args,
const QByteArray& photo,
const QString& fileName,
const QVariant& chatId,
const QString &description,
unsigned long long replyToMessageId = 0,
const KeyboardOnMessage &keyboard = {});
/**
* @brief sendFileWithDescription This method sents a byte array as a file into @a chatId with additional text @a description.
* @param file This is a file source
* @param fileName This is a dispalyed name of file.
* @param chatId This is distanation id chat.
* @param description additional text for message.
* @return true if the message sents successful else false.
*/
bool sendFileWithDescription(const QByteArray& file,
const QString& fileName,
const QVariant& chatId,
const QString& description);
/**
* @brief sendFileWithDescription This method sents a byte array as a file into @a chatId with additional text @a description.
* @param file This is a file source
* @param chatId This is distanation id chat.
* @param description additional text for message.
* @return true if the message sents successful else false.
*/
bool sendFileWithDescription( const QFileInfo& file,
const QVariant& chatId,
const QString& description);
/**
* @brief sendFileById This is specific method of the telegram bot. sents file by id.
* @param fileID This is file id.
@ -294,27 +238,38 @@ public:
/**
* @brief sendLocation This method sents locatin to user.
* @param chatId This is distanation id chat.
* @param args - This is structure with general arguments of the tellegram message.
* Use This structure for sets target and another arguments.
* @param latitude
* @param longitude
* @param replyToMessageId The unique identifier of the message to reply to, skip if you want to sent new independet message.
* @param keyboard A list of maps where each map represents a button with a callback function (optional).
* @return true if locations sent successful else false
*/
bool sendLocation(const QVariant& chatId,
const QString& text,
bool sendLocation(const TelegramArgs &args,
float latitude,
float longitude,
unsigned long long replyToMessageId = 0);
const KeyboardOnMessage &keyboard = {});
/**
* @brief sendContact This method sents a contact data.
* @param args base arguments of message.
* @param firstName This is first name of contact.
* @param phone This is phone number of contact.
* @param secondName This is second name of user.
* @return true if contact sent successful
*/
bool sendContact(const TelegramArgs &args,
const QString& phone,
const QString& firstName,
const QString& secondName = "");
// to do
// * forwardMessage implementations
// * copyMessage implementations
// * sendPhoto implementations
// * sendAudio implementations
// * sendVideo implementations
// * sendVoice implementations
// * sendLocation implementations
// * sendContact implementations
// * sendPoll implementations
// * sendDice implementations
@ -361,6 +316,13 @@ public:
*/
const QString& username() const;
/**
* @brief gelLastMessageId this method returns last sendet message id.
* @param chatId chat id, when you want to get last message id.
* @return message id.
*/
int gelLastMessageId(unsigned long long &chatId) const;
protected:
/**
@ -399,6 +361,15 @@ protected:
void handleIncomeNewUpdate(const QSharedPointer<iUpdate> &) override;
/**
* @brief sendMessageRequest This method invoke when bot will be sent eny messages into chat.
* @param rquest This is a message request.
* @param msgIdCB call back function for the get a sent message id
* @return true if the message sent successful else false.
*/
virtual bool sendMessageRequest(const QSharedPointer<iRequest> &rquest,
const std::function<void(int msgId)>& msgIdCB = {});
private slots:
void handleLogin();
void handleLoginErr(QNetworkReply::NetworkError err);
@ -407,23 +378,23 @@ private slots:
private:
bool sendFileWithPrivate(const QSharedPointer<TelegramSendFile>& file);
QString findFileInlocatStorage(const QString& fileId) const;
QMap<QString, QSharedPointer<QJsonObject>>
prepareInlineKeyBoard(const QList<QMap<QString, std::function<void (const QString &, const QVariant &)> > > &keyboard);
QMap<QString, QSharedPointer<QJsonObject>>
prepareKeyboard(bool autoResizeKeyboard, bool onTimeKeyboard, const QList<QList<QString> > &keyboard);
QHash<QString, QSharedPointer<QJsonObject> >
prepareInlineKeyBoard(const KeyboardOnMessage &keyboard);
ExtraJsonObjects prepareKeyboard(bool autoResizeKeyboard,
bool onTimeKeyboard,
const QList<QList<QString> > &keyboard);
unsigned long long _id = 0;
QString _username;
QSharedPointer<QNetworkReply> _loginReplay;
QMap<QString, std::function<void(const QString&, const QVariant&)>> _handleButtons;
QHash<unsigned long long, int> _lastMessageId;
QHash<QString, QSharedPointer<TelegramFile>> _filesMetaInfo;
};
}
#endif // ITELEGRAMBOT_H

View File

@ -6,6 +6,7 @@
//#
#include "telegramlocation.h"
#include "qpoint.h"
namespace qTbot {
@ -25,7 +26,11 @@ double TelegramLocation::latitude() const {
}
double TelegramLocation::longitude() const {
return rawJson()["latitude"].toDouble();
return rawJson()["longitude"].toDouble();
}
QPointF TelegramLocation::toQPoint() const {
return QPointF(latitude(), longitude());
}
}

View File

@ -38,6 +38,12 @@ public:
* @return float value of longitude
*/
double longitude() const;
/**
* @brief toQPoint This method convert location data to general qt class.
* @return location as a qt point
*/
QPointF toQPoint() const;
};
}
#endif // TELEGRAMLOCATION_H

View File

@ -0,0 +1,59 @@
//#
//# Copyright (C) 2023-2023 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 "telegramargs.h"
namespace qTbot {
TelegramArgs::TelegramArgs(const QVariant &id,
const QString &text,
unsigned long long replyToMessageId,
const QString &parseMode,
bool disableWebPagePreview,
const QString &callBackQueryId, const std::function<void (int)> &msgIdCB)
{
this->chatId = id;
this->text = text;
this->callBackQueryId = callBackQueryId;
this->disableWebPagePreview = disableWebPagePreview;
this->replyToMessageId = replyToMessageId;
this->parseMode = parseMode;
this->msgIdCB = msgIdCB;
}
QMap<QString, QVariant> TelegramArgs::toMap(bool textAsCaption) const {
QMap<QString, QVariant> args {{"chat_id", chatId}};
if (text.size()) {
if (textAsCaption) {
args["caption"] = text;
} else {
args["text"] = text;
}
}
if (replyToMessageId) {
args["reply_to_message_id"] = replyToMessageId;
}
if (parseMode.size()) {
args["parse_mode"] = parseMode;
}
if (disableWebPagePreview) {
args["disable_web_page_preview"] = disableWebPagePreview;
}
if (callBackQueryId.size()) {
args["callback_query_id"] = callBackQueryId;
}
return args;
}
}

View File

@ -0,0 +1,80 @@
//#
//# Copyright (C) 2023-2023 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.
//#
#ifndef TELEGRAMARGS_H
#define TELEGRAMARGS_H
#include <QVariant>
#include "global.h"
namespace qTbot {
/**
* @brief The TelegramArgs class is base structure for the all tellegram message arguments.
*/
struct QTBOT_EXPORT TelegramArgs
{
TelegramArgs(const QVariant& id,
const QString& text = "",
unsigned long long replyToMessageId = 0,
const QString& parseMode = "html",
bool disableWebPagePreview = false,
const QString& callBackQueryId = "",
const std::function<void(int msgId)>& msgIdCB = {}
);
/**
* @brief Text of the message.
* Default: "".
*/
QString text = "";
/**
* @brief Chat ID where the message will be sent.
* Default: {}.
*/
QVariant chatId = {};
/**
* @brief Callback query ID.
* Default: "".
*/
QString callBackQueryId = {};
/**
* @brief ID of the message to which a reply will be given.
* Default: 0.
*/
unsigned long long replyToMessageId = 0;
/**
* @brief Parse mode of the message.
* Default: "html".
*/
QString parseMode = "html";
/**
* @brief Disable web page preview when sending links.
* Default: false.
*/
bool disableWebPagePreview = false;
/**
* @brief toMap convert all arguments to the map.
* @param textAsCaption This option force to replace text key to caption. Used on some requests as a sendDocument
* @return list of arguments.
*/
QMap<QString, QVariant> toMap(bool textAsCaption = false) const;
/**
* @brief msgIdCB This is id message call bak function. Will be inwoked when request finished successful.
*/
std::function<void(int msgId)> msgIdCB = {};
};
}
#endif // TELEGRAMARGS_H

View File

@ -39,7 +39,7 @@ void TelegramRestBot::startUpdates() {
if (delta >= _updateDelay) {
auto&& replay = sendRequest(QSharedPointer<TelegramGetUpdate>::create());
auto&& replay = sendRequest(QSharedPointer<TelegramGetUpdate>::create(_lanstUpdateid + 1));
connect(replay.get(), &QNetworkReply::finished,
this, std::bind(&TelegramRestBot::handleReceiveUpdates, this, replay.toWeakRef()),
@ -50,9 +50,9 @@ void TelegramRestBot::startUpdates() {
Qt::DirectConnection);
return;
} else {
QTimer::singleShot( _updateDelay - delta, this, [this](){startUpdates();});
}
QTimer::singleShot( _updateDelay - delta, this, [this](){startUpdates();});
}
int TelegramRestBot::updateDelay() const {
@ -63,6 +63,15 @@ void TelegramRestBot::setUpdateDelay(int newUpdateDelay) {
_updateDelay = newUpdateDelay;
}
void TelegramRestBot::setProcessed(const QSet<unsigned long long> &newProcessed) {
auto&& it = std::min_element(newProcessed.begin(), newProcessed.end());
if (it != newProcessed.end()) {
_lanstUpdateid = *it;
}
IBot::setProcessed(newProcessed);
}
void TelegramRestBot::handleReceiveUpdates(const QWeakPointer<QNetworkReply> &replay) {
if (auto&& sharedReplay = replay.lock()) {
@ -75,6 +84,9 @@ void TelegramRestBot::handleReceiveUpdates(const QWeakPointer<QNetworkReply> &re
for (const auto& ref: resultArray) {
auto&& update = IBot::makeMesasge<TelegramUpdate>(ref.toObject());
incomeNewUpdate(update);
if (_lanstUpdateid < update->updateId()) {
_lanstUpdateid = update->updateId();
};
}
}
}

View File

@ -41,6 +41,7 @@ public:
*/
void setUpdateDelay(int newUpdateDelay);
void setProcessed(const QSet<unsigned long long> &newProcessed) override;
private slots:
void handleReceiveUpdates(const QWeakPointer<QNetworkReply>& replay);
void handleReceiveUpdatesErr(QNetworkReply::NetworkError err);
@ -49,6 +50,8 @@ private:
void startUpdates();
long long _lanstUpdateTime = 0;
unsigned long long _lanstUpdateid = 0;
int _updateDelay = 1000;
};