From 38f8755a35e696c52c3bae6aceb19513b73d41af Mon Sep 17 00:00:00 2001 From: llnulldisk <48621230+llnulldisk@users.noreply.github.com> Date: Sun, 4 Sep 2022 17:16:56 +0200 Subject: Update to API 5.2 --- src/Api.cpp | 25 ++++++--- src/TgTypeParser.cpp | 109 +++++++++++++++++++++++++++++++++----- src/types/InputMessageContent.cpp | 2 + 3 files changed, 117 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/Api.cpp b/src/Api.cpp index bcb711c..5cdab54 100644 --- a/src/Api.cpp +++ b/src/Api.cpp @@ -893,12 +893,15 @@ Message::Ptr Api::sendDice(std::int64_t chatId, return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendDice", args)); } -void Api::sendChatAction(std::int64_t chatId, const string& action) const { +bool Api::sendChatAction(std::int64_t chatId, + const std::string& action) const { vector args; args.reserve(2); + args.emplace_back("chat_id", chatId); args.emplace_back("action", action); - sendRequest("sendChatAction", args); + + return sendRequest("sendChatAction", args).get("", false); } UserProfilePhotos::Ptr Api::getUserProfilePhotos(std::int64_t userId, std::int32_t offset, std::int32_t limit) const { @@ -1573,7 +1576,6 @@ Message::Ptr Api::sendInvoice(std::int64_t chatId, const std::string& description, const std::string& payload, const std::string& providerToken, - const std::string& startParameter, const std::string& currency, const std::vector& prices, const std::string& providerData, @@ -1591,18 +1593,29 @@ Message::Ptr Api::sendInvoice(std::int64_t chatId, std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup, bool disableNotification, - bool allowSendingWithoutReply) const { + bool allowSendingWithoutReply, + std::int32_t maxTipAmount, + const std::vector& suggestedTipAmounts, + const std::string& startParameter) const { vector args; - args.reserve(24); + args.reserve(26); args.emplace_back("chat_id", chatId); args.emplace_back("title", title); args.emplace_back("description", description); args.emplace_back("payload", payload); args.emplace_back("provider_token", providerToken); - args.emplace_back("start_parameter", startParameter); args.emplace_back("currency", currency); args.emplace_back("prices", _tgTypeParser.parseArray(&TgTypeParser::parseLabeledPrice, prices)); + args.emplace_back("max_tip_amount", maxTipAmount); + if (!suggestedTipAmounts.empty()) { + args.emplace_back("suggested_tip_amounts", _tgTypeParser.parseArray([] (const std::int32_t& option) -> std::int32_t { + return option; + }, suggestedTipAmounts)); + } + if (!startParameter.empty()) { + args.emplace_back("start_parameter", startParameter); + } if (!providerData.empty()) { args.emplace_back("provider_data", providerData); } diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index 0491360..48b542a 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -188,6 +188,7 @@ Message::Ptr TgTypeParser::parseJsonAndGetMessage(const boost::property_tree::pt result->passportData = tryParseJson(&TgTypeParser::parseJsonAndGetPassportData, data, "passport_data"); result->passportData = tryParseJson(&TgTypeParser::parseJsonAndGetPassportData, data, "passport_data"); result->proximityAlertTriggered = tryParseJson(&TgTypeParser::parseJsonAndGetProximityAlertTriggered, data, "proximity_alert_triggered"); + result->voiceChatScheduled = tryParseJson(&TgTypeParser::parseJsonAndGetVoiceChatScheduled, data, "voice_chat_scheduled"); result->voiceChatStarted = tryParseJson(&TgTypeParser::parseJsonAndGetVoiceChatStarted, data, "voice_chat_started"); result->voiceChatEnded = tryParseJson(&TgTypeParser::parseJsonAndGetVoiceChatEnded, data, "voice_chat_ended"); result->voiceChatParticipantsInvited = tryParseJson(&TgTypeParser::parseJsonAndGetVoiceChatParticipantsInvited, data, "voice_chat_participants_invited"); @@ -253,6 +254,7 @@ std::string TgTypeParser::parseMessage(const Message::Ptr& object) const { appendToJson(result, "connected_website", object->connectedWebsite); appendToJson(result, "passport_data", parsePassportData(object->passportData)); appendToJson(result, "proximity_alert_triggered", parseProximityAlertTriggered(object->proximityAlertTriggered)); + appendToJson(result, "voice_chat_scheduled", parseVoiceChatScheduled(object->voiceChatScheduled)); appendToJson(result, "voice_chat_started", parseVoiceChatStarted(object->voiceChatStarted)); appendToJson(result, "voice_chat_ended", parseVoiceChatEnded(object->voiceChatEnded)); appendToJson(result, "voice_chat_participants_invited", parseVoiceChatParticipantsInvited(object->voiceChatParticipantsInvited)); @@ -913,6 +915,24 @@ std::string TgTypeParser::parseMessageAutoDeleteTimerChanged(const MessageAutoDe return result; } +VoiceChatScheduled::Ptr TgTypeParser::parseJsonAndGetVoiceChatScheduled(const boost::property_tree::ptree& data) const { + auto result(std::make_shared()); + result->startDate = data.get("start_date", 0); + return result; +} + +std::string TgTypeParser::parseVoiceChatScheduled(const VoiceChatScheduled::Ptr& object) const { + if (!object) { + return ""; + } + std::string result; + result += '{'; + appendToJson(result, "start_date", object->startDate); + removeLastComma(result); + result += '}'; + return result; +} + VoiceChatStarted::Ptr TgTypeParser::parseJsonAndGetVoiceChatStarted(const boost::property_tree::ptree& data) const { auto result(std::make_shared()); return result; @@ -1529,12 +1549,12 @@ std::string TgTypeParser::parseGenericReply(const GenericReply::Ptr& object) con InlineQuery::Ptr TgTypeParser::parseJsonAndGetInlineQuery(const boost::property_tree::ptree& data) const { auto result(std::make_shared()); - result->id = data.get("id"); + result->id = data.get("id", ""); result->from = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "from"); + result->query = data.get("query", ""); + result->offset = data.get("offset", ""); + result->chatType = data.get("chat_type", ""); result->location = tryParseJson(&TgTypeParser::parseJsonAndGetLocation, data, "location"); - result->query = data.get("query"); - result->offset = data.get("offset"); - return result; } @@ -1546,9 +1566,10 @@ std::string TgTypeParser::parseInlineQuery(const InlineQuery::Ptr& object) const result += '{'; appendToJson(result, "id", object->id); appendToJson(result, "from", parseUser(object->from)); - appendToJson(result, "location", parseLocation(object->location)); appendToJson(result, "query", object->query); appendToJson(result, "offset", object->offset); + appendToJson(result, "chat_type", object->chatType); + appendToJson(result, "location", parseLocation(object->location)); removeLastComma(result); result += '}'; return result; @@ -2499,17 +2520,20 @@ InputMessageContent::Ptr TgTypeParser::parseJsonAndGetInputMessageContent(const std::string messageText = data.get("message_text", ""); float latitude = data.get("latitude", 1000); // latitude belong (-90,90) - std::string title = data.get("title", ""); + std::string address = data.get("address", ""); std::string phoneNumber = data.get("phone_number", ""); + std::string description = data.get("description", ""); if (!messageText.empty()) { result = std::static_pointer_cast(parseJsonAndGetInputTextMessageContent(data)); - } else if (!title.empty()) { + } else if (!address.empty()) { result = std::static_pointer_cast(parseJsonAndGetInputVenueMessageContent(data)); } else if (latitude != 1000) { result = std::static_pointer_cast(parseJsonAndGetInputLocationMessageContent(data)); } else if (!phoneNumber.empty()) { result = std::static_pointer_cast(parseJsonAndGetInputContactMessageContent(data)); + } else if (!description.empty()) { + result = std::static_pointer_cast(parseJsonAndGetInputInvoiceMessageContent(data)); } else { result = std::make_shared(); } @@ -2526,15 +2550,14 @@ std::string TgTypeParser::parseInputMessageContent(const InputMessageContent::Pt if (object->type == InputTextMessageContent::TYPE) { result += parseInputTextMessageContent(std::static_pointer_cast(object)); - } - else if (object->type == InputLocationMessageContent::TYPE) { + } else if (object->type == InputLocationMessageContent::TYPE) { result += parseInputLocationMessageContent(std::static_pointer_cast(object)); - } - else if (object->type == InputVenueMessageContent::TYPE) { + } else if (object->type == InputVenueMessageContent::TYPE) { result += parseInputVenueMessageContent(std::static_pointer_cast(object)); - } - else if (object->type == InputContactMessageContent::TYPE) { + } else if (object->type == InputContactMessageContent::TYPE) { result += parseInputContactMessageContent(std::static_pointer_cast(object)); + } else if (object->type == InputInvoiceMessageContent::TYPE) { + result += parseInputInvoiceMessageContent(std::static_pointer_cast(object)); } removeLastComma(result); @@ -2650,6 +2673,66 @@ std::string TgTypeParser::parseInputContactMessageContent(const InputContactMess return result; } +InputInvoiceMessageContent::Ptr TgTypeParser::parseJsonAndGetInputInvoiceMessageContent(const boost::property_tree::ptree& data) const { + // NOTE: This function will be called by parseJsonAndGetInputMessageContent(). + auto result(std::make_shared()); + result->title = data.get("title", ""); + result->description = data.get("description", ""); + result->payload = data.get("payload", ""); + result->providerToken = data.get("provider_token", ""); + result->currency = data.get("currency", ""); + result->prices = parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetLabeledPrice, data, "prices"); + result->maxTipAmount = data.get("max_tip_amount", 0); + result->suggestedTipAmounts = parseJsonAndGetArray([] (const boost::property_tree::ptree& innerData)->std::int32_t { + return innerData.get(0); + }, data, "suggested_tip_amounts"); + result->providerData = data.get("provider_data", ""); + result->photoUrl = data.get("photo_url", ""); + result->photoSize = data.get("photo_size", 0); + result->photoWidth = data.get("photo_width", 0); + result->photoHeight = data.get("photo_height", 0); + result->needName = data.get("need_name", false); + result->needPhoneNumber = data.get("need_phone_number", false); + result->needEmail = data.get("need_email", false); + result->needShippingAddress = data.get("need_shipping_address", false); + result->sendPhoneNumberToProvider = data.get("send_phone_number_to_provider", false); + result->sendEmailToProvider = data.get("send_email_to_provider", false); + result->isFlexible = data.get("is_flexible", false); + return result; +} + +std::string TgTypeParser::parseInputInvoiceMessageContent(const InputInvoiceMessageContent::Ptr& object) const { + if (!object) { + return ""; + } + // This function will be called by parseInputMessageContent() + std::string result; + appendToJson(result, "title", object->title); + appendToJson(result, "description", object->description); + appendToJson(result, "payload", object->payload); + appendToJson(result, "provider_token", object->providerToken); + appendToJson(result, "currency", object->currency); + appendToJson(result, "prices", parseArray(&TgTypeParser::parseLabeledPrice, object->prices)); + appendToJson(result, "max_tip_amount", object->maxTipAmount); + appendToJson(result, "suggested_tip_amounts", parseArray([] (std::int32_t i)->std::int32_t { + return i; + }, object->suggestedTipAmounts)); + appendToJson(result, "provider_data", object->providerData); + appendToJson(result, "photo_url", object->photoUrl); + appendToJson(result, "photo_size", object->photoSize); + appendToJson(result, "photo_width", object->photoWidth); + appendToJson(result, "photo_height", object->photoHeight); + appendToJson(result, "need_name", object->needName); + appendToJson(result, "need_phone_number", object->needPhoneNumber); + appendToJson(result, "need_email", object->needEmail); + appendToJson(result, "need_shipping_address", object->needShippingAddress); + appendToJson(result, "send_phone_number_to_provider", object->sendPhoneNumberToProvider); + appendToJson(result, "send_email_to_provider", object->sendEmailToProvider); + appendToJson(result, "is_flexible", object->isFlexible); + // The last comma will be erased by parseInputMessageContent(). + return result; +} + Invoice::Ptr TgTypeParser::parseJsonAndGetInvoice(const boost::property_tree::ptree& data) const { auto result(std::make_shared()); result->title = data.get("title"); diff --git a/src/types/InputMessageContent.cpp b/src/types/InputMessageContent.cpp index 8f6d69d..2f54e00 100644 --- a/src/types/InputMessageContent.cpp +++ b/src/types/InputMessageContent.cpp @@ -2,6 +2,7 @@ #include "tgbot/types/InputLocationMessageContent.h" #include "tgbot/types/InputVenueMessageContent.h" #include "tgbot/types/InputContactMessageContent.h" +#include "tgbot/types/InputInvoiceMessageContent.h" #include @@ -11,3 +12,4 @@ const std::string InputTextMessageContent::TYPE = "text"; const std::string InputLocationMessageContent::TYPE = "location"; const std::string InputVenueMessageContent::TYPE = "venue"; const std::string InputContactMessageContent::TYPE = "contact"; +const std::string InputInvoiceMessageContent::TYPE = "invoice"; -- cgit v1.2.3