From 84a70acefa63c74f30e49d25134fb537f32a6412 Mon Sep 17 00:00:00 2001 From: light Date: Sun, 12 Dec 2021 14:31:23 +0500 Subject: [PATCH 01/11] added include file for lib --- include.pri | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 include.pri diff --git a/include.pri b/include.pri new file mode 100644 index 0000000..e5f2f1d --- /dev/null +++ b/include.pri @@ -0,0 +1,11 @@ + +HEADERS += \ + $$PWD/src/telegrambot.h \ + $$PWD/modules/httpserver/httpserver.h \ + $$PWD/vendor/qdelegate/include/qdelegate.h + +INCLUDEPATH += $$PWD/src/ +INCLUDEPATH += $$PWD/modules/sslserver/ +INCLUDEPATH += $$PWD/modules/httpserver/ +INCLUDEPATH += $$PWD/vendor/qdelegate/include/ + From ddde9f0b3857489ffc8910ef8f527a96e1a6d125 Mon Sep 17 00:00:00 2001 From: light Date: Mon, 13 Dec 2021 02:15:14 +0500 Subject: [PATCH 02/11] 64 bit identifiers --- src/telegramdatastructs.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/telegramdatastructs.h b/src/telegramdatastructs.h index c8e16c9..5023a75 100644 --- a/src/telegramdatastructs.h +++ b/src/telegramdatastructs.h @@ -234,7 +234,7 @@ typedef QList> TelegramKeyboard; // TelegramBotChat - This object represents a chat. struct TelegramBotChat : public TelegramBotObject { - qint32 id; // Unique identifier for this chat. This number may be greater than 32 bits and some programming languages may have difficulty/silent defects in interpreting it. But it is smaller than 52 bits, so a signed 64 bit integer or double-precision float type are safe for storing this identifier. + qint64 id; // Unique identifier for this chat. This number from now can be signed 64 bit integer or double-precision float type are safe for storing this identifier. QString type; // Type of chat, can be either “private”, “group”, “supergroup” or “channel” QString title; // Optional. Title, for supergroups, channels and group chats QString username; // Optional. Username, for private chats, supergroups and channels if available @@ -247,7 +247,7 @@ struct TelegramBotChat : public TelegramBotObject { // parse logic virtual void fromJson(QJsonObject& object) { - JsonHelperT::jsonPathGet(object, "id", this->id); + JsonHelperT::jsonPathGet(object, "id", this->id); JsonHelperT::jsonPathGet(object, "type", this->type); JsonHelperT::jsonPathGet(object, "title", this->title, false); JsonHelperT::jsonPathGet(object, "username", this->username, false); From 5921d5ac23ebb0e4de9ada513909729a022ad17a Mon Sep 17 00:00:00 2001 From: light Date: Wed, 5 Jan 2022 21:33:45 +0500 Subject: [PATCH 03/11] fix build Qt 5.15+ --- modules/httpserver/httpserver.cpp | 7 ++++--- src/jsonhelper.cpp | 2 +- src/telegrambot.cpp | 2 -- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/modules/httpserver/httpserver.cpp b/modules/httpserver/httpserver.cpp index 82978e3..9a64913 100644 --- a/modules/httpserver/httpserver.cpp +++ b/modules/httpserver/httpserver.cpp @@ -46,14 +46,15 @@ void HttpServer::handleNewData() // build response QByteArray responseContent; - responseContent += (response->version.isEmpty() ? "HTTP/1.1" : response->version) + " "; // Version - responseContent += QByteArray::number((qint32)response->status) + " " + response->StatusNames.value((qint32)response->status, "") + "\r\n"; // Status + responseContent += (response->version.isEmpty() ? "HTTP/1.1" : response->version.toUtf8()) + " "; // Version + responseContent += QByteArray::number((qint32)response->status) + " " + + response->StatusNames.value((qint32)response->status, "").toUtf8() + "\r\n"; // Status // add headers qint64 contentLength = 0; for(auto itr = response->headers.begin(); itr != response->headers.end(); itr++) { if(itr.key().toLower() == "content-length") contentLength = itr.value().toLongLong(); - responseContent += itr.key() + ": " + itr.value() + "\r\n"; + responseContent += itr.key().toUtf8() + ": " + itr.value().toUtf8() + "\r\n"; } // add content length if not available diff --git a/src/jsonhelper.cpp b/src/jsonhelper.cpp index c8352b0..643d7a8 100644 --- a/src/jsonhelper.cpp +++ b/src/jsonhelper.cpp @@ -3,7 +3,7 @@ QVariant JsonHelper::jsonPathGetImpl(QJsonValue data, QString path, bool showWarnings) { // json parse - QStringList splittedPath = path.split('.', QString::SkipEmptyParts); + QStringList splittedPath = path.split('.'); QStringList processed; while(!splittedPath.isEmpty()) { QString pathElement = splittedPath.takeFirst(); diff --git a/src/telegrambot.cpp b/src/telegrambot.cpp index a5df7a1..40ec79c 100644 --- a/src/telegrambot.cpp +++ b/src/telegrambot.cpp @@ -754,8 +754,6 @@ QNetworkReply* TelegramBot::callApi(QString method, QUrlQuery params, bool delet QUrl url(QString("https://api.telegram.org/bot%1/%2").arg(this->apiKey, method)); url.setQuery(params); - qDebug() << url; - // execute QNetworkRequest request(url); QNetworkReply* reply = multiPart ? this->aManager.post(request, multiPart) : this->aManager.get(request); From 65841255fffa46e2157ed6cdd2fa2afe13053957 Mon Sep 17 00:00:00 2001 From: light Date: Thu, 6 Jan 2022 04:16:52 +0500 Subject: [PATCH 04/11] name corrections and defaults --- src/telegrambot.cpp | 30 +++++++++++++++--------------- src/telegrambot.h | 2 +- src/telegramdatastructs.h | 4 ++-- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/telegrambot.cpp b/src/telegrambot.cpp index 40ec79c..d0123bf 100644 --- a/src/telegrambot.cpp +++ b/src/telegrambot.cpp @@ -207,7 +207,7 @@ void TelegramBot::sendMessage(QVariant chatId, QString text, int replyToMessageI if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId)); // handle reply markup - this->hanldeReplyMarkup(params, flags, keyboard); + this->handleReplyMarkup(params, flags, keyboard); // call api return this->callApiTemplate("sendMessage", params, response); @@ -233,7 +233,7 @@ void TelegramBot::editMessageText(QVariant chatId, QVariant messageId, QString t // only build inline keyboard if(!(flags && TelegramFlags::ReplyKeyboardMarkup) && !(flags && TelegramFlags::ForceReply) && !(flags && TelegramFlags::ReplyKeyboardRemove)) { - this->hanldeReplyMarkup(params, flags, keyboard); + this->handleReplyMarkup(params, flags, keyboard); } // call api @@ -251,7 +251,7 @@ void TelegramBot::editMessageCaption(QVariant chatId, QVariant messageId, QStrin if(!caption.isNull()) params.addQueryItem("caption", caption); // only build inline keyboard - this->hanldeReplyMarkup(params, TelegramFlags(), keyboard); + this->handleReplyMarkup(params, TelegramFlags(), keyboard); // call api this->callApiTemplate("editMessageCaption", params, response); @@ -267,7 +267,7 @@ void TelegramBot::editMessageReplyMarkup(QVariant chatId, QVariant messageId, Te params.addQueryItem(isInlineMessageId ? "inline_message_id" : "message_id", messageId.toString()); // only build inline keyboard - this->hanldeReplyMarkup(params, TelegramFlags(), keyboard); + this->handleReplyMarkup(params, TelegramFlags(), keyboard); // call api this->callApiTemplate("editMessageReplyMarkup", params, response); @@ -305,7 +305,7 @@ void TelegramBot::sendPhoto(QVariant chatId, QVariant photo, QString caption, in if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId)); // handle reply markup - this->hanldeReplyMarkup(params, flags, keyboard); + this->handleReplyMarkup(params, flags, keyboard); // handle file QHttpMultiPart* multiPart = this->handleFile("photo", photo, params); @@ -326,7 +326,7 @@ void TelegramBot::sendAudio(QVariant chatId, QVariant audio, QString caption, QS if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId)); // handle reply markup - this->hanldeReplyMarkup(params, flags, keyboard); + this->handleReplyMarkup(params, flags, keyboard); // handle file QHttpMultiPart* multiPart = this->handleFile("audio", audio, params); @@ -344,7 +344,7 @@ void TelegramBot::sendDocument(QVariant chatId, QVariant document, QString capti if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId)); // handle reply markup - this->hanldeReplyMarkup(params, flags, keyboard); + this->handleReplyMarkup(params, flags, keyboard); // handle file QHttpMultiPart* multiPart = this->handleFile("document", document, params); @@ -361,7 +361,7 @@ void TelegramBot::sendSticker(QVariant chatId, QVariant sticker, int replyToMess if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId)); // handle reply markup - this->hanldeReplyMarkup(params, flags, keyboard); + this->handleReplyMarkup(params, flags, keyboard); // handle file QHttpMultiPart* multiPart = this->handleFile("sticker", sticker, params); @@ -382,7 +382,7 @@ void TelegramBot::sendVideo(QVariant chatId, QVariant video, QString caption, in if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId)); // handle reply markup - this->hanldeReplyMarkup(params, flags, keyboard); + this->handleReplyMarkup(params, flags, keyboard); // handle file QHttpMultiPart* multiPart = this->handleFile("video", video, params); @@ -401,7 +401,7 @@ void TelegramBot::sendVoice(QVariant chatId, QVariant voice, QString caption, in if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId)); // handle reply markup - this->hanldeReplyMarkup(params, flags, keyboard); + this->handleReplyMarkup(params, flags, keyboard); // handle file QHttpMultiPart* multiPart = this->handleFile("voice", voice, params); @@ -420,7 +420,7 @@ void TelegramBot::sendVideoNote(QVariant chatId, QVariant videoNote, int length, if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId)); // handle reply markup - this->hanldeReplyMarkup(params, flags, keyboard); + this->handleReplyMarkup(params, flags, keyboard); // handle file QHttpMultiPart* multiPart = this->handleFile("video_note", videoNote, params); @@ -439,7 +439,7 @@ void TelegramBot::sendLocation(QVariant chatId, double latitude, double longitud if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId)); // handle reply markup - this->hanldeReplyMarkup(params, flags, keyboard); + this->handleReplyMarkup(params, flags, keyboard); // call api this->callApiTemplate("sendLocation", params, response); @@ -458,7 +458,7 @@ void TelegramBot::sendVenue(QVariant chatId, double latitude, double longitude, if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId)); // handle reply markup - this->hanldeReplyMarkup(params, flags, keyboard); + this->handleReplyMarkup(params, flags, keyboard); // call api this->callApiTemplate("sendVenue", params, response); @@ -475,7 +475,7 @@ void TelegramBot::sendContact(QVariant chatId, QString phoneNumber, QString firs if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId)); // handle reply markup - this->hanldeReplyMarkup(params, flags, keyboard); + this->handleReplyMarkup(params, flags, keyboard); // call api this->callApiTemplate("sendContact", params, response); @@ -791,7 +791,7 @@ QHttpMultiPart* TelegramBot::createUploadFile(QString name, QString fileName, QB return multiPart; } -void TelegramBot::hanldeReplyMarkup(QUrlQuery& params, TelegramFlags flags, TelegramKeyboardRequest &keyboard) +void TelegramBot::handleReplyMarkup(QUrlQuery& params, TelegramFlags flags, TelegramKeyboardRequest &keyboard) { // handle types QString replyMarkup; diff --git a/src/telegrambot.h b/src/telegrambot.h index 2627511..00d3fbc 100644 --- a/src/telegrambot.h +++ b/src/telegrambot.h @@ -164,7 +164,7 @@ class TelegramBot : public QObject // helpers QHttpMultiPart* createUploadFile(QString name, QString fileName, QByteArray& content, bool detectMimeType = false, QHttpMultiPart* multiPart = 0); - void hanldeReplyMarkup(QUrlQuery& params, TelegramFlags flags, TelegramKeyboardRequest& keyboard); + void handleReplyMarkup(QUrlQuery& params, TelegramFlags flags, TelegramKeyboardRequest& keyboard); QHttpMultiPart* handleFile(QString fieldName, QVariant file, QUrlQuery& params, QHttpMultiPart* multiPart = 0); // global data diff --git a/src/telegramdatastructs.h b/src/telegramdatastructs.h index 5023a75..71fa073 100644 --- a/src/telegramdatastructs.h +++ b/src/telegramdatastructs.h @@ -18,8 +18,8 @@ QString switchInlineQueryCurrentChat; \ \ /* normal keyboard */ \ - bool requestContact; \ - bool requestLocation; + bool requestContact = false; \ + bool requestLocation = false; struct TelegramBotKeyboardButtonRequest { From f60f6fc8898b97e442dea8f664495ed5b072bf5f Mon Sep 17 00:00:00 2001 From: light Date: Sat, 6 May 2023 17:37:25 +0500 Subject: [PATCH 05/11] prepare answerinlinequery support --- src/telegrambot.cpp | 30 +++++++++++++++++++++++ src/telegrambot.h | 2 +- src/telegramdatainterface.h | 1 + src/telegramdatastructs.h | 48 ++++++++++++++++++++++++++++++++++--- 4 files changed, 77 insertions(+), 4 deletions(-) diff --git a/src/telegrambot.cpp b/src/telegrambot.cpp index d0123bf..d248613 100644 --- a/src/telegrambot.cpp +++ b/src/telegrambot.cpp @@ -191,6 +191,36 @@ void TelegramBot::answerCallbackQuery(QString callbackQueryId, QString text, boo this->callApiTemplate("answerCallbackQuery", params, response); } +void TelegramBot::answerInlineQuery(const QString inlineQueryId, + QList &results , + bool is_personal, + int cacheTime, + QString switch_pm_text, + QString switch_pm_parameter, + QString url, + bool *response) { + QUrlQuery params; + params.addQueryItem("inline_query_id", inlineQueryId); + QJsonArray formedResults; + for (auto result : results) { + QJsonObject resultObj; + result->toJson(resultObj); + formedResults.append(resultObj); + delete result; + } + if (!switch_pm_parameter.isEmpty()) + params.addQueryItem("switch_pm_parameter", switch_pm_parameter); + + if (!switch_pm_text.isEmpty()) + params.addQueryItem("switch_pm_text", switch_pm_text); + + params.addQueryItem("results", QJsonDocument(formedResults).toJson(QJsonDocument::Compact)); + if(!url.isNull()) params.addQueryItem("url", url); + if (cacheTime > 0) params.addQueryItem("cache_time", QString::number(cacheTime)); + + this->callApiTemplate("answerInlineQuery", params, response); +} + /* * Message Functions diff --git a/src/telegrambot.h b/src/telegrambot.h index 00d3fbc..ed5b7c9 100644 --- a/src/telegrambot.h +++ b/src/telegrambot.h @@ -107,7 +107,7 @@ class TelegramBot : public QObject // Callback Query Functions void answerCallbackQuery(QString callbackQueryId, QString text = QString(), bool showAlert = false, int cacheTime = 0, QString url = QString(), bool* response = 0); - + void answerInlineQuery(const QString inlineQueryId, QList &results, bool is_personal = false, int cacheTime=0, QString switch_pm_text="", QString switch_pm_parameter="", QString url="", bool* response = 0); // Message Functions void sendMessage(QVariant chatId, QString text, int replyToMessageId = 0, TelegramFlags flags = TelegramFlags::NoFlag, TelegramKeyboardRequest keyboard = TelegramKeyboardRequest(), TelegramBotMessage* response = 0); void editMessageText(QVariant chatId, QVariant messageId, QString text, TelegramFlags flags = TelegramFlags::NoFlag, TelegramKeyboardRequest keyboard = TelegramKeyboardRequest(), bool* response = 0); diff --git a/src/telegramdatainterface.h b/src/telegramdatainterface.h index e3346fe..af3f0f5 100644 --- a/src/telegramdatainterface.h +++ b/src/telegramdatainterface.h @@ -6,6 +6,7 @@ struct TelegramBotObject { virtual void fromJson(QJsonObject& object) = 0; + virtual void toJson(QJsonObject& object) {Q_UNUSED(object)}; virtual ~TelegramBotObject() {} }; diff --git a/src/telegramdatastructs.h b/src/telegramdatastructs.h index 71fa073..6e88580 100644 --- a/src/telegramdatastructs.h +++ b/src/telegramdatastructs.h @@ -605,6 +605,13 @@ struct TelegramBotInputMessageContent : public TelegramBotObject { JsonHelperT::jsonPathGet(object, "parse_mode", this->parseMode, false); JsonHelperT::jsonPathGet(object, "disable_web_page_preview", this->disableWebPagePreview, false); } + QJsonObject toJson() { + QJsonObject ret; + ret.insert("message_text", messageText); + if (!parseMode.isEmpty()) + ret.insert("parse_mode", parseMode); + return ret; + } }; // This object represents one result of an inline query. Telegram clients currently support results of the following 20 types: @@ -613,16 +620,44 @@ struct TelegramBotInlineQueryResult : public TelegramBotObject { QString type; // Type of the result, must be article QString id; // Unique identifier for this result, 1-64 Bytes QString title; // Title of the result - TelegramBotInputMessageContent inputMessageContent; // Content of the message to be sent TelegramKeyboard replyMarkup; // Optional. Inline keyboard attached to the message QString url; // Optional. URL of the result bool hideUrl; // Optional. Pass True, if you don't want the URL to be shown in the message - QString description; // Optional. Short description of the result QString thumbUrl; // Optional. Url of the thumbnail for the result qint32 thumbWidth; // Optional. Thumbnail width qint32 thumbHeight; // Optional. Thumbnail height - virtual void fromJson(QJsonObject& object) { + virtual void fromJson(QJsonObject& object) override { + JsonHelperT::jsonPathGet(object, "type", this->type); + JsonHelperT::jsonPathGet(object, "id", this->id); + JsonHelperT::jsonPathGet(object, "title", this->title); + JsonHelperT::jsonPathGetArrayArray(object, "reply_markup", this->replyMarkup, false); + JsonHelperT::jsonPathGet(object, "url", this->url, false); + JsonHelperT::jsonPathGet(object, "hide_url", this->hideUrl, false); + JsonHelperT::jsonPathGet(object, "thumb_url", this->thumbUrl, false); + JsonHelperT::jsonPathGet(object, "thumb_width", this->thumbWidth, false); + JsonHelperT::jsonPathGet(object, "thumb_height", this->thumbHeight, false); + } + virtual void toJson(QJsonObject & object) override { + object.insert("id", id); + object.insert("type", type); + object.insert("title", title); + } +}; + +// This object represents one result of an inline query. Telegram clients currently support results of the following 20 types: +class TelegramBotInlineQueryResultArticle : + public TelegramBotInlineQueryResult { +public: + TelegramBotInlineQueryResultArticle() { + type = "article"; + } + TelegramBotInputMessageContent inputMessageContent; // Content of the message to be sent + QString description; // Optional. Short description of the result + QString thumbUrl; // Optional. Url of the thumbnail for the result + qint32 thumbWidth; // Optional. Thumbnail width + qint32 thumbHeight; // Optional. Thumbnail height + void fromJson(QJsonObject& object) override { JsonHelperT::jsonPathGet(object, "type", this->type); JsonHelperT::jsonPathGet(object, "id", this->id); JsonHelperT::jsonPathGet(object, "title", this->title); @@ -635,6 +670,13 @@ struct TelegramBotInlineQueryResult : public TelegramBotObject { JsonHelperT::jsonPathGet(object, "thumb_width", this->thumbWidth, false); JsonHelperT::jsonPathGet(object, "thumb_height", this->thumbHeight, false); } + void toJson(QJsonObject & object) override { + TelegramBotInlineQueryResult::toJson(object); + object.insert("input_message_content", + inputMessageContent.toJson()); + if (!description.isEmpty()) + object.insert("description", description); + } }; // Represents a link to a photo. By default, this photo will be sent by the user with optional caption. Alternatively, you can use inputMessageContent to send a message with the specified content instead of the photo. From 8b930fe0761094e464f67dfa6b044bf6cce2005d Mon Sep 17 00:00:00 2001 From: light Date: Sun, 7 May 2023 13:11:55 +0500 Subject: [PATCH 06/11] answerinlinequery is_personal field --- src/telegrambot.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/telegrambot.cpp b/src/telegrambot.cpp index d248613..9f30a98 100644 --- a/src/telegrambot.cpp +++ b/src/telegrambot.cpp @@ -214,6 +214,8 @@ void TelegramBot::answerInlineQuery(const QString inlineQueryId, if (!switch_pm_text.isEmpty()) params.addQueryItem("switch_pm_text", switch_pm_text); + params.addQueryItem("is_personal", is_personal?"true":"false"); + params.addQueryItem("results", QJsonDocument(formedResults).toJson(QJsonDocument::Compact)); if(!url.isNull()) params.addQueryItem("url", url); if (cacheTime > 0) params.addQueryItem("cache_time", QString::number(cacheTime)); From 3e0543f6574b5338cde05b1156b8dafee599565e Mon Sep 17 00:00:00 2001 From: light Date: Sun, 7 May 2023 23:21:24 +0500 Subject: [PATCH 07/11] answerinline with inline keyboard --- src/telegramdatastructs.h | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/telegramdatastructs.h b/src/telegramdatastructs.h index 6e88580..0122a55 100644 --- a/src/telegramdatastructs.h +++ b/src/telegramdatastructs.h @@ -219,7 +219,7 @@ struct TelegramBotKeyboardButton : public TelegramBotObject TelegramBotGame callbackGame; // parse logic - virtual void fromJson(QJsonObject& object) { + virtual void fromJson(QJsonObject& object) override { JsonHelperT::jsonPathGet(object, "text", this->text); JsonHelperT::jsonPathGet(object, "url", this->url, false); JsonHelperT::jsonPathGet(object, "callback_data", this->callbackData, false); @@ -229,6 +229,11 @@ struct TelegramBotKeyboardButton : public TelegramBotObject JsonHelperT::jsonPathGet(object, "request_contact", this->requestContact, false); JsonHelperT::jsonPathGet(object, "request_location", this->requestLocation, false); } + void toJson(QJsonObject & object) override { + object.insert("text", text); + if (!callbackData.isEmpty()) + object.insert("callback_data", callbackData); + } }; typedef QList> TelegramKeyboard; @@ -642,6 +647,23 @@ struct TelegramBotInlineQueryResult : public TelegramBotObject { object.insert("id", id); object.insert("type", type); object.insert("title", title); + if (!replyMarkup.isEmpty()) { + QJsonArray keyboardJ; + for (const auto &keyboardRow : replyMarkup) { + QJsonArray keyboardLine; + for (auto keyboard : keyboardRow) { + QJsonObject keyObj; + keyboard.toJson(keyObj); + + keyboardLine.append(keyObj); + } + keyboardJ.append(keyboardLine); + } + QJsonObject inlineObj; + inlineObj.insert("inline_keyboard", keyboardJ); + object.insert("reply_markup", inlineObj); + } + } }; From de0637afe4526c243d53f8ffb362ef33e9c47234 Mon Sep 17 00:00:00 2001 From: light Date: Mon, 15 May 2023 07:19:26 +0500 Subject: [PATCH 08/11] 64 bit user id --- src/telegramdatastructs.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/telegramdatastructs.h b/src/telegramdatastructs.h index 0122a55..a531f8d 100644 --- a/src/telegramdatastructs.h +++ b/src/telegramdatastructs.h @@ -105,7 +105,7 @@ struct TelegramBotWebHookInfo : public TelegramBotObject { // TelegramBotUser - This object represents a Telegram user or bot. struct TelegramBotUser : public TelegramBotObject { - qint32 id; // Unique identifier for this user or bot + qint64 id; // Unique identifier for this user or bot QString firstName; // User‘s or bot’s first name QString lastName; // Optional. User‘s or bot’s last name QString username; // Optional. User‘s or bot’s username @@ -116,7 +116,7 @@ struct TelegramBotUser : public TelegramBotObject { // parse logic virtual void fromJson(QJsonObject& object) { - JsonHelperT::jsonPathGet(object, "id", this->id); + JsonHelperT::jsonPathGet(object, "id", this->id); JsonHelperT::jsonPathGet(object, "first_name", this->firstName); JsonHelperT::jsonPathGet(object, "last_name", this->lastName, false); JsonHelperT::jsonPathGet(object, "username", this->username, false); From 6baa90a7fce73fc4530e3ad58008f782c09c7a3c Mon Sep 17 00:00:00 2001 From: light Date: Wed, 17 May 2023 08:48:22 +0500 Subject: [PATCH 09/11] fix parse answers --- src/telegramdatainterface.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/telegramdatainterface.h b/src/telegramdatainterface.h index af3f0f5..4ee51cb 100644 --- a/src/telegramdatainterface.h +++ b/src/telegramdatainterface.h @@ -16,7 +16,11 @@ class JsonHelperT Date: Fri, 19 May 2023 09:23:37 +0500 Subject: [PATCH 10/11] fix answerinlinephoto --- src/telegrambot.cpp | 28 ++++++++++++--------- src/telegrambot.h | 19 +++++++------- src/telegramdatainterface.h | 3 ++- src/telegramdatastructs.h | 49 ++++++++++++++++++++++--------------- 4 files changed, 57 insertions(+), 42 deletions(-) diff --git a/src/telegrambot.cpp b/src/telegrambot.cpp index 9f30a98..9544c31 100644 --- a/src/telegrambot.cpp +++ b/src/telegrambot.cpp @@ -235,7 +235,7 @@ void TelegramBot::sendMessage(QVariant chatId, QString text, int replyToMessageI if(flags && TelegramFlags::Markdown) params.addQueryItem("parse_mode", "Markdown"); else if(flags && TelegramFlags::Html) params.addQueryItem("parse_mode", "HTML"); if(flags && TelegramFlags::DisableWebPagePreview) params.addQueryItem("disable_web_page_preview", "true"); - if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true"); + if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true"); if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId)); // handle reply markup @@ -311,7 +311,7 @@ void TelegramBot::forwardMessage(QVariant targetChatId, QVariant fromChatId, qin params.addQueryItem("chat_id", targetChatId.toString()); params.addQueryItem("from_chat_id", fromChatId.toString()); params.addQueryItem("message_id", QString::number(fromMessageId)); - if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true"); + if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true"); this->callApiTemplate("forwardMessage", params, response); } @@ -333,7 +333,11 @@ void TelegramBot::sendPhoto(QVariant chatId, QVariant photo, QString caption, in QUrlQuery params; params.addQueryItem("chat_id", chatId.toString()); if(!caption.isNull()) params.addQueryItem("caption", caption); - if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true"); + if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true"); + if(flags && TelegramFlags::Markdown) params.addQueryItem("parse_mode", "Markdown"); + else if(flags && TelegramFlags::Html) params.addQueryItem("parse_mode", "HTML"); + if(flags && TelegramFlags::MarkDownV2) params.addQueryItem("parse_mode", "MarkdownV2"); + if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId)); // handle reply markup @@ -354,7 +358,7 @@ void TelegramBot::sendAudio(QVariant chatId, QVariant audio, QString caption, QS if(duration >= 0) params.addQueryItem("duration", QString::number(duration)); if(!performer.isNull()) params.addQueryItem("performer", performer); if(!title.isNull()) params.addQueryItem("title", title); - if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true"); + if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true"); if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId)); // handle reply markup @@ -372,7 +376,7 @@ void TelegramBot::sendDocument(QVariant chatId, QVariant document, QString capti QUrlQuery params; params.addQueryItem("chat_id", chatId.toString()); if(!caption.isNull()) params.addQueryItem("caption", caption); - if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true"); + if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true"); if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId)); // handle reply markup @@ -389,7 +393,7 @@ void TelegramBot::sendSticker(QVariant chatId, QVariant sticker, int replyToMess { QUrlQuery params; params.addQueryItem("chat_id", chatId.toString()); - if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true"); + if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true"); if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId)); // handle reply markup @@ -410,7 +414,7 @@ void TelegramBot::sendVideo(QVariant chatId, QVariant video, QString caption, in if(duration >= 0) params.addQueryItem("duration", QString::number(duration)); if(width >= 0) params.addQueryItem("width", QString::number(width)); if(height >= 0) params.addQueryItem("height", QString::number(height)); - if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true"); + if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true"); if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId)); // handle reply markup @@ -429,7 +433,7 @@ void TelegramBot::sendVoice(QVariant chatId, QVariant voice, QString caption, in params.addQueryItem("chat_id", chatId.toString()); if(!caption.isNull()) params.addQueryItem("caption", caption); if(duration >= 0) params.addQueryItem("duration", QString::number(duration)); - if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true"); + if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true"); if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId)); // handle reply markup @@ -448,7 +452,7 @@ void TelegramBot::sendVideoNote(QVariant chatId, QVariant videoNote, int length, params.addQueryItem("chat_id", chatId.toString()); if(length >= 0) params.addQueryItem("length", QString::number(length)); if(duration >= 0) params.addQueryItem("duration", QString::number(duration)); - if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true"); + if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true"); if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId)); // handle reply markup @@ -467,7 +471,7 @@ void TelegramBot::sendLocation(QVariant chatId, double latitude, double longitud params.addQueryItem("chat_id", chatId.toString()); params.addQueryItem("latitude", QString::number(latitude)); params.addQueryItem("longitude", QString::number(longitude)); - if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true"); + if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true"); if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId)); // handle reply markup @@ -486,7 +490,7 @@ void TelegramBot::sendVenue(QVariant chatId, double latitude, double longitude, params.addQueryItem("title", title); params.addQueryItem("address", address); if(!foursquareId.isNull()) params.addQueryItem("foursquare_id", foursquareId); - if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true"); + if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true"); if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId)); // handle reply markup @@ -503,7 +507,7 @@ void TelegramBot::sendContact(QVariant chatId, QString phoneNumber, QString firs params.addQueryItem("phone_number", phoneNumber); params.addQueryItem("first_name", firstName); if(!lastName.isNull()) params.addQueryItem("last_name", lastName); - if(flags && TelegramFlags::DisableNotfication) params.addQueryItem("disable_notification", "true"); + if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true"); if(replyToMessageId) params.addQueryItem("reply_to_message_id", QString::number(replyToMessageId)); // handle reply markup diff --git a/src/telegrambot.h b/src/telegrambot.h index ed5b7c9..0426e62 100644 --- a/src/telegrambot.h +++ b/src/telegrambot.h @@ -56,23 +56,24 @@ class TelegramBot : public QObject // Message Markdown = 1 << 0, - Html = 1 << 1, - DisableWebPagePreview = 1 << 2, - DisableNotfication = 1 << 3, + MarkDownV2 = 1 << 1, + Html = 1 << 2, + DisableWebPagePreview = 1 << 3, + DisableNotification = 1 << 4, // Keyboard Global - Selective = 1 << 4, + Selective = 1 << 5, // ReplyKeyboardMarkup - ReplyKeyboardMarkup = 1 << 5, - ResizeKeyboard = 1 << 6, - OneTimeKeyboard = 1 << 7, + ReplyKeyboardMarkup = 1 << 6, + ResizeKeyboard = 1 << 7, + OneTimeKeyboard = 1 << 8, // ReplyKeyboardRemove - ReplyKeyboardRemove = 1 << 8, + ReplyKeyboardRemove = 1 << 9, // ForceReply - ForceReply = 1 << 9 + ForceReply = 1 << 10 }; // Keyboard construction helpers diff --git a/src/telegramdatainterface.h b/src/telegramdatainterface.h index 4ee51cb..eea809c 100644 --- a/src/telegramdatainterface.h +++ b/src/telegramdatainterface.h @@ -3,8 +3,9 @@ #include "jsonhelper.h" -struct TelegramBotObject +class TelegramBotObject { +public: virtual void fromJson(QJsonObject& object) = 0; virtual void toJson(QJsonObject& object) {Q_UNUSED(object)}; virtual ~TelegramBotObject() {} diff --git a/src/telegramdatastructs.h b/src/telegramdatastructs.h index a531f8d..4975eb7 100644 --- a/src/telegramdatastructs.h +++ b/src/telegramdatastructs.h @@ -620,11 +620,12 @@ struct TelegramBotInputMessageContent : public TelegramBotObject { }; // This object represents one result of an inline query. Telegram clients currently support results of the following 20 types: -struct TelegramBotInlineQueryResult : public TelegramBotObject { - +class TelegramBotInlineQueryResult : public TelegramBotObject { +public: QString type; // Type of the result, must be article QString id; // Unique identifier for this result, 1-64 Bytes QString title; // Title of the result + QString description; // Optional. Short description of the result TelegramKeyboard replyMarkup; // Optional. Inline keyboard attached to the message QString url; // Optional. URL of the result bool hideUrl; // Optional. Pass True, if you don't want the URL to be shown in the message @@ -636,6 +637,7 @@ struct TelegramBotInlineQueryResult : public TelegramBotObject { JsonHelperT::jsonPathGet(object, "type", this->type); JsonHelperT::jsonPathGet(object, "id", this->id); JsonHelperT::jsonPathGet(object, "title", this->title); + JsonHelperT::jsonPathGet(object, "description", this->description); JsonHelperT::jsonPathGetArrayArray(object, "reply_markup", this->replyMarkup, false); JsonHelperT::jsonPathGet(object, "url", this->url, false); JsonHelperT::jsonPathGet(object, "hide_url", this->hideUrl, false); @@ -647,6 +649,7 @@ struct TelegramBotInlineQueryResult : public TelegramBotObject { object.insert("id", id); object.insert("type", type); object.insert("title", title); + object.insert("description", description); if (!replyMarkup.isEmpty()) { QJsonArray keyboardJ; for (const auto &keyboardRow : replyMarkup) { @@ -675,19 +678,14 @@ class TelegramBotInlineQueryResultArticle : type = "article"; } TelegramBotInputMessageContent inputMessageContent; // Content of the message to be sent - QString description; // Optional. Short description of the result QString thumbUrl; // Optional. Url of the thumbnail for the result qint32 thumbWidth; // Optional. Thumbnail width qint32 thumbHeight; // Optional. Thumbnail height void fromJson(QJsonObject& object) override { - JsonHelperT::jsonPathGet(object, "type", this->type); - JsonHelperT::jsonPathGet(object, "id", this->id); - JsonHelperT::jsonPathGet(object, "title", this->title); + TelegramBotInlineQueryResult::fromJson(object); JsonHelperT::jsonPathGet(object, "input_message_content", this->inputMessageContent); - JsonHelperT::jsonPathGetArrayArray(object, "reply_markup", this->replyMarkup, false); JsonHelperT::jsonPathGet(object, "url", this->url, false); JsonHelperT::jsonPathGet(object, "hide_url", this->hideUrl, false); - JsonHelperT::jsonPathGet(object, "description", this->description, false); JsonHelperT::jsonPathGet(object, "thumb_url", this->thumbUrl, false); JsonHelperT::jsonPathGet(object, "thumb_width", this->thumbWidth, false); JsonHelperT::jsonPathGet(object, "thumb_height", this->thumbHeight, false); @@ -997,26 +995,37 @@ struct TelegramBotInlineQueryResultGame : public TelegramBotObject { }; // Represents a link to a photo stored on the Telegram servers. By default, this photo will be sent by the user with an optional caption. Alternatively, you can use inputMessageContent to send a message with the specified content instead of the photo. -struct TelegramBotInlineQueryResultCachedPhoto : public TelegramBotObject { - QString type; // Type of the result, must be photo - QString id; // Unique identifier for this result, 1-64 bytes +class TelegramBotInlineQueryResultCachedPhoto : + public TelegramBotInlineQueryResult { + +public: + TelegramBotInlineQueryResultCachedPhoto() { + type = "photo"; + } QString photoFileId; // A valid file identifier of the photo - QString title; // Optional. Title for the result - QString description; // Optional. Short description of the result QString caption; // Optional. Caption of the photo to be sent, 0-200 characters - TelegramKeyboard replyMarkup; // Optional. Inline keyboard attached to the message + QString parseMode; TelegramBotInputMessageContent inputMessageContent; // Optional. Content of the message to be sent instead of the photo - virtual void fromJson(QJsonObject& object) { - JsonHelperT::jsonPathGet(object, "type", this->type); - JsonHelperT::jsonPathGet(object, "id", this->id); + virtual void fromJson(QJsonObject& object) override { + TelegramBotInlineQueryResult::fromJson(object); JsonHelperT::jsonPathGet(object, "photo_file_id", this->photoFileId); - JsonHelperT::jsonPathGet(object, "title", this->title, false); - JsonHelperT::jsonPathGet(object, "description", this->description, false); JsonHelperT::jsonPathGet(object, "caption", this->caption, false); - JsonHelperT::jsonPathGetArrayArray(object, "reply_markup", this->replyMarkup, false); + JsonHelperT::jsonPathGet(object, "parse_mode", this->parseMode, false); JsonHelperT::jsonPathGet(object, "input_message_content", this->inputMessageContent, false); } + void toJson(QJsonObject & object) override { + TelegramBotInlineQueryResult::toJson(object); + + object.insert("photo_file_id", photoFileId); + object.insert("caption", caption); + object.insert("parse_mode", parseMode); + if (!inputMessageContent.messageText.isEmpty()) + object.insert("input_message_content", + inputMessageContent.toJson()); + if (!description.isEmpty()) + object.insert("description", description); + } }; // Represents a link to an animated GIF file stored on the Telegram servers. By default, this animated GIF file will be sent by the user with an optional caption. Alternatively, you can use inputMessageContent to send a message with specified content instead of the animation. From bfc3460aff27122f999dc77a4d0ccd9721eeb846 Mon Sep 17 00:00:00 2001 From: light Date: Mon, 6 Oct 2025 02:39:30 +0500 Subject: [PATCH 11/11] markdownv2 fix support --- src/telegrambot.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/telegrambot.cpp b/src/telegrambot.cpp index 9544c31..4dcd440 100644 --- a/src/telegrambot.cpp +++ b/src/telegrambot.cpp @@ -233,6 +233,7 @@ void TelegramBot::sendMessage(QVariant chatId, QString text, int replyToMessageI params.addQueryItem("chat_id", chatId.toString()); params.addQueryItem("text", text); if(flags && TelegramFlags::Markdown) params.addQueryItem("parse_mode", "Markdown"); + else if(flags && TelegramFlags::MarkDownV2) params.addQueryItem("parse_mode", "MarkdownV2"); else if(flags && TelegramFlags::Html) params.addQueryItem("parse_mode", "HTML"); if(flags && TelegramFlags::DisableWebPagePreview) params.addQueryItem("disable_web_page_preview", "true"); if(flags && TelegramFlags::DisableNotification) params.addQueryItem("disable_notification", "true");