From 4aa6af3180aef70f9a4320a2f6d818f68f5bb669 Mon Sep 17 00:00:00 2001 From: llnulldisk <48621230+llnulldisk@users.noreply.github.com> Date: Tue, 30 Aug 2022 19:34:12 +0200 Subject: Sort api methods --- src/Api.cpp | 1399 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 697 insertions(+), 702 deletions(-) (limited to 'src/Api.cpp') diff --git a/src/Api.cpp b/src/Api.cpp index 38ad8e3..def9243 100644 --- a/src/Api.cpp +++ b/src/Api.cpp @@ -19,6 +19,67 @@ Api::Api(string token, const HttpClient& httpClient, const std::string& url) : _token(std::move(token)), _httpClient(httpClient), _tgTypeParser(), _url(url) { } +vector Api::getUpdates(std::int32_t offset, std::int32_t limit, std::int32_t timeout, const StringArrayPtr& allowedUpdates) const { + vector args; + args.reserve(4); + if (offset) { + args.emplace_back("offset", offset); + } + limit = max(1, min(100, limit)); + args.emplace_back("limit", limit); + if (timeout) { + args.emplace_back("timeout", timeout); + } + if (allowedUpdates != nullptr) { + string allowedUpdatesJson = _tgTypeParser.parseArray( + [] (const string& s)->string { + return s; + }, *allowedUpdates); + args.emplace_back("allowed_updates", allowedUpdatesJson); + } + return _tgTypeParser.parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetUpdate, sendRequest("getUpdates", args)); +} + +void Api::setWebhook(const string& url, const InputFile::Ptr certificate, std::int32_t maxConnection, const StringArrayPtr& allowedUpdates) const { + vector args; + args.reserve(4); + if (!url.empty()) { + args.emplace_back("url", url); + } + if (certificate != nullptr) { + args.emplace_back("certificate", certificate->data, true, certificate->mimeType, certificate->fileName); + } + if (maxConnection != 40) { + args.emplace_back("max_connections", maxConnection); + } + if (allowedUpdates != nullptr) { + auto allowedUpdatesJson = _tgTypeParser.parseArray( + [] (const string& s)->string { + return s; + }, *allowedUpdates); + args.emplace_back("allowed_updates", allowedUpdatesJson); + } + + sendRequest("setWebhook", args); +} + +bool Api::deleteWebhook() const { + return sendRequest("deleteWebhook").get("", false); +} + +WebhookInfo::Ptr Api::getWebhookInfo() const { + ptree p = sendRequest("getWebhookInfo"); + + if (!p.get_child_optional("url")) { + return nullptr; + } + if (p.get("url", "") != string("")) { + return _tgTypeParser.parseJsonAndGetWebhookInfo(p); + } else { + return nullptr; + } +} + User::Ptr Api::getMe() const { return _tgTypeParser.parseJsonAndGetUser(sendRequest("getMe")); } @@ -37,7 +98,7 @@ Message::Ptr Api::sendMessage(boost::variant c if (disableWebPagePreview) { args.emplace_back("disable_web_page_preview", disableWebPagePreview); } - if (disableNotification){ + if (disableNotification) { args.emplace_back("disable_notification", disableNotification); } if (replyToMessageId) { @@ -51,8 +112,8 @@ Message::Ptr Api::sendMessage(boost::variant c } Message::Ptr Api::sendMessage(const std::string& chatId, const std::string& text, bool disableWebPagePreview, - std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup, const std::string& parseMode, - bool disableNotification) const { + std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup, const std::string& parseMode, + bool disableNotification) const { return sendMessage(chatId, text, parseMode, disableWebPagePreview, disableNotification, replyToMessageId, replyMarkup); } @@ -70,7 +131,7 @@ Message::Ptr Api::forwardMessage(std::int64_t chatId, std::int64_t fromChatId, s args.emplace_back("chat_id", chatId); args.emplace_back("from_chat_id", fromChatId); args.emplace_back("message_id", messageId); - if (disableNotification){ + if (disableNotification) { args.emplace_back("disable_notification", disableNotification); } return _tgTypeParser.parseJsonAndGetMessage(sendRequest("forwardMessage", args)); @@ -98,13 +159,13 @@ Message::Ptr Api::sendPhoto(std::int64_t chatId, boost::variant audio, const string &caption, std::int32_t duration, const string& performer, const string& title, boost::variant thumb, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { +Message::Ptr Api::sendAudio(std::int64_t chatId, boost::variant audio, const string& caption, std::int32_t duration, const string& performer, const string& title, boost::variant thumb, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector args; args.reserve(11); args.emplace_back("chat_id", chatId); @@ -120,10 +181,10 @@ Message::Ptr Api::sendAudio(std::int64_t chatId, boost::variant document, boost::variant thumb, const string &caption, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { +Message::Ptr Api::sendDocument(std::int64_t chatId, boost::variant document, boost::variant thumb, const string& caption, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector args; args.reserve(8); args.emplace_back("chat_id", chatId); @@ -181,64 +242,45 @@ Message::Ptr Api::sendDocument(std::int64_t chatId, boost::variant& prices, - const std::string& providerData, const std::string& photoUrl, std::int32_t photoSize, - std::int32_t photoWidth, std::int32_t photoHeight, bool needName, - bool needPhoneNumber, bool needEmail, bool needShippingAddress, - bool sendPhoneNumberToProvider, bool sendEmailToProvider, bool isFlexible, - std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const { +Message::Ptr Api::sendVideo(std::int64_t chatId, boost::variant video, bool supportsStreaming, std::int32_t duration, std::int32_t width, std::int32_t height, boost::variant thumb, const string& caption, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector args; - args.reserve(23); + args.reserve(12); 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)); - if (!providerData.empty()) { - args.emplace_back("provider_data", providerData); - } - if (!photoUrl.empty()) { - args.emplace_back("photo_url", photoUrl); - } - if (photoSize) { - args.emplace_back("photo_size", photoSize); - } - if (photoWidth) { - args.emplace_back("photo_width", photoWidth); - } - if (photoHeight) { - args.emplace_back("photo_height", photoHeight); - } - if (needName) { - args.emplace_back("need_name", needName); + if (video.which() == 0 /* InputFile::Ptr */) { + auto file = boost::get(video); + args.emplace_back("video", file->data, true, file->mimeType, file->fileName); + } else /* std::string */ { + args.emplace_back("video", boost::get(video)); } - if (needPhoneNumber) { - args.emplace_back("need_phone_number", needPhoneNumber); + if (supportsStreaming) { + args.emplace_back("supports_streaming", supportsStreaming); } - if (needEmail) { - args.emplace_back("need_email", needEmail); + if (duration) { + args.emplace_back("duration", duration); } - if (needShippingAddress) { - args.emplace_back("need_shipping_address", needShippingAddress); + if (width) { + args.emplace_back("width", width); } - if (sendPhoneNumberToProvider) { - args.emplace_back("send_phone_number_to_provider", sendPhoneNumberToProvider); + if (height) { + args.emplace_back("height", height); } - if (sendEmailToProvider) { - args.emplace_back("send_email_to_provider", sendEmailToProvider); + if (thumb.which() == 0 /* InputFile::Ptr */) { + auto file = boost::get(thumb); + args.emplace_back("thumb", file->data, true, file->mimeType, file->fileName); + } else /* std::string */ { + auto thumbStr = boost::get(thumb); + if (!thumbStr.empty()) { + args.emplace_back("thumb", thumbStr); + } } - if (isFlexible) { - args.emplace_back("is_flexible", isFlexible); + if (!caption.empty()) { + args.emplace_back("caption", caption); } if (replyToMessageId) { args.emplace_back("reply_to_message_id", replyToMessageId); @@ -246,47 +288,45 @@ Message::Ptr Api::sendInvoice(std::int64_t chatId, const std::string& title, con if (replyMarkup) { args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); } - if (disableNotification){ + if (!parseMode.empty()) { + args.emplace_back("parse_mode", parseMode); + } + if (disableNotification) { args.emplace_back("disable_notification", disableNotification); } - return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendInvoice", args)); + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendVideo", args)); } -bool Api::answerShippingQuery(const std::string& shippingQueryId, bool ok, const std::vector& shippingOptions, const std::string& errorMessage) const { +Message::Ptr Api::sendAnimation(std::int64_t chatId, boost::variant animation, std::int32_t duration, std::int32_t width, std::int32_t height, boost::variant thumb, const string& caption, std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector args; - args.reserve(4); - args.emplace_back("shipping_query_id", shippingQueryId); - args.emplace_back("ok", ok); - if (!shippingOptions.empty()) { - args.emplace_back("shipping_options", _tgTypeParser.parseArray(&TgTypeParser::parseShippingOption, shippingOptions)); + args.reserve(11); + args.emplace_back("chat_id", chatId); + if (animation.which() == 0 /* InputFile::Ptr */) { + auto file = boost::get(animation); + args.emplace_back("animation", file->data, true, file->mimeType, file->fileName); + } else /* std::string */ { + args.emplace_back("animation", boost::get(animation)); } - if (!errorMessage.empty()) { - args.emplace_back("error_message", errorMessage); + if (duration) { + args.emplace_back("duration", duration); } - return sendRequest("answerShippingQuery", args).get("", false); -} - -bool Api::answerPreCheckoutQuery(const std::string& preCheckoutQueryId, bool ok, const std::string& errorMessage) const { - vector args; - args.reserve(3); - args.emplace_back("pre_checkout_query_id", preCheckoutQueryId); - args.emplace_back("ok", ok); - if (!errorMessage.empty()) { - args.emplace_back("error_message", errorMessage); + if (width) { + args.emplace_back("width", width); } - return sendRequest("answerPreCheckoutQuery", args).get("", false); -} - - -Message::Ptr Api::sendSticker(std::int64_t chatId, boost::variant sticker, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const { - vector args; - args.reserve(5); - args.emplace_back("chat_id", chatId); - if (sticker.which() == 0 /* InputFile::Ptr */) { - auto file = boost::get(sticker); - args.emplace_back("sticker", file->data, true, file->mimeType, file->fileName); + if (height) { + args.emplace_back("height", height); + } + if (thumb.which() == 0 /* InputFile::Ptr */) { + auto file = boost::get(thumb); + args.emplace_back("thumb", file->data, true, file->mimeType, file->fileName); } else /* std::string */ { - args.emplace_back("sticker", boost::get(sticker)); + auto thumbStr = boost::get(thumb); + if (!thumbStr.empty()) { + args.emplace_back("thumb", thumbStr); + } + } + if (!caption.empty()) { + args.emplace_back("caption", caption); } if (replyToMessageId) { args.emplace_back("reply_to_message_id", replyToMessageId); @@ -294,223 +334,44 @@ Message::Ptr Api::sendSticker(std::int64_t chatId, boost::variant args; - args.reserve(1); - args.emplace_back("name", name); - return _tgTypeParser.parseJsonAndGetStickerSet(sendRequest("getStickerSet", args)); -} - -File::Ptr Api::uploadStickerFile(std::int64_t userId, const InputFile::Ptr pngSticker) const { - vector args; - args.reserve(2); - args.emplace_back("user_id", userId); - args.emplace_back("png_sticker", pngSticker->data, true, pngSticker->mimeType, pngSticker->fileName); - return _tgTypeParser.parseJsonAndGetFile(sendRequest("uploadStickerFile", args)); + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendAnimation", args)); } -bool Api::createNewStickerSet(std::int64_t userId, const std::string& name, const std::string& title, - const std::string& emojis, boost::variant pngSticker, boost::variant tgsSticker, - bool containsMasks, MaskPosition::Ptr maskPosition) const { +Message::Ptr Api::sendVoice(std::int64_t chatId, boost::variant voice, const string& caption, int duration, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector args; args.reserve(8); - - args.emplace_back("user_id", userId); - args.emplace_back("name", name); - args.emplace_back("title", title); - args.emplace_back("emojis", emojis); - if (pngSticker.which() == 0 /* InputFile::Ptr */) { - auto file = boost::get(pngSticker); - args.emplace_back("png_sticker", file->data, true, file->mimeType, file->fileName); + args.emplace_back("chat_id", chatId); + if (voice.which() == 0 /* InputFile::Ptr */) { + auto file = boost::get(voice); + args.emplace_back("voice", file->data, true, file->mimeType, file->fileName); } else /* std::string */ { - args.emplace_back("png_sticker", boost::get(pngSticker)); + args.emplace_back("voice", boost::get(voice)); } - if (tgsSticker.which() == 0 /* InputFile::Ptr */) { - auto file = boost::get(tgsSticker); - args.emplace_back("tgs_sticker", file->data, true, file->mimeType, file->fileName); - } else /* std::string */ { - args.emplace_back("tgs_sticker", boost::get(tgsSticker)); + if (!caption.empty()) { + args.emplace_back("caption", caption); } - if (containsMasks) { - args.emplace_back("contains_mask", containsMasks); + if (duration) { + args.emplace_back("duration", duration); } - if (maskPosition != nullptr) { - args.emplace_back("mask_position", _tgTypeParser.parseMaskPosition(maskPosition)); + if (replyToMessageId) { + args.emplace_back("reply_to_message_id", replyToMessageId); } - - return sendRequest("createNewStickerSet", args).get("", false); -} - -bool Api::createNewStickerSet(std::int64_t userId, const std::string& name, const std::string& title, - boost::variant pngSticker, const std::string& emojis, bool containsMasks, - MaskPosition::Ptr maskPosition) const { - - return createNewStickerSet(userId, name, title, emojis, pngSticker, "", containsMasks, maskPosition); -} - -bool Api::addStickerToSet(std::int64_t userId, const std::string& name, const std::string& emojis, - boost::variant pngSticker, boost::variant tgsSticker, MaskPosition::Ptr maskPosition) const { - vector args; - args.reserve(6); - - args.emplace_back("user_id", userId); - args.emplace_back("name", name); - args.emplace_back("emojis", emojis); - if (pngSticker.which() == 0 /* InputFile::Ptr */) { - auto file = boost::get(pngSticker); - args.emplace_back("png_sticker", file->data, true, file->mimeType, file->fileName); - } else /* std::string */ { - args.emplace_back("png_sticker", boost::get(pngSticker)); - } - if (tgsSticker.which() == 0 /* InputFile::Ptr */) { - auto file = boost::get(tgsSticker); - args.emplace_back("tgs_sticker", file->data, true, file->mimeType, file->fileName); - } else /* std::string */ { - args.emplace_back("tgs_sticker", boost::get(tgsSticker)); - } - if (maskPosition != nullptr) { - args.emplace_back("mask_position", _tgTypeParser.parseMaskPosition(maskPosition)); - } - - return sendRequest("addStickerToSet", args).get("", false); -} - -bool Api::addStickerToSet(std::int64_t userId, const std::string& name, boost::variant pngSticker, - const std::string& emojis, MaskPosition::Ptr maskPosition) const { - - return addStickerToSet(userId, name, emojis, pngSticker, "", maskPosition); -} - -bool Api::setStickerPositionInSet(const string& sticker, std::uint32_t position) const { - vector args; - args.reserve(2); - args.emplace_back("sticker", sticker); - args.emplace_back("position", position); - return sendRequest("setStickerPositionInSet", args).get("", false); -} - -bool Api::deleteStickerFromSet(const string& sticker) const { - vector args; - args.reserve(1); - args.emplace_back("sticker", sticker); - return sendRequest("deleteStickerFromSet", args).get("", false); -} - -bool Api::setStickerSetThumb(const std::string& name, std::int64_t userId, boost::variant thumb) const { - vector args; - args.reserve(3); - - args.emplace_back("name", name); - args.emplace_back("user_id", userId); - if (thumb.which() == 0 /* InputFile::Ptr */) { - auto file = boost::get(thumb); - args.emplace_back("thumb", file->data, true, file->mimeType, file->fileName); - } else /* std::string */ { - args.emplace_back("thumb", boost::get(thumb)); - } - - return sendRequest("setStickerSetThumb", args).get("", false); -} - -Message::Ptr Api::sendVideo(std::int64_t chatId, boost::variant video, bool supportsStreaming, std::int32_t duration, std::int32_t width, std::int32_t height, boost::variant thumb, const string &caption, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { - vector args; - args.reserve(12); - args.emplace_back("chat_id", chatId); - if (video.which() == 0 /* InputFile::Ptr */) { - auto file = boost::get(video); - args.emplace_back("video", file->data, true, file->mimeType, file->fileName); - } else /* std::string */ { - args.emplace_back("video", boost::get(video)); - } - if (supportsStreaming) { - args.emplace_back("supports_streaming", supportsStreaming); - } - if (duration) { - args.emplace_back("duration", duration); - } - if (width) { - args.emplace_back("width", width); - } - if (height) { - args.emplace_back("height", height); - } - if (thumb.which() == 0 /* InputFile::Ptr */) { - auto file = boost::get(thumb); - args.emplace_back("thumb", file->data, true, file->mimeType, file->fileName); - } else /* std::string */ { - auto thumbStr = boost::get(thumb); - if (!thumbStr.empty()) { - args.emplace_back("thumb", thumbStr); - } - } - if (!caption.empty()) { - args.emplace_back("caption", caption); - } - if (replyToMessageId) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); - } - if (!parseMode.empty()) { - args.emplace_back("parse_mode", parseMode); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendVideo", args)); -} - -Message::Ptr Api::sendAnimation(std::int64_t chatId, boost::variant animation, std::int32_t duration, std::int32_t width, std::int32_t height, boost::variant thumb, const string& caption, std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { - vector args; - args.reserve(11); - args.emplace_back("chat_id", chatId); - if (animation.which() == 0 /* InputFile::Ptr */) { - auto file = boost::get(animation); - args.emplace_back("animation", file->data, true, file->mimeType, file->fileName); - } else /* std::string */ { - args.emplace_back("animation", boost::get(animation)); - } - if (duration) { - args.emplace_back("duration", duration); - } - if (width) { - args.emplace_back("width", width); - } - if (height) { - args.emplace_back("height", height); - } - if (thumb.which() == 0 /* InputFile::Ptr */) { - auto file = boost::get(thumb); - args.emplace_back("thumb", file->data, true, file->mimeType, file->fileName); - } else /* std::string */ { - auto thumbStr = boost::get(thumb); - if (!thumbStr.empty()) { - args.emplace_back("thumb", thumbStr); - } - } - if (!caption.empty()) { - args.emplace_back("caption", caption); - } - if (replyToMessageId) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); - } - if (!parseMode.empty()) { - args.emplace_back("parse_mode", parseMode); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendAnimation", args)); + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + if (!parseMode.empty()) { + args.emplace_back("parse_mode", parseMode); + } + if (disableNotification) { + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendVoice", args)); } Message::Ptr Api::sendVideoNote(std::int64_t chatId, boost::variant videoNote, std::int64_t replyToMessageId, bool disableNotification, std::int32_t duration, std::int32_t length, boost::variant thumb, const GenericReply::Ptr replyMarkup) const { @@ -561,54 +422,6 @@ vector Api::sendMediaGroup(std::int64_t chatId, const vector(&TgTypeParser::parseJsonAndGetMessage, sendRequest("sendMediaGroup", args)); } -Message::Ptr Api::sendVoice(std::int64_t chatId, boost::variant voice, const string &caption, int duration, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { - vector args; - args.reserve(8); - args.emplace_back("chat_id", chatId); - if (voice.which() == 0 /* InputFile::Ptr */) { - auto file = boost::get(voice); - args.emplace_back("voice", file->data, true, file->mimeType, file->fileName); - } else /* std::string */ { - args.emplace_back("voice", boost::get(voice)); - } - if (!caption.empty()) { - args.emplace_back("caption", caption); - } - if (duration){ - args.emplace_back("duration", duration); - } - if (replyToMessageId) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); - } - if (!parseMode.empty()) { - args.emplace_back("parse_mode", parseMode); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendVoice", args)); -} - -Message::Ptr Api::sendGame(std::int64_t chatId, const std::string& gameShortName, std::int32_t replyToMessageId, const InlineKeyboardMarkup::Ptr replyMarkup, bool disableNotification) const { - vector args; - args.reserve(5); - args.emplace_back("chat_id", chatId); - args.emplace_back("game_short_name", gameShortName); - if (replyToMessageId) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendGame", args)); -} - Message::Ptr Api::sendLocation(std::int64_t chatId, float latitude, float longitude, std::uint32_t livePeriod, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const { vector args; args.reserve(7); @@ -624,7 +437,7 @@ Message::Ptr Api::sendLocation(std::int64_t chatId, float latitude, float longit if (replyMarkup) { args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); } - if (disableNotification){ + if (disableNotification) { args.emplace_back("disable_notification", disableNotification); } return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendLocation", args)); @@ -668,21 +481,6 @@ Message::Ptr Api::stopMessageLiveLocation(std::int64_t chatId, std::int32_t mess return _tgTypeParser.parseJsonAndGetMessage(sendRequest("editMessageLiveLocation", args)); } -bool Api::setChatStickerSet(std::int64_t chatId, const string& stickerSetName) const { - vector args; - args.reserve(2); - args.emplace_back("chat_id", chatId); - args.emplace_back("sticker_set_name ", stickerSetName); - return sendRequest("setChatStickerSet", args).get("", false); -} - -bool Api::deleteChatStickerSet(std::int64_t chatId) const { - vector args; - args.reserve(1); - args.emplace_back("chat_id", chatId); - return sendRequest("deleteChatStickerSet", args).get("", false); -} - Message::Ptr Api::sendVenue(std::int64_t chatId, float latitude, float longitude, const string& title, const string& address, const string& foursquareId, const string& foursquareType, bool disableNotification, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup) const { vector args; args.reserve(10); @@ -703,7 +501,7 @@ Message::Ptr Api::sendVenue(std::int64_t chatId, float latitude, float longitude if (replyMarkup) { args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); } - if (disableNotification){ + if (disableNotification) { args.emplace_back("disable_notification", disableNotification); } return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendVenue", args)); @@ -727,290 +525,114 @@ Message::Ptr Api::sendContact(std::int64_t chatId, const string& phoneNumber, co if (replyMarkup) { args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); } - if (disableNotification){ + if (disableNotification) { args.emplace_back("disable_notification", disableNotification); } return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendContact", args)); } -void Api::sendChatAction(std::int64_t chatId, const string& action) const { - vector args; - args.reserve(2); - args.emplace_back("chat_id", chatId); - args.emplace_back("action", action); - sendRequest("sendChatAction", args); -} - -UserProfilePhotos::Ptr Api::getUserProfilePhotos(std::int64_t userId, std::int32_t offset, std::int32_t limit) const { - vector args; - args.reserve(3); - args.emplace_back("user_id", userId); - if (offset) { - args.emplace_back("offset", offset); - } - limit = max(1, min(100, limit)); - args.emplace_back("limit", limit); - return _tgTypeParser.parseJsonAndGetUserProfilePhotos(sendRequest("getUserProfilePhotos", args)); -} - -File::Ptr Api::getFile(const string &fileId) const { +Message::Ptr Api::sendPoll(std::int64_t chatId, const std::string& question, const std::vector& options, + bool isAnonymous, const std::string& type, bool allowsMultipleAnswers, + std::int32_t correctOptionId, bool isClosed, bool disableNotification, + std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup) const { vector args; - args.reserve(1); - args.emplace_back("file_id", fileId); - return _tgTypeParser.parseJsonAndGetFile(sendRequest("getFile", args)); -} + args.reserve(11); -bool Api::leaveChat(std::int64_t chatId) const { - vector args; - args.reserve(1); args.emplace_back("chat_id", chatId); - return sendRequest("leaveChat", args).get("", false); -} - -Chat::Ptr Api::getChat(std::int64_t chatId) const { - vector args; - args.reserve(1); - args.emplace_back("chat_id", chatId); - return _tgTypeParser.parseJsonAndGetChat(sendRequest("getChat", args)); -} - -vector Api::getChatAdministrators(std::int64_t chatId) const { - vector args; - args.reserve(1); - args.emplace_back("chat_id", chatId); - return _tgTypeParser.parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetChatMember, sendRequest("getChatAdministrators", args)); -} - -int32_t Api::getChatMembersCount(std::int64_t chatId) const { - vector args; - args.reserve(1); - args.emplace_back("chat_id", chatId); - return sendRequest("getChatMembersCount", args).get("", 0); -} - -bool Api::answerCallbackQuery(const string & callbackQueryId, const string & text, bool showAlert, const string &url, std::int32_t cacheTime) const { - vector args; - args.reserve(5); - args.emplace_back("callback_query_id", callbackQueryId); - if (!text.empty()) { - args.emplace_back("text", text); - } - if (showAlert) { - args.emplace_back("show_alert", showAlert); - } - if (!url.empty()) { - args.emplace_back("url", url); + args.emplace_back("question", question); + args.emplace_back("options", _tgTypeParser.parseArray([] (const std::string& option) -> std::string { + return StringTools::urlEncode(option); + }, options)); + if (!isAnonymous) { + args.emplace_back("is_anonymous", isAnonymous); } - if (cacheTime) { - args.emplace_back("cache_time", cacheTime); + if (!type.empty()) { + args.emplace_back("type", type); } - return sendRequest("answerCallbackQuery", args).get("", false); -} - -Message::Ptr Api::editMessageText(const string& text, std::int64_t chatId, std::int32_t messageId, const string& inlineMessageId, - const string& parseMode, bool disableWebPagePreview, const GenericReply::Ptr replyMarkup) const { - - vector args; - args.reserve(7); - args.emplace_back("text", text); - if (chatId) { - args.emplace_back("chat_id", chatId); + if (allowsMultipleAnswers) { + args.emplace_back("allows_multiple_answers", allowsMultipleAnswers); } - if (messageId) { - args.emplace_back("message_id", messageId); + if (correctOptionId != 0) { + args.emplace_back("correct_option_id", correctOptionId); } - if (!inlineMessageId.empty()) { - args.emplace_back("inline_message_id", inlineMessageId); + if (isClosed) { + args.emplace_back("is_closed", isClosed); } - if (!parseMode.empty()) { - args.emplace_back("parse_mode", parseMode); + if (disableNotification) { + args.emplace_back("disable_notification", disableNotification); } - if (disableWebPagePreview) { - args.emplace_back("disable_web_page_preview", disableWebPagePreview); + if (replyToMessageId != 0) { + args.emplace_back("reply_to_message_id", replyToMessageId); } if (replyMarkup) { args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); } - ptree p = sendRequest("editMessageText", args); - if (p.get_child_optional("message_id")) { - return _tgTypeParser.parseJsonAndGetMessage(p); - } else { - return nullptr; - } + + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendPoll", args)); } -Message::Ptr Api::editMessageCaption(std::int64_t chatId, std::int32_t messageId, const string& caption, - const string& inlineMessageId, const GenericReply::Ptr replyMarkup) const { +Message::Ptr Api::sendPoll(std::int64_t chatId, const std::string& question, const std::vector& options, + bool disableNotification, std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup) const { - vector args; - args.reserve(5); - if (chatId) { - args.emplace_back("chat_id", chatId); - } - if (messageId) { - args.emplace_back("message_id", messageId); - } - if (!caption.empty()) { - args.emplace_back("caption", caption); - } - if (!inlineMessageId.empty()) { - args.emplace_back("inline_message_id", inlineMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); - } - ptree p = sendRequest("editMessageCaption", args); - if (p.get_child_optional("message_id")) { - return _tgTypeParser.parseJsonAndGetMessage(p); - } else { - return nullptr; - } + return sendPoll(chatId, question, options, true, "", false, 0, false, disableNotification, replyToMessageId, replyMarkup); } -Message::Ptr Api::editMessageReplyMarkup(std::int64_t chatId, std::int32_t messageId, const string& inlineMessageId, - const GenericReply::Ptr replyMarkup) const { - +Message::Ptr Api::sendDice(std::int64_t chatId, bool disableNotification, std::int32_t replyToMessageId, + GenericReply::Ptr replyMarkup) const { vector args; args.reserve(4); - if (chatId) { - args.emplace_back("chat_id", chatId); - } - if (messageId) { - args.emplace_back("message_id", messageId); - } - if (!inlineMessageId.empty()) { - args.emplace_back("inline_message_id", inlineMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); - } - ptree p = sendRequest("editMessageReplyMarkup", args); - if (p.get_child_optional("message_id")) { - return _tgTypeParser.parseJsonAndGetMessage(p); - } else { - return nullptr; - } -} - -Message::Ptr Api::editMessageMedia(InputMedia::Ptr media, std::int64_t chatId, std::int32_t messageId, const std::string& inlineMessageId, - GenericReply::Ptr replyMarkup) const { - vector args; - args.reserve(5); - args.emplace_back("media", _tgTypeParser.parseInputMedia(media)); - if (chatId) { - args.emplace_back("chat_id", chatId); - } - if (messageId) { - args.emplace_back("message_id", messageId); + args.emplace_back("chat_id", chatId); + if (disableNotification) { + args.emplace_back("disable_notification", disableNotification); } - if (!inlineMessageId.empty()) { - args.emplace_back("inline_message_id", inlineMessageId); + if (replyToMessageId != 0) { + args.emplace_back("reply_to_message_id", replyToMessageId); } if (replyMarkup) { args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); } - ptree p = sendRequest("editMessageMedia", args); - if (p.get_child_optional("message_id")) { - return _tgTypeParser.parseJsonAndGetMessage(p); - } else { - return nullptr; - } + + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendDice", args)); } -ChatMember::Ptr Api::getChatMember(std::int64_t chatId, std::int64_t userId) const { +void Api::sendChatAction(std::int64_t chatId, const string& action) const { vector args; args.reserve(2); args.emplace_back("chat_id", chatId); - args.emplace_back("user_id", userId); - return _tgTypeParser.parseJsonAndGetChatMember(sendRequest("getChatMember", args)); + args.emplace_back("action", action); + sendRequest("sendChatAction", args); } -vector Api::getUpdates(std::int32_t offset, std::int32_t limit, std::int32_t timeout, const StringArrayPtr &allowedUpdates) const { +UserProfilePhotos::Ptr Api::getUserProfilePhotos(std::int64_t userId, std::int32_t offset, std::int32_t limit) const { vector args; - args.reserve(4); + args.reserve(3); + args.emplace_back("user_id", userId); if (offset) { args.emplace_back("offset", offset); } limit = max(1, min(100, limit)); args.emplace_back("limit", limit); - if (timeout) { - args.emplace_back("timeout", timeout); - } - if (allowedUpdates != nullptr) { - string allowedUpdatesJson = _tgTypeParser.parseArray( - [](const string &s)->string { - return s; - }, *allowedUpdates); - args.emplace_back("allowed_updates", allowedUpdatesJson); - } - return _tgTypeParser.parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetUpdate, sendRequest("getUpdates", args)); + return _tgTypeParser.parseJsonAndGetUserProfilePhotos(sendRequest("getUserProfilePhotos", args)); } -void Api::setWebhook(const string& url, const InputFile::Ptr certificate, std::int32_t maxConnection, const StringArrayPtr &allowedUpdates) const { +File::Ptr Api::getFile(const string& fileId) const { vector args; - args.reserve(4); - if (!url.empty()) { - args.emplace_back("url", url); - } - if (certificate != nullptr) { - args.emplace_back("certificate", certificate->data, true, certificate->mimeType, certificate->fileName); - } - if (maxConnection != 40) { - args.emplace_back("max_connections", maxConnection); - } - if (allowedUpdates != nullptr) { - auto allowedUpdatesJson = _tgTypeParser.parseArray( - [](const string &s)->string { - return s; - }, *allowedUpdates); - args.emplace_back("allowed_updates", allowedUpdatesJson); - } - - sendRequest("setWebhook", args); -} - -bool Api::deleteWebhook() const { - return sendRequest("deleteWebhook").get("", false); + args.reserve(1); + args.emplace_back("file_id", fileId); + return _tgTypeParser.parseJsonAndGetFile(sendRequest("getFile", args)); } -WebhookInfo::Ptr Api::getWebhookInfo() const { - ptree p = sendRequest("getWebhookInfo"); +string Api::downloadFile(const string& filePath, const std::vector& args) const { + string url(_url); + url += "/file/bot"; + url += _token; + url += "/"; + url += filePath; - if (!p.get_child_optional("url")) { - return nullptr; - } - if (p.get("url","") != string("")) { - return _tgTypeParser.parseJsonAndGetWebhookInfo(p); - } - else { - return nullptr; - } -} + string serverResponse = _httpClient.makeRequest(url, args); -bool Api::answerInlineQuery(const string& inlineQueryId, const std::vector& results, - std::int32_t cacheTime, bool isPersonal, const string& nextOffset, const string& switchPmText, const string& switchPmParameter) const { - vector args; - args.reserve(7); - args.emplace_back("inline_query_id", inlineQueryId); - string resultsJson = _tgTypeParser.parseArray(&TgTypeParser::parseInlineQueryResult, results); - args.emplace_back("results", resultsJson); - if (cacheTime) { - args.emplace_back("cache_time", cacheTime); - } - if (isPersonal) { - args.emplace_back("is_personal", isPersonal); - } - if (!nextOffset.empty()) { - args.emplace_back("next_offset", nextOffset); - } - if (!switchPmText.empty()) { - args.emplace_back("switch_pm_text", switchPmText); - } - if (!switchPmParameter.empty()) { - args.emplace_back("switch_pm_parameter", switchPmParameter); - } - return sendRequest("answerInlineQuery", args).get("", false); + return serverResponse; } bool Api::kickChatMember(std::int64_t chatId, std::int64_t userId, std::uint64_t untilDate) const { @@ -1031,6 +653,7 @@ bool Api::unbanChatMember(std::int64_t chatId, std::int64_t userId) const { args.emplace_back("user_id", userId); return sendRequest("unbanChatMember", args).get("", false); } + bool Api::restrictChatMember(std::int64_t chatId, std::int64_t userId, TgBot::ChatPermissions::Ptr permissions, std::uint64_t untilDate) const { vector args; @@ -1086,7 +709,7 @@ bool Api::setChatAdministratorCustomTitle(std::int64_t chatId, std::int64_t user return sendRequest("setChatAdministratorCustomTitle", args).get("", false); } -bool Api::setChatPermissions(std::int64_t chatId, ChatPermissions::Ptr permissions) const{ +bool Api::setChatPermissions(std::int64_t chatId, ChatPermissions::Ptr permissions) const { vector args; args.reserve(2); args.emplace_back("chat_id", chatId); @@ -1150,144 +773,531 @@ bool Api::unpinChatMessage(std::int64_t chatId) const { return sendRequest("unpinChatMessage", args).get("", false); } -Message::Ptr Api::setGameScore(std::int64_t userId, std::int32_t score, bool force, bool disableEditMessage, std::int64_t chatId, std::int32_t messageId, const std::string& inlineMessageId) const { +bool Api::leaveChat(std::int64_t chatId) const { vector args; - args.reserve(7); - args.emplace_back("user_id", userId); - args.emplace_back("score", score); - if (force) { - args.emplace_back("force", force); - } - if (disableEditMessage) { - args.emplace_back("disable_edit_message", disableEditMessage); - } - if (chatId){ - args.emplace_back("chat_id", chatId); - } - if (messageId){ - args.emplace_back("message_id", messageId); - } - if (!inlineMessageId.empty()){ - args.emplace_back("inline_message_id", inlineMessageId); - } - return _tgTypeParser.parseJsonAndGetMessage(sendRequest("setGameScore", args)); -} + args.reserve(1); + args.emplace_back("chat_id", chatId); + return sendRequest("leaveChat", args).get("", false); +} + +Chat::Ptr Api::getChat(std::int64_t chatId) const { + vector args; + args.reserve(1); + args.emplace_back("chat_id", chatId); + return _tgTypeParser.parseJsonAndGetChat(sendRequest("getChat", args)); +} + +vector Api::getChatAdministrators(std::int64_t chatId) const { + vector args; + args.reserve(1); + args.emplace_back("chat_id", chatId); + return _tgTypeParser.parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetChatMember, sendRequest("getChatAdministrators", args)); +} + +int32_t Api::getChatMembersCount(std::int64_t chatId) const { + vector args; + args.reserve(1); + args.emplace_back("chat_id", chatId); + return sendRequest("getChatMembersCount", args).get("", 0); +} + +ChatMember::Ptr Api::getChatMember(std::int64_t chatId, std::int64_t userId) const { + vector args; + args.reserve(2); + args.emplace_back("chat_id", chatId); + args.emplace_back("user_id", userId); + return _tgTypeParser.parseJsonAndGetChatMember(sendRequest("getChatMember", args)); +} + +bool Api::setChatStickerSet(std::int64_t chatId, const string& stickerSetName) const { + vector args; + args.reserve(2); + args.emplace_back("chat_id", chatId); + args.emplace_back("sticker_set_name ", stickerSetName); + return sendRequest("setChatStickerSet", args).get("", false); +} + +bool Api::deleteChatStickerSet(std::int64_t chatId) const { + vector args; + args.reserve(1); + args.emplace_back("chat_id", chatId); + return sendRequest("deleteChatStickerSet", args).get("", false); +} + +bool Api::answerCallbackQuery(const string& callbackQueryId, const string& text, bool showAlert, const string& url, std::int32_t cacheTime) const { + vector args; + args.reserve(5); + args.emplace_back("callback_query_id", callbackQueryId); + if (!text.empty()) { + args.emplace_back("text", text); + } + if (showAlert) { + args.emplace_back("show_alert", showAlert); + } + if (!url.empty()) { + args.emplace_back("url", url); + } + if (cacheTime) { + args.emplace_back("cache_time", cacheTime); + } + return sendRequest("answerCallbackQuery", args).get("", false); +} + +bool Api::setMyCommands(const std::vector& commands) const { + vector args; + args.reserve(5); + + string commandsJson = _tgTypeParser.parseArray(&TgTypeParser::parseBotCommand, commands); + args.emplace_back("commands", commandsJson); + + return sendRequest("setMyCommands", args).get("", false); +} + +std::vector Api::getMyCommands() const { + return _tgTypeParser.parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetBotCommand, sendRequest("getMyCommands")); +} + +Message::Ptr Api::editMessageText(const string& text, std::int64_t chatId, std::int32_t messageId, const string& inlineMessageId, + const string& parseMode, bool disableWebPagePreview, const GenericReply::Ptr replyMarkup) const { + + vector args; + args.reserve(7); + args.emplace_back("text", text); + if (chatId) { + args.emplace_back("chat_id", chatId); + } + if (messageId) { + args.emplace_back("message_id", messageId); + } + if (!inlineMessageId.empty()) { + args.emplace_back("inline_message_id", inlineMessageId); + } + if (!parseMode.empty()) { + args.emplace_back("parse_mode", parseMode); + } + if (disableWebPagePreview) { + args.emplace_back("disable_web_page_preview", disableWebPagePreview); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + ptree p = sendRequest("editMessageText", args); + if (p.get_child_optional("message_id")) { + return _tgTypeParser.parseJsonAndGetMessage(p); + } else { + return nullptr; + } +} + +Message::Ptr Api::editMessageCaption(std::int64_t chatId, std::int32_t messageId, const string& caption, + const string& inlineMessageId, const GenericReply::Ptr replyMarkup) const { + vector args; + args.reserve(5); + if (chatId) { + args.emplace_back("chat_id", chatId); + } + if (messageId) { + args.emplace_back("message_id", messageId); + } + if (!caption.empty()) { + args.emplace_back("caption", caption); + } + if (!inlineMessageId.empty()) { + args.emplace_back("inline_message_id", inlineMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + ptree p = sendRequest("editMessageCaption", args); + if (p.get_child_optional("message_id")) { + return _tgTypeParser.parseJsonAndGetMessage(p); + } else { + return nullptr; + } +} + +Message::Ptr Api::editMessageMedia(InputMedia::Ptr media, std::int64_t chatId, std::int32_t messageId, const std::string& inlineMessageId, + GenericReply::Ptr replyMarkup) const { + + vector args; + args.reserve(5); + args.emplace_back("media", _tgTypeParser.parseInputMedia(media)); + if (chatId) { + args.emplace_back("chat_id", chatId); + } + if (messageId) { + args.emplace_back("message_id", messageId); + } + if (!inlineMessageId.empty()) { + args.emplace_back("inline_message_id", inlineMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + ptree p = sendRequest("editMessageMedia", args); + if (p.get_child_optional("message_id")) { + return _tgTypeParser.parseJsonAndGetMessage(p); + } else { + return nullptr; + } +} + +Message::Ptr Api::editMessageReplyMarkup(std::int64_t chatId, std::int32_t messageId, const string& inlineMessageId, + const GenericReply::Ptr replyMarkup) const { + + vector args; + args.reserve(4); + if (chatId) { + args.emplace_back("chat_id", chatId); + } + if (messageId) { + args.emplace_back("message_id", messageId); + } + if (!inlineMessageId.empty()) { + args.emplace_back("inline_message_id", inlineMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + ptree p = sendRequest("editMessageReplyMarkup", args); + if (p.get_child_optional("message_id")) { + return _tgTypeParser.parseJsonAndGetMessage(p); + } else { + return nullptr; + } +} + +Poll::Ptr Api::stopPoll(std::int64_t chatId, std::int64_t messageId, const InlineKeyboardMarkup::Ptr replyMarkup) const { + vector args; + args.reserve(3); + args.emplace_back("chat_id", chatId); + args.emplace_back("message_id", messageId); + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + return _tgTypeParser.parseJsonAndGetPoll(sendRequest("stopPoll", args)); +} + +void Api::deleteMessage(std::int64_t chatId, std::int32_t messageId) const { + sendRequest("deleteMessage", { HttpReqArg("chat_id", chatId), HttpReqArg("message_id", messageId) }); +} + +Message::Ptr Api::sendSticker(std::int64_t chatId, boost::variant sticker, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const { + vector args; + args.reserve(5); + args.emplace_back("chat_id", chatId); + if (sticker.which() == 0 /* InputFile::Ptr */) { + auto file = boost::get(sticker); + args.emplace_back("sticker", file->data, true, file->mimeType, file->fileName); + } else /* std::string */ { + args.emplace_back("sticker", boost::get(sticker)); + } + if (replyToMessageId) { + args.emplace_back("reply_to_message_id", replyToMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + if (disableNotification) { + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendSticker", args)); +} + +StickerSet::Ptr Api::getStickerSet(const string& name) const { + vector args; + args.reserve(1); + args.emplace_back("name", name); + return _tgTypeParser.parseJsonAndGetStickerSet(sendRequest("getStickerSet", args)); +} + +File::Ptr Api::uploadStickerFile(std::int64_t userId, const InputFile::Ptr pngSticker) const { + vector args; + args.reserve(2); + args.emplace_back("user_id", userId); + args.emplace_back("png_sticker", pngSticker->data, true, pngSticker->mimeType, pngSticker->fileName); + return _tgTypeParser.parseJsonAndGetFile(sendRequest("uploadStickerFile", args)); +} + +bool Api::createNewStickerSet(std::int64_t userId, const std::string& name, const std::string& title, + const std::string& emojis, boost::variant pngSticker, boost::variant tgsSticker, + bool containsMasks, MaskPosition::Ptr maskPosition) const { + vector args; + args.reserve(8); + + args.emplace_back("user_id", userId); + args.emplace_back("name", name); + args.emplace_back("title", title); + args.emplace_back("emojis", emojis); + if (pngSticker.which() == 0 /* InputFile::Ptr */) { + auto file = boost::get(pngSticker); + args.emplace_back("png_sticker", file->data, true, file->mimeType, file->fileName); + } else /* std::string */ { + args.emplace_back("png_sticker", boost::get(pngSticker)); + } + if (tgsSticker.which() == 0 /* InputFile::Ptr */) { + auto file = boost::get(tgsSticker); + args.emplace_back("tgs_sticker", file->data, true, file->mimeType, file->fileName); + } else /* std::string */ { + args.emplace_back("tgs_sticker", boost::get(tgsSticker)); + } + if (containsMasks) { + args.emplace_back("contains_mask", containsMasks); + } + if (maskPosition != nullptr) { + args.emplace_back("mask_position", _tgTypeParser.parseMaskPosition(maskPosition)); + } + + return sendRequest("createNewStickerSet", args).get("", false); +} + +bool Api::createNewStickerSet(std::int64_t userId, const std::string& name, const std::string& title, + boost::variant pngSticker, const std::string& emojis, bool containsMasks, + MaskPosition::Ptr maskPosition) const { + + return createNewStickerSet(userId, name, title, emojis, pngSticker, "", containsMasks, maskPosition); +} + +bool Api::addStickerToSet(std::int64_t userId, const std::string& name, const std::string& emojis, + boost::variant pngSticker, boost::variant tgsSticker, MaskPosition::Ptr maskPosition) const { + vector args; + args.reserve(6); + + args.emplace_back("user_id", userId); + args.emplace_back("name", name); + args.emplace_back("emojis", emojis); + if (pngSticker.which() == 0 /* InputFile::Ptr */) { + auto file = boost::get(pngSticker); + args.emplace_back("png_sticker", file->data, true, file->mimeType, file->fileName); + } else /* std::string */ { + args.emplace_back("png_sticker", boost::get(pngSticker)); + } + if (tgsSticker.which() == 0 /* InputFile::Ptr */) { + auto file = boost::get(tgsSticker); + args.emplace_back("tgs_sticker", file->data, true, file->mimeType, file->fileName); + } else /* std::string */ { + args.emplace_back("tgs_sticker", boost::get(tgsSticker)); + } + if (maskPosition != nullptr) { + args.emplace_back("mask_position", _tgTypeParser.parseMaskPosition(maskPosition)); + } -vector Api::getGameHighScores(std::int64_t userId, std::int32_t score, bool force, bool disableEditMessage, std::int64_t chatId, std::int32_t messageId, const std::string& inlineMessageId) const { + return sendRequest("addStickerToSet", args).get("", false); +} + +bool Api::addStickerToSet(std::int64_t userId, const std::string& name, boost::variant pngSticker, + const std::string& emojis, MaskPosition::Ptr maskPosition) const { + + return addStickerToSet(userId, name, emojis, pngSticker, "", maskPosition); +} + +bool Api::setStickerPositionInSet(const string& sticker, std::uint32_t position) const { vector args; - args.reserve(7); + args.reserve(2); + args.emplace_back("sticker", sticker); + args.emplace_back("position", position); + return sendRequest("setStickerPositionInSet", args).get("", false); +} + +bool Api::deleteStickerFromSet(const string& sticker) const { + vector args; + args.reserve(1); + args.emplace_back("sticker", sticker); + return sendRequest("deleteStickerFromSet", args).get("", false); +} + +bool Api::setStickerSetThumb(const std::string& name, std::int64_t userId, boost::variant thumb) const { + vector args; + args.reserve(3); + + args.emplace_back("name", name); args.emplace_back("user_id", userId); - args.emplace_back("score", score); - if (force) { - args.emplace_back("force", force); + if (thumb.which() == 0 /* InputFile::Ptr */) { + auto file = boost::get(thumb); + args.emplace_back("thumb", file->data, true, file->mimeType, file->fileName); + } else /* std::string */ { + args.emplace_back("thumb", boost::get(thumb)); } - if (disableEditMessage) { - args.emplace_back("disable_edit_message", disableEditMessage); + + return sendRequest("setStickerSetThumb", args).get("", false); +} + +bool Api::answerInlineQuery(const string& inlineQueryId, const std::vector& results, + std::int32_t cacheTime, bool isPersonal, const string& nextOffset, const string& switchPmText, const string& switchPmParameter) const { + vector args; + args.reserve(7); + args.emplace_back("inline_query_id", inlineQueryId); + string resultsJson = _tgTypeParser.parseArray(&TgTypeParser::parseInlineQueryResult, results); + args.emplace_back("results", resultsJson); + if (cacheTime) { + args.emplace_back("cache_time", cacheTime); } - if (chatId) { - args.emplace_back("chat_id", chatId); + if (isPersonal) { + args.emplace_back("is_personal", isPersonal); } - if (messageId) { - args.emplace_back("message_id", messageId); + if (!nextOffset.empty()) { + args.emplace_back("next_offset", nextOffset); } - if (!inlineMessageId.empty()){ - args.emplace_back("inline_message_id", inlineMessageId); + if (!switchPmText.empty()) { + args.emplace_back("switch_pm_text", switchPmText); } - return _tgTypeParser.parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetGameHighScore, sendRequest("getGameHighScores", args)); -} - -void Api::deleteMessage(std::int64_t chatId, std::int32_t messageId) const { - sendRequest("deleteMessage", { HttpReqArg("chat_id", chatId), HttpReqArg("message_id", messageId) }); + if (!switchPmParameter.empty()) { + args.emplace_back("switch_pm_parameter", switchPmParameter); + } + return sendRequest("answerInlineQuery", args).get("", false); } -Message::Ptr Api::sendPoll(std::int64_t chatId, const std::string& question, const std::vector& options, - bool isAnonymous, const std::string& type, bool allowsMultipleAnswers, - std::int32_t correctOptionId, bool isClosed, bool disableNotification, - std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup) const { +Message::Ptr Api::sendInvoice(std::int64_t chatId, const std::string& title, 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, const std::string& photoUrl, std::int32_t photoSize, + std::int32_t photoWidth, std::int32_t photoHeight, bool needName, + bool needPhoneNumber, bool needEmail, bool needShippingAddress, + bool sendPhoneNumberToProvider, bool sendEmailToProvider, bool isFlexible, + std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const { vector args; - args.reserve(11); - + args.reserve(23); args.emplace_back("chat_id", chatId); - args.emplace_back("question", question); - args.emplace_back("options", _tgTypeParser.parseArray([] (const std::string& option) -> std::string { - return StringTools::urlEncode(option); - }, options)); - if (!isAnonymous) { - args.emplace_back("is_anonymous", isAnonymous); + 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)); + if (!providerData.empty()) { + args.emplace_back("provider_data", providerData); } - if (!type.empty()) { - args.emplace_back("type", type); + if (!photoUrl.empty()) { + args.emplace_back("photo_url", photoUrl); } - if (allowsMultipleAnswers) { - args.emplace_back("allows_multiple_answers", allowsMultipleAnswers); + if (photoSize) { + args.emplace_back("photo_size", photoSize); } - if (correctOptionId != 0) { - args.emplace_back("correct_option_id", correctOptionId); + if (photoWidth) { + args.emplace_back("photo_width", photoWidth); } - if (isClosed) { - args.emplace_back("is_closed", isClosed); + if (photoHeight) { + args.emplace_back("photo_height", photoHeight); } - if (disableNotification) { - args.emplace_back("disable_notification", disableNotification); + if (needName) { + args.emplace_back("need_name", needName); } - if (replyToMessageId != 0) { + if (needPhoneNumber) { + args.emplace_back("need_phone_number", needPhoneNumber); + } + if (needEmail) { + args.emplace_back("need_email", needEmail); + } + if (needShippingAddress) { + args.emplace_back("need_shipping_address", needShippingAddress); + } + if (sendPhoneNumberToProvider) { + args.emplace_back("send_phone_number_to_provider", sendPhoneNumberToProvider); + } + if (sendEmailToProvider) { + args.emplace_back("send_email_to_provider", sendEmailToProvider); + } + if (isFlexible) { + args.emplace_back("is_flexible", isFlexible); + } + if (replyToMessageId) { args.emplace_back("reply_to_message_id", replyToMessageId); } if (replyMarkup) { args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); } - - return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendPoll", args)); + if (disableNotification) { + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendInvoice", args)); } -Message::Ptr Api::sendPoll(std::int64_t chatId, const std::string& question, const std::vector& options, - bool disableNotification, std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup) const { - - return sendPoll(chatId, question, options, true, "", false, 0, false, disableNotification, replyToMessageId, replyMarkup); +bool Api::answerShippingQuery(const std::string& shippingQueryId, bool ok, const std::vector& shippingOptions, const std::string& errorMessage) const { + vector args; + args.reserve(4); + args.emplace_back("shipping_query_id", shippingQueryId); + args.emplace_back("ok", ok); + if (!shippingOptions.empty()) { + args.emplace_back("shipping_options", _tgTypeParser.parseArray(&TgTypeParser::parseShippingOption, shippingOptions)); + } + if (!errorMessage.empty()) { + args.emplace_back("error_message", errorMessage); + } + return sendRequest("answerShippingQuery", args).get("", false); } -Message::Ptr Api::sendDice(std::int64_t chatId, bool disableNotification, std::int32_t replyToMessageId, - GenericReply::Ptr replyMarkup) const { +bool Api::answerPreCheckoutQuery(const std::string& preCheckoutQueryId, bool ok, const std::string& errorMessage) const { vector args; - args.reserve(4); + args.reserve(3); + args.emplace_back("pre_checkout_query_id", preCheckoutQueryId); + args.emplace_back("ok", ok); + if (!errorMessage.empty()) { + args.emplace_back("error_message", errorMessage); + } + return sendRequest("answerPreCheckoutQuery", args).get("", false); +} +Message::Ptr Api::sendGame(std::int64_t chatId, const std::string& gameShortName, std::int32_t replyToMessageId, const InlineKeyboardMarkup::Ptr replyMarkup, bool disableNotification) const { + vector args; + args.reserve(5); args.emplace_back("chat_id", chatId); - if (disableNotification) { - args.emplace_back("disable_notification", disableNotification); - } - if (replyToMessageId != 0) { + args.emplace_back("game_short_name", gameShortName); + if (replyToMessageId) { args.emplace_back("reply_to_message_id", replyToMessageId); } if (replyMarkup) { args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); } - - return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendDice", args)); + if (disableNotification) { + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendGame", args)); } -Poll::Ptr Api::stopPoll(std::int64_t chatId, std::int64_t messageId, const InlineKeyboardMarkup::Ptr replyMarkup) const { +Message::Ptr Api::setGameScore(std::int64_t userId, std::int32_t score, bool force, bool disableEditMessage, std::int64_t chatId, std::int32_t messageId, const std::string& inlineMessageId) const { vector args; - args.reserve(3); - args.emplace_back("chat_id", chatId); - args.emplace_back("message_id", messageId); - if (replyMarkup){ - args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + args.reserve(7); + args.emplace_back("user_id", userId); + args.emplace_back("score", score); + if (force) { + args.emplace_back("force", force); } - return _tgTypeParser.parseJsonAndGetPoll(sendRequest("stopPoll", args)); + if (disableEditMessage) { + args.emplace_back("disable_edit_message", disableEditMessage); + } + if (chatId) { + args.emplace_back("chat_id", chatId); + } + if (messageId) { + args.emplace_back("message_id", messageId); + } + if (!inlineMessageId.empty()) { + args.emplace_back("inline_message_id", inlineMessageId); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("setGameScore", args)); } -bool Api::setMyCommands(const std::vector& commands) const { +vector Api::getGameHighScores(std::int64_t userId, std::int32_t score, bool force, bool disableEditMessage, std::int64_t chatId, std::int32_t messageId, const std::string& inlineMessageId) const { vector args; - args.reserve(5); - - string commandsJson = _tgTypeParser.parseArray(&TgTypeParser::parseBotCommand, commands); - args.emplace_back("commands", commandsJson); - - return sendRequest("setMyCommands",args).get("",false); -} - -std::vector Api::getMyCommands() const { - return _tgTypeParser.parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetBotCommand, sendRequest("getMyCommands")); + args.reserve(7); + args.emplace_back("user_id", userId); + args.emplace_back("score", score); + if (force) { + args.emplace_back("force", force); + } + if (disableEditMessage) { + args.emplace_back("disable_edit_message", disableEditMessage); + } + if (chatId) { + args.emplace_back("chat_id", chatId); + } + if (messageId) { + args.emplace_back("message_id", messageId); + } + if (!inlineMessageId.empty()) { + args.emplace_back("inline_message_id", inlineMessageId); + } + return _tgTypeParser.parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetGameHighScore, sendRequest("getGameHighScores", args)); } ptree Api::sendRequest(const string& method, const vector& args) const { @@ -1313,19 +1323,4 @@ ptree Api::sendRequest(const string& method, const vector& args) con throw TgException("tgbot-cpp library can't parse json response. " + string(e.what())); } } - -string Api::downloadFile(const string& filePath, const std::vector& args) const { - string url(_url); - url += "/file/bot"; - url += _token; - url += "/"; - url += filePath; - - string serverResponse = _httpClient.makeRequest(url, args); - - return serverResponse; -} - - - } -- cgit v1.2.3