diff options
author | llnulldisk <48621230+llnulldisk@users.noreply.github.com> | 2022-08-30 19:34:12 +0200 |
---|---|---|
committer | llnulldisk <48621230+llnulldisk@users.noreply.github.com> | 2022-08-30 19:34:12 +0200 |
commit | 4aa6af3180aef70f9a4320a2f6d818f68f5bb669 (patch) | |
tree | 14879274b4640594954a6c4c7acfb2277f62c512 /src/Api.cpp | |
parent | eb1de0a6148c88510be63ac57f1c03fddb6cb395 (diff) |
Sort api methods
Diffstat (limited to 'src/Api.cpp')
-rw-r--r-- | src/Api.cpp | 1179 |
1 files changed, 587 insertions, 592 deletions
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<Update::Ptr> Api::getUpdates(std::int32_t offset, std::int32_t limit, std::int32_t timeout, const StringArrayPtr& allowedUpdates) const { + vector<HttpReqArg> 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<string>( + [] (const string& s)->string { + return s; + }, *allowedUpdates); + args.emplace_back("allowed_updates", allowedUpdatesJson); + } + return _tgTypeParser.parseJsonAndGetArray<Update>(&TgTypeParser::parseJsonAndGetUpdate, sendRequest("getUpdates", args)); +} + +void Api::setWebhook(const string& url, const InputFile::Ptr certificate, std::int32_t maxConnection, const StringArrayPtr& allowedUpdates) const { + vector<HttpReqArg> 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<string>( + [] (const string& s)->string { + return s; + }, *allowedUpdates); + args.emplace_back("allowed_updates", allowedUpdatesJson); + } + + sendRequest("setWebhook", args); +} + +bool Api::deleteWebhook() const { + return sendRequest("deleteWebhook").get<bool>("", false); +} + +WebhookInfo::Ptr Api::getWebhookInfo() const { + ptree p = sendRequest("getWebhookInfo"); + + if (!p.get_child_optional("url")) { + return nullptr; + } + if (p.get<string>("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<std::int64_t, const std::string&> 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<std::int64_t, const std::string&> 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<InputFile::Ptr, if (!parseMode.empty()) { args.emplace_back("parse_mode", parseMode); } - if (disableNotification){ + if (disableNotification) { args.emplace_back("disable_notification", disableNotification); } return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendPhoto", args)); } -Message::Ptr Api::sendAudio(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> audio, const string &caption, std::int32_t duration, const string& performer, const string& title, boost::variant<InputFile::Ptr, std::string> 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<InputFile::Ptr, std::string> audio, const string& caption, std::int32_t duration, const string& performer, const string& title, boost::variant<InputFile::Ptr, std::string> thumb, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector<HttpReqArg> args; args.reserve(11); args.emplace_back("chat_id", chatId); @@ -120,10 +181,10 @@ Message::Ptr Api::sendAudio(std::int64_t chatId, boost::variant<InputFile::Ptr, if (duration) { args.emplace_back("duration", duration); } - if (!performer.empty()){ + if (!performer.empty()) { args.emplace_back("performer", performer); } - if (!title.empty()){ + if (!title.empty()) { args.emplace_back("title", title); } if (thumb.which() == 0 /* InputFile::Ptr */) { @@ -144,13 +205,13 @@ Message::Ptr Api::sendAudio(std::int64_t chatId, boost::variant<InputFile::Ptr, if (!parseMode.empty()) { args.emplace_back("parse_mode", parseMode); } - if (disableNotification){ + if (disableNotification) { args.emplace_back("disable_notification", disableNotification); } return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendAudio", args)); } -Message::Ptr Api::sendDocument(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> document, boost::variant<InputFile::Ptr, std::string> 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<InputFile::Ptr, std::string> document, boost::variant<InputFile::Ptr, std::string> thumb, const string& caption, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector<HttpReqArg> args; args.reserve(8); args.emplace_back("chat_id", chatId); @@ -181,244 +242,13 @@ Message::Ptr Api::sendDocument(std::int64_t chatId, boost::variant<InputFile::Pt if (!parseMode.empty()) { args.emplace_back("parse_mode", parseMode); } - if (disableNotification){ + if (disableNotification) { args.emplace_back("disable_notification", disableNotification); } return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendDocument", args)); } -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<LabeledPrice::Ptr>& 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<HttpReqArg> args; - args.reserve(23); - 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<LabeledPrice>(&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 (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)); - } - if (disableNotification){ - args.emplace_back("disable_notification", disableNotification); - } - return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendInvoice", args)); -} - -bool Api::answerShippingQuery(const std::string& shippingQueryId, bool ok, const std::vector<ShippingOption::Ptr>& shippingOptions, const std::string& errorMessage) const { - vector<HttpReqArg> 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<ShippingOption>(&TgTypeParser::parseShippingOption, shippingOptions)); - } - if (!errorMessage.empty()) { - args.emplace_back("error_message", errorMessage); - } - return sendRequest("answerShippingQuery", args).get<bool>("", false); -} - -bool Api::answerPreCheckoutQuery(const std::string& preCheckoutQueryId, bool ok, const std::string& errorMessage) const { - vector<HttpReqArg> 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); - } - return sendRequest("answerPreCheckoutQuery", args).get<bool>("", false); -} - - -Message::Ptr Api::sendSticker(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> sticker, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const { - vector<HttpReqArg> args; - args.reserve(5); - args.emplace_back("chat_id", chatId); - if (sticker.which() == 0 /* InputFile::Ptr */) { - auto file = boost::get<InputFile::Ptr>(sticker); - args.emplace_back("sticker", file->data, true, file->mimeType, file->fileName); - } else /* std::string */ { - args.emplace_back("sticker", boost::get<std::string>(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<HttpReqArg> 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<HttpReqArg> 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<InputFile::Ptr, std::string> pngSticker, boost::variant<InputFile::Ptr, std::string> tgsSticker, - bool containsMasks, MaskPosition::Ptr maskPosition) const { - vector<HttpReqArg> 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<InputFile::Ptr>(pngSticker); - args.emplace_back("png_sticker", file->data, true, file->mimeType, file->fileName); - } else /* std::string */ { - args.emplace_back("png_sticker", boost::get<std::string>(pngSticker)); - } - if (tgsSticker.which() == 0 /* InputFile::Ptr */) { - auto file = boost::get<InputFile::Ptr>(tgsSticker); - args.emplace_back("tgs_sticker", file->data, true, file->mimeType, file->fileName); - } else /* std::string */ { - args.emplace_back("tgs_sticker", boost::get<std::string>(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<bool>("", false); -} - -bool Api::createNewStickerSet(std::int64_t userId, const std::string& name, const std::string& title, - boost::variant<InputFile::Ptr, std::string> 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<InputFile::Ptr, std::string> pngSticker, boost::variant<InputFile::Ptr, std::string> tgsSticker, MaskPosition::Ptr maskPosition) const { - vector<HttpReqArg> 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<InputFile::Ptr>(pngSticker); - args.emplace_back("png_sticker", file->data, true, file->mimeType, file->fileName); - } else /* std::string */ { - args.emplace_back("png_sticker", boost::get<std::string>(pngSticker)); - } - if (tgsSticker.which() == 0 /* InputFile::Ptr */) { - auto file = boost::get<InputFile::Ptr>(tgsSticker); - args.emplace_back("tgs_sticker", file->data, true, file->mimeType, file->fileName); - } else /* std::string */ { - args.emplace_back("tgs_sticker", boost::get<std::string>(tgsSticker)); - } - if (maskPosition != nullptr) { - args.emplace_back("mask_position", _tgTypeParser.parseMaskPosition(maskPosition)); - } - - return sendRequest("addStickerToSet", args).get<bool>("", false); -} - -bool Api::addStickerToSet(std::int64_t userId, const std::string& name, boost::variant<InputFile::Ptr, std::string> 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<HttpReqArg> args; - args.reserve(2); - args.emplace_back("sticker", sticker); - args.emplace_back("position", position); - return sendRequest("setStickerPositionInSet", args).get<bool>("", false); -} - -bool Api::deleteStickerFromSet(const string& sticker) const { - vector<HttpReqArg> args; - args.reserve(1); - args.emplace_back("sticker", sticker); - return sendRequest("deleteStickerFromSet", args).get<bool>("", false); -} - -bool Api::setStickerSetThumb(const std::string& name, std::int64_t userId, boost::variant<InputFile::Ptr, std::string> thumb) const { - vector<HttpReqArg> 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<InputFile::Ptr>(thumb); - args.emplace_back("thumb", file->data, true, file->mimeType, file->fileName); - } else /* std::string */ { - args.emplace_back("thumb", boost::get<std::string>(thumb)); - } - - return sendRequest("setStickerSetThumb", args).get<bool>("", false); -} - -Message::Ptr Api::sendVideo(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> video, bool supportsStreaming, std::int32_t duration, std::int32_t width, std::int32_t height, boost::variant<InputFile::Ptr, std::string> thumb, const string &caption, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { +Message::Ptr Api::sendVideo(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> video, bool supportsStreaming, std::int32_t duration, std::int32_t width, std::int32_t height, boost::variant<InputFile::Ptr, std::string> thumb, const string& caption, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector<HttpReqArg> args; args.reserve(12); args.emplace_back("chat_id", chatId); @@ -461,7 +291,7 @@ Message::Ptr Api::sendVideo(std::int64_t chatId, boost::variant<InputFile::Ptr, if (!parseMode.empty()) { args.emplace_back("parse_mode", parseMode); } - if (disableNotification){ + if (disableNotification) { args.emplace_back("disable_notification", disableNotification); } return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendVideo", args)); @@ -507,12 +337,43 @@ Message::Ptr Api::sendAnimation(std::int64_t chatId, boost::variant<InputFile::P if (!parseMode.empty()) { args.emplace_back("parse_mode", parseMode); } - if (disableNotification){ + if (disableNotification) { args.emplace_back("disable_notification", disableNotification); } return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendAnimation", args)); } +Message::Ptr Api::sendVoice(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> voice, const string& caption, int duration, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { + vector<HttpReqArg> args; + args.reserve(8); + args.emplace_back("chat_id", chatId); + if (voice.which() == 0 /* InputFile::Ptr */) { + auto file = boost::get<InputFile::Ptr>(voice); + args.emplace_back("voice", file->data, true, file->mimeType, file->fileName); + } else /* std::string */ { + args.emplace_back("voice", boost::get<std::string>(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::sendVideoNote(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> videoNote, std::int64_t replyToMessageId, bool disableNotification, std::int32_t duration, std::int32_t length, boost::variant<InputFile::Ptr, std::string> thumb, const GenericReply::Ptr replyMarkup) const { vector<HttpReqArg> args; args.reserve(8); @@ -561,54 +422,6 @@ vector<Message::Ptr> Api::sendMediaGroup(std::int64_t chatId, const vector<Input return _tgTypeParser.parseJsonAndGetArray<Message>(&TgTypeParser::parseJsonAndGetMessage, sendRequest("sendMediaGroup", args)); } -Message::Ptr Api::sendVoice(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> voice, const string &caption, int duration, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { - vector<HttpReqArg> args; - args.reserve(8); - args.emplace_back("chat_id", chatId); - if (voice.which() == 0 /* InputFile::Ptr */) { - auto file = boost::get<InputFile::Ptr>(voice); - args.emplace_back("voice", file->data, true, file->mimeType, file->fileName); - } else /* std::string */ { - args.emplace_back("voice", boost::get<std::string>(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<HttpReqArg> 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<HttpReqArg> 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<HttpReqArg> args; - args.reserve(2); - args.emplace_back("chat_id", chatId); - args.emplace_back("sticker_set_name ", stickerSetName); - return sendRequest("setChatStickerSet", args).get<bool>("", false); -} - -bool Api::deleteChatStickerSet(std::int64_t chatId) const { - vector<HttpReqArg> args; - args.reserve(1); - args.emplace_back("chat_id", chatId); - return sendRequest("deleteChatStickerSet", args).get<bool>("", 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<HttpReqArg> 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,12 +525,77 @@ 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)); } +Message::Ptr Api::sendPoll(std::int64_t chatId, const std::string& question, const std::vector<std::string>& 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<HttpReqArg> args; + args.reserve(11); + + args.emplace_back("chat_id", chatId); + args.emplace_back("question", question); + args.emplace_back("options", _tgTypeParser.parseArray<std::string>([] (const std::string& option) -> std::string { + return StringTools::urlEncode(option); + }, options)); + if (!isAnonymous) { + args.emplace_back("is_anonymous", isAnonymous); + } + if (!type.empty()) { + args.emplace_back("type", type); + } + if (allowsMultipleAnswers) { + args.emplace_back("allows_multiple_answers", allowsMultipleAnswers); + } + if (correctOptionId != 0) { + args.emplace_back("correct_option_id", correctOptionId); + } + if (isClosed) { + args.emplace_back("is_closed", isClosed); + } + if (disableNotification) { + args.emplace_back("disable_notification", disableNotification); + } + if (replyToMessageId != 0) { + args.emplace_back("reply_to_message_id", replyToMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendPoll", args)); +} + +Message::Ptr Api::sendPoll(std::int64_t chatId, const std::string& question, const std::vector<std::string>& options, + bool disableNotification, std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup) const { + + return sendPoll(chatId, question, options, true, "", false, 0, false, disableNotification, replyToMessageId, replyMarkup); +} + +Message::Ptr Api::sendDice(std::int64_t chatId, bool disableNotification, std::int32_t replyToMessageId, + GenericReply::Ptr replyMarkup) const { + vector<HttpReqArg> args; + args.reserve(4); + + args.emplace_back("chat_id", chatId); + if (disableNotification) { + args.emplace_back("disable_notification", disableNotification); + } + if (replyToMessageId != 0) { + args.emplace_back("reply_to_message_id", replyToMessageId); + } + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendDice", args)); +} + void Api::sendChatAction(std::int64_t chatId, const string& action) const { vector<HttpReqArg> args; args.reserve(2); @@ -753,13 +616,163 @@ UserProfilePhotos::Ptr Api::getUserProfilePhotos(std::int64_t userId, std::int32 return _tgTypeParser.parseJsonAndGetUserProfilePhotos(sendRequest("getUserProfilePhotos", args)); } -File::Ptr Api::getFile(const string &fileId) const { +File::Ptr Api::getFile(const string& fileId) const { vector<HttpReqArg> args; args.reserve(1); args.emplace_back("file_id", fileId); return _tgTypeParser.parseJsonAndGetFile(sendRequest("getFile", args)); } +string Api::downloadFile(const string& filePath, const std::vector<HttpReqArg>& args) const { + string url(_url); + url += "/file/bot"; + url += _token; + url += "/"; + url += filePath; + + string serverResponse = _httpClient.makeRequest(url, args); + + return serverResponse; +} + +bool Api::kickChatMember(std::int64_t chatId, std::int64_t userId, std::uint64_t untilDate) const { + vector<HttpReqArg> args; + args.reserve(3); + args.emplace_back("chat_id", chatId); + args.emplace_back("user_id", userId); + if (untilDate) { + args.emplace_back("until_date", untilDate); + } + return sendRequest("kickChatMember", args).get<bool>("", false); +} + +bool Api::unbanChatMember(std::int64_t chatId, std::int64_t userId) const { + vector<HttpReqArg> args; + args.reserve(2); + args.emplace_back("chat_id", chatId); + args.emplace_back("user_id", userId); + return sendRequest("unbanChatMember", args).get<bool>("", false); +} + +bool Api::restrictChatMember(std::int64_t chatId, std::int64_t userId, TgBot::ChatPermissions::Ptr permissions, + std::uint64_t untilDate) const { + vector<HttpReqArg> args; + args.reserve(4); + args.emplace_back("chat_id", chatId); + args.emplace_back("user_id", userId); + args.emplace_back("permissions", _tgTypeParser.parseChatPermissions(permissions)); + if (untilDate) { + args.emplace_back("until_date", untilDate); + } + + return sendRequest("restrictChatMember", args).get<bool>("", false); +} + +bool Api::promoteChatMember(std::int64_t chatId, std::int64_t userId, bool canChangeInfo, bool canPostMessages, + bool canEditMessages, bool canDeleteMessages, bool canInviteUsers, bool canPinMessages, bool canPromoteMembers) const { + vector<HttpReqArg> args; + args.reserve(9); + args.emplace_back("chat_id", chatId); + args.emplace_back("user_id", userId); + if (canChangeInfo) { + args.emplace_back("can_change_info", canChangeInfo); + } + if (canPostMessages) { + args.emplace_back("can_post_messages", canPostMessages); + } + if (canEditMessages) { + args.emplace_back("can_edit_messages", canEditMessages); + } + if (canDeleteMessages) { + args.emplace_back("can_delete_messages", canDeleteMessages); + } + if (canInviteUsers) { + args.emplace_back("can_invite_users", canInviteUsers); + } + if (canPinMessages) { + args.emplace_back("can_pin_messages", canPinMessages); + } + if (canPromoteMembers) { + args.emplace_back("can_promote_members", canPromoteMembers); + } + return sendRequest("promoteChatMember", args).get<bool>("", false); +} + +bool Api::setChatAdministratorCustomTitle(std::int64_t chatId, std::int64_t userId, const std::string& customTitle) const { + vector<HttpReqArg> args; + args.reserve(3); + + args.emplace_back("chat_id", chatId); + args.emplace_back("user_id", userId); + args.emplace_back("custom_title", customTitle); + + return sendRequest("setChatAdministratorCustomTitle", args).get<bool>("", false); +} + +bool Api::setChatPermissions(std::int64_t chatId, ChatPermissions::Ptr permissions) const { + vector<HttpReqArg> args; + args.reserve(2); + args.emplace_back("chat_id", chatId); + args.emplace_back("permissions", _tgTypeParser.parseChatPermissions(permissions)); + return sendRequest("setChatPermissions", args).get<bool>("", false); +} + +string Api::exportChatInviteLink(std::int64_t chatId) const { + vector<HttpReqArg> args; + args.reserve(1); + args.emplace_back("chat_id", chatId); + return sendRequest("exportChatInviteLink", args).get("", ""); +} + +bool Api::setChatPhoto(std::int64_t chatId, const InputFile::Ptr photo) const { + vector<HttpReqArg> args; + args.reserve(2); + args.emplace_back("chat_id", chatId); + args.emplace_back("photo", photo->data, true, photo->mimeType, photo->fileName); + return sendRequest("setChatPhoto", args).get<bool>("", false); +} + +bool Api::deleteChatPhoto(std::int64_t chatId) const { + vector<HttpReqArg> args; + args.reserve(1); + args.emplace_back("chat_id", chatId); + return sendRequest("deleteChatPhoto", args).get<bool>("", false); +} + +bool Api::setChatTitle(std::int64_t chatId, const string& title) const { + vector<HttpReqArg> args; + args.reserve(2); + args.emplace_back("chat_id", chatId); + args.emplace_back("title", title); + return sendRequest("setChatTitle", args).get<bool>("", false); +} + +bool Api::setChatDescription(std::int64_t chatId, const string& description) const { + vector<HttpReqArg> args; + args.reserve(2); + args.emplace_back("chat_id", chatId); + args.emplace_back("description", description); + return sendRequest("setChatDescription", args).get<bool>("", false); +} + +bool Api::pinChatMessage(std::int64_t chatId, std::int32_t messageId, bool disableNotification) const { + vector<HttpReqArg> args; + args.reserve(3); + args.emplace_back("chat_id", chatId); + args.emplace_back("message_id", messageId); + if (disableNotification) { + args.emplace_back("disable_notification", disableNotification); + } + return sendRequest("pinChatMessage", args).get<bool>("", false); +} + +bool Api::unpinChatMessage(std::int64_t chatId) const { + vector<HttpReqArg> args; + args.reserve(1); + args.emplace_back("chat_id", chatId); + return sendRequest("unpinChatMessage", args).get<bool>("", false); +} + bool Api::leaveChat(std::int64_t chatId) const { vector<HttpReqArg> args; args.reserve(1); @@ -788,7 +801,30 @@ int32_t Api::getChatMembersCount(std::int64_t chatId) const { return sendRequest("getChatMembersCount", args).get<int32_t>("", 0); } -bool Api::answerCallbackQuery(const string & callbackQueryId, const string & text, bool showAlert, const string &url, std::int32_t cacheTime) const { +ChatMember::Ptr Api::getChatMember(std::int64_t chatId, std::int64_t userId) const { + vector<HttpReqArg> 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<HttpReqArg> args; + args.reserve(2); + args.emplace_back("chat_id", chatId); + args.emplace_back("sticker_set_name ", stickerSetName); + return sendRequest("setChatStickerSet", args).get<bool>("", false); +} + +bool Api::deleteChatStickerSet(std::int64_t chatId) const { + vector<HttpReqArg> args; + args.reserve(1); + args.emplace_back("chat_id", chatId); + return sendRequest("deleteChatStickerSet", args).get<bool>("", false); +} + +bool Api::answerCallbackQuery(const string& callbackQueryId, const string& text, bool showAlert, const string& url, std::int32_t cacheTime) const { vector<HttpReqArg> args; args.reserve(5); args.emplace_back("callback_query_id", callbackQueryId); @@ -807,8 +843,22 @@ bool Api::answerCallbackQuery(const string & callbackQueryId, const string & tex return sendRequest("answerCallbackQuery", args).get<bool>("", false); } +bool Api::setMyCommands(const std::vector<BotCommand::Ptr>& commands) const { + vector<HttpReqArg> args; + args.reserve(5); + + string commandsJson = _tgTypeParser.parseArray<BotCommand>(&TgTypeParser::parseBotCommand, commands); + args.emplace_back("commands", commandsJson); + + return sendRequest("setMyCommands", args).get<bool>("", false); +} + +std::vector<BotCommand::Ptr> Api::getMyCommands() const { + return _tgTypeParser.parseJsonAndGetArray<BotCommand>(&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 { + const string& parseMode, bool disableWebPagePreview, const GenericReply::Ptr replyMarkup) const { vector<HttpReqArg> args; args.reserve(7); @@ -841,7 +891,6 @@ Message::Ptr Api::editMessageText(const string& text, std::int64_t chatId, std:: Message::Ptr Api::editMessageCaption(std::int64_t chatId, std::int32_t messageId, const string& caption, const string& inlineMessageId, const GenericReply::Ptr replyMarkup) const { - vector<HttpReqArg> args; args.reserve(5); if (chatId) { @@ -867,11 +916,12 @@ Message::Ptr Api::editMessageCaption(std::int64_t chatId, std::int32_t messageId } } -Message::Ptr Api::editMessageReplyMarkup(std::int64_t chatId, std::int32_t messageId, const string& inlineMessageId, - const GenericReply::Ptr replyMarkup) const { +Message::Ptr Api::editMessageMedia(InputMedia::Ptr media, std::int64_t chatId, std::int32_t messageId, const std::string& inlineMessageId, + GenericReply::Ptr replyMarkup) const { vector<HttpReqArg> args; - args.reserve(4); + args.reserve(5); + args.emplace_back("media", _tgTypeParser.parseInputMedia(media)); if (chatId) { args.emplace_back("chat_id", chatId); } @@ -884,7 +934,7 @@ Message::Ptr Api::editMessageReplyMarkup(std::int64_t chatId, std::int32_t messa if (replyMarkup) { args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); } - ptree p = sendRequest("editMessageReplyMarkup", args); + ptree p = sendRequest("editMessageMedia", args); if (p.get_child_optional("message_id")) { return _tgTypeParser.parseJsonAndGetMessage(p); } else { @@ -892,12 +942,11 @@ Message::Ptr Api::editMessageReplyMarkup(std::int64_t chatId, std::int32_t messa } } -Message::Ptr Api::editMessageMedia(InputMedia::Ptr media, std::int64_t chatId, std::int32_t messageId, const std::string& inlineMessageId, - GenericReply::Ptr replyMarkup) const { +Message::Ptr Api::editMessageReplyMarkup(std::int64_t chatId, std::int32_t messageId, const string& inlineMessageId, + const GenericReply::Ptr replyMarkup) const { vector<HttpReqArg> args; - args.reserve(5); - args.emplace_back("media", _tgTypeParser.parseInputMedia(media)); + args.reserve(4); if (chatId) { args.emplace_back("chat_id", chatId); } @@ -910,7 +959,7 @@ Message::Ptr Api::editMessageMedia(InputMedia::Ptr media, std::int64_t chatId, s if (replyMarkup) { args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); } - ptree p = sendRequest("editMessageMedia", args); + ptree p = sendRequest("editMessageReplyMarkup", args); if (p.get_child_optional("message_id")) { return _tgTypeParser.parseJsonAndGetMessage(p); } else { @@ -918,74 +967,159 @@ Message::Ptr Api::editMessageMedia(InputMedia::Ptr media, std::int64_t chatId, s } } -ChatMember::Ptr Api::getChatMember(std::int64_t chatId, std::int64_t userId) const { +Poll::Ptr Api::stopPoll(std::int64_t chatId, std::int64_t messageId, const InlineKeyboardMarkup::Ptr replyMarkup) const { vector<HttpReqArg> args; - args.reserve(2); + args.reserve(3); args.emplace_back("chat_id", chatId); - args.emplace_back("user_id", userId); - return _tgTypeParser.parseJsonAndGetChatMember(sendRequest("getChatMember", args)); + args.emplace_back("message_id", messageId); + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + return _tgTypeParser.parseJsonAndGetPoll(sendRequest("stopPoll", args)); } -vector<Update::Ptr> Api::getUpdates(std::int32_t offset, std::int32_t limit, std::int32_t timeout, const StringArrayPtr &allowedUpdates) const { +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<InputFile::Ptr, std::string> sticker, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const { vector<HttpReqArg> args; - args.reserve(4); - if (offset) { - args.emplace_back("offset", offset); + args.reserve(5); + args.emplace_back("chat_id", chatId); + if (sticker.which() == 0 /* InputFile::Ptr */) { + auto file = boost::get<InputFile::Ptr>(sticker); + args.emplace_back("sticker", file->data, true, file->mimeType, file->fileName); + } else /* std::string */ { + args.emplace_back("sticker", boost::get<std::string>(sticker)); } - limit = max(1, min(100, limit)); - args.emplace_back("limit", limit); - if (timeout) { - args.emplace_back("timeout", timeout); + if (replyToMessageId) { + args.emplace_back("reply_to_message_id", replyToMessageId); } - if (allowedUpdates != nullptr) { - string allowedUpdatesJson = _tgTypeParser.parseArray<string>( - [](const string &s)->string { - return s; - }, *allowedUpdates); - args.emplace_back("allowed_updates", allowedUpdatesJson); + if (replyMarkup) { + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); } - return _tgTypeParser.parseJsonAndGetArray<Update>(&TgTypeParser::parseJsonAndGetUpdate, sendRequest("getUpdates", args)); + if (disableNotification) { + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendSticker", args)); +} + +StickerSet::Ptr Api::getStickerSet(const string& name) const { + vector<HttpReqArg> args; + args.reserve(1); + args.emplace_back("name", name); + return _tgTypeParser.parseJsonAndGetStickerSet(sendRequest("getStickerSet", args)); } -void Api::setWebhook(const string& url, const InputFile::Ptr certificate, std::int32_t maxConnection, const StringArrayPtr &allowedUpdates) const { +File::Ptr Api::uploadStickerFile(std::int64_t userId, const InputFile::Ptr pngSticker) const { vector<HttpReqArg> args; - args.reserve(4); - if (!url.empty()) { - args.emplace_back("url", url); + 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<InputFile::Ptr, std::string> pngSticker, boost::variant<InputFile::Ptr, std::string> tgsSticker, + bool containsMasks, MaskPosition::Ptr maskPosition) const { + vector<HttpReqArg> 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<InputFile::Ptr>(pngSticker); + args.emplace_back("png_sticker", file->data, true, file->mimeType, file->fileName); + } else /* std::string */ { + args.emplace_back("png_sticker", boost::get<std::string>(pngSticker)); } - if (certificate != nullptr) { - args.emplace_back("certificate", certificate->data, true, certificate->mimeType, certificate->fileName); + if (tgsSticker.which() == 0 /* InputFile::Ptr */) { + auto file = boost::get<InputFile::Ptr>(tgsSticker); + args.emplace_back("tgs_sticker", file->data, true, file->mimeType, file->fileName); + } else /* std::string */ { + args.emplace_back("tgs_sticker", boost::get<std::string>(tgsSticker)); } - if (maxConnection != 40) { - args.emplace_back("max_connections", maxConnection); + if (containsMasks) { + args.emplace_back("contains_mask", containsMasks); } - if (allowedUpdates != nullptr) { - auto allowedUpdatesJson = _tgTypeParser.parseArray<string>( - [](const string &s)->string { - return s; - }, *allowedUpdates); - args.emplace_back("allowed_updates", allowedUpdatesJson); + if (maskPosition != nullptr) { + args.emplace_back("mask_position", _tgTypeParser.parseMaskPosition(maskPosition)); } - sendRequest("setWebhook", args); + return sendRequest("createNewStickerSet", args).get<bool>("", false); } -bool Api::deleteWebhook() const { - return sendRequest("deleteWebhook").get<bool>("", false); +bool Api::createNewStickerSet(std::int64_t userId, const std::string& name, const std::string& title, + boost::variant<InputFile::Ptr, std::string> pngSticker, const std::string& emojis, bool containsMasks, + MaskPosition::Ptr maskPosition) const { + + return createNewStickerSet(userId, name, title, emojis, pngSticker, "", containsMasks, maskPosition); } -WebhookInfo::Ptr Api::getWebhookInfo() const { - ptree p = sendRequest("getWebhookInfo"); +bool Api::addStickerToSet(std::int64_t userId, const std::string& name, const std::string& emojis, + boost::variant<InputFile::Ptr, std::string> pngSticker, boost::variant<InputFile::Ptr, std::string> tgsSticker, MaskPosition::Ptr maskPosition) const { + vector<HttpReqArg> args; + args.reserve(6); - if (!p.get_child_optional("url")) { - return nullptr; + 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<InputFile::Ptr>(pngSticker); + args.emplace_back("png_sticker", file->data, true, file->mimeType, file->fileName); + } else /* std::string */ { + args.emplace_back("png_sticker", boost::get<std::string>(pngSticker)); } - if (p.get<string>("url","") != string("")) { - return _tgTypeParser.parseJsonAndGetWebhookInfo(p); + if (tgsSticker.which() == 0 /* InputFile::Ptr */) { + auto file = boost::get<InputFile::Ptr>(tgsSticker); + args.emplace_back("tgs_sticker", file->data, true, file->mimeType, file->fileName); + } else /* std::string */ { + args.emplace_back("tgs_sticker", boost::get<std::string>(tgsSticker)); } - else { - return nullptr; + if (maskPosition != nullptr) { + args.emplace_back("mask_position", _tgTypeParser.parseMaskPosition(maskPosition)); } + + return sendRequest("addStickerToSet", args).get<bool>("", false); +} + +bool Api::addStickerToSet(std::int64_t userId, const std::string& name, boost::variant<InputFile::Ptr, std::string> 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<HttpReqArg> args; + args.reserve(2); + args.emplace_back("sticker", sticker); + args.emplace_back("position", position); + return sendRequest("setStickerPositionInSet", args).get<bool>("", false); +} + +bool Api::deleteStickerFromSet(const string& sticker) const { + vector<HttpReqArg> args; + args.reserve(1); + args.emplace_back("sticker", sticker); + return sendRequest("deleteStickerFromSet", args).get<bool>("", false); +} + +bool Api::setStickerSetThumb(const std::string& name, std::int64_t userId, boost::variant<InputFile::Ptr, std::string> thumb) const { + vector<HttpReqArg> 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<InputFile::Ptr>(thumb); + args.emplace_back("thumb", file->data, true, file->mimeType, file->fileName); + } else /* std::string */ { + args.emplace_back("thumb", boost::get<std::string>(thumb)); + } + + return sendRequest("setStickerSetThumb", args).get<bool>("", false); } bool Api::answerInlineQuery(const string& inlineQueryId, const std::vector<InlineQueryResult::Ptr>& results, @@ -1013,141 +1147,111 @@ bool Api::answerInlineQuery(const string& inlineQueryId, const std::vector<Inlin return sendRequest("answerInlineQuery", args).get<bool>("", false); } -bool Api::kickChatMember(std::int64_t chatId, std::int64_t userId, std::uint64_t untilDate) 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<LabeledPrice::Ptr>& 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<HttpReqArg> args; - args.reserve(3); + args.reserve(23); args.emplace_back("chat_id", chatId); - args.emplace_back("user_id", userId); - if (untilDate) { - args.emplace_back("until_date", untilDate); + 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<LabeledPrice>(&TgTypeParser::parseLabeledPrice, prices)); + if (!providerData.empty()) { + args.emplace_back("provider_data", providerData); } - return sendRequest("kickChatMember", args).get<bool>("", false); -} - -bool Api::unbanChatMember(std::int64_t chatId, std::int64_t userId) const { - vector<HttpReqArg> args; - args.reserve(2); - args.emplace_back("chat_id", chatId); - args.emplace_back("user_id", userId); - return sendRequest("unbanChatMember", args).get<bool>("", false); -} -bool Api::restrictChatMember(std::int64_t chatId, std::int64_t userId, TgBot::ChatPermissions::Ptr permissions, - std::uint64_t untilDate) const { - vector<HttpReqArg> args; - args.reserve(4); - args.emplace_back("chat_id", chatId); - args.emplace_back("user_id", userId); - args.emplace_back("permissions", _tgTypeParser.parseChatPermissions(permissions)); - if (untilDate) { - args.emplace_back("until_date", untilDate); + if (!photoUrl.empty()) { + args.emplace_back("photo_url", photoUrl); } - - return sendRequest("restrictChatMember", args).get<bool>("", false); -} - -bool Api::promoteChatMember(std::int64_t chatId, std::int64_t userId, bool canChangeInfo, bool canPostMessages, - bool canEditMessages, bool canDeleteMessages, bool canInviteUsers, bool canPinMessages, bool canPromoteMembers) const { - vector<HttpReqArg> args; - args.reserve(9); - args.emplace_back("chat_id", chatId); - args.emplace_back("user_id", userId); - if (canChangeInfo) { - args.emplace_back("can_change_info", canChangeInfo); + if (photoSize) { + args.emplace_back("photo_size", photoSize); } - if (canPostMessages) { - args.emplace_back("can_post_messages", canPostMessages); + if (photoWidth) { + args.emplace_back("photo_width", photoWidth); } - if (canEditMessages) { - args.emplace_back("can_edit_messages", canEditMessages); + if (photoHeight) { + args.emplace_back("photo_height", photoHeight); } - if (canDeleteMessages) { - args.emplace_back("can_delete_messages", canDeleteMessages); + if (needName) { + args.emplace_back("need_name", needName); } - if (canInviteUsers) { - args.emplace_back("can_invite_users", canInviteUsers); + if (needPhoneNumber) { + args.emplace_back("need_phone_number", needPhoneNumber); } - if (canPinMessages) { - args.emplace_back("can_pin_messages", canPinMessages); + if (needEmail) { + args.emplace_back("need_email", needEmail); } - if (canPromoteMembers) { - args.emplace_back("can_promote_members", canPromoteMembers); + if (needShippingAddress) { + args.emplace_back("need_shipping_address", needShippingAddress); } - return sendRequest("promoteChatMember", args).get<bool>("", false); -} - -bool Api::setChatAdministratorCustomTitle(std::int64_t chatId, std::int64_t userId, const std::string& customTitle) const { - vector<HttpReqArg> args; - args.reserve(3); - - args.emplace_back("chat_id", chatId); - args.emplace_back("user_id", userId); - args.emplace_back("custom_title", customTitle); - - return sendRequest("setChatAdministratorCustomTitle", args).get<bool>("", false); -} - -bool Api::setChatPermissions(std::int64_t chatId, ChatPermissions::Ptr permissions) const{ - vector<HttpReqArg> args; - args.reserve(2); - args.emplace_back("chat_id", chatId); - args.emplace_back("permissions", _tgTypeParser.parseChatPermissions(permissions)); - return sendRequest("setChatPermissions", args).get<bool>("", false); -} - -string Api::exportChatInviteLink(std::int64_t chatId) const { - vector<HttpReqArg> args; - args.reserve(1); - args.emplace_back("chat_id", chatId); - return sendRequest("exportChatInviteLink", args).get("", ""); -} - -bool Api::setChatPhoto(std::int64_t chatId, const InputFile::Ptr photo) const { - vector<HttpReqArg> args; - args.reserve(2); - args.emplace_back("chat_id", chatId); - args.emplace_back("photo", photo->data, true, photo->mimeType, photo->fileName); - return sendRequest("setChatPhoto", args).get<bool>("", false); -} - -bool Api::deleteChatPhoto(std::int64_t chatId) const { - vector<HttpReqArg> args; - args.reserve(1); - args.emplace_back("chat_id", chatId); - return sendRequest("deleteChatPhoto", args).get<bool>("", false); + 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)); + } + if (disableNotification) { + args.emplace_back("disable_notification", disableNotification); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendInvoice", args)); } -bool Api::setChatTitle(std::int64_t chatId, const string& title) const { +bool Api::answerShippingQuery(const std::string& shippingQueryId, bool ok, const std::vector<ShippingOption::Ptr>& shippingOptions, const std::string& errorMessage) const { vector<HttpReqArg> args; - args.reserve(2); - args.emplace_back("chat_id", chatId); - args.emplace_back("title", title); - return sendRequest("setChatTitle", args).get<bool>("", false); + 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<ShippingOption>(&TgTypeParser::parseShippingOption, shippingOptions)); + } + if (!errorMessage.empty()) { + args.emplace_back("error_message", errorMessage); + } + return sendRequest("answerShippingQuery", args).get<bool>("", false); } -bool Api::setChatDescription(std::int64_t chatId, const string& description) const { +bool Api::answerPreCheckoutQuery(const std::string& preCheckoutQueryId, bool ok, const std::string& errorMessage) const { vector<HttpReqArg> args; - args.reserve(2); - args.emplace_back("chat_id", chatId); - args.emplace_back("description", description); - return sendRequest("setChatDescription", args).get<bool>("", false); + 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<bool>("", false); } -bool Api::pinChatMessage(std::int64_t chatId, std::int32_t messageId, bool disableNotification) const { +Message::Ptr Api::sendGame(std::int64_t chatId, const std::string& gameShortName, std::int32_t replyToMessageId, const InlineKeyboardMarkup::Ptr replyMarkup, bool disableNotification) const { vector<HttpReqArg> args; - args.reserve(3); + args.reserve(5); args.emplace_back("chat_id", chatId); - args.emplace_back("message_id", messageId); + 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 sendRequest("pinChatMessage", args).get<bool>("", false); -} - -bool Api::unpinChatMessage(std::int64_t chatId) const { - vector<HttpReqArg> args; - args.reserve(1); - args.emplace_back("chat_id", chatId); - return sendRequest("unpinChatMessage", args).get<bool>("", false); + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendGame", args)); } 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 { @@ -1161,13 +1265,13 @@ Message::Ptr Api::setGameScore(std::int64_t userId, std::int32_t score, bool for if (disableEditMessage) { args.emplace_back("disable_edit_message", disableEditMessage); } - if (chatId){ + if (chatId) { args.emplace_back("chat_id", chatId); } - if (messageId){ + if (messageId) { args.emplace_back("message_id", messageId); } - if (!inlineMessageId.empty()){ + if (!inlineMessageId.empty()) { args.emplace_back("inline_message_id", inlineMessageId); } return _tgTypeParser.parseJsonAndGetMessage(sendRequest("setGameScore", args)); @@ -1190,106 +1294,12 @@ vector<GameHighScore::Ptr> Api::getGameHighScores(std::int64_t userId, std::int3 if (messageId) { args.emplace_back("message_id", messageId); } - if (!inlineMessageId.empty()){ + if (!inlineMessageId.empty()) { args.emplace_back("inline_message_id", inlineMessageId); } return _tgTypeParser.parseJsonAndGetArray<GameHighScore>(&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) }); -} - -Message::Ptr Api::sendPoll(std::int64_t chatId, const std::string& question, const std::vector<std::string>& 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<HttpReqArg> args; - args.reserve(11); - - args.emplace_back("chat_id", chatId); - args.emplace_back("question", question); - args.emplace_back("options", _tgTypeParser.parseArray<std::string>([] (const std::string& option) -> std::string { - return StringTools::urlEncode(option); - }, options)); - if (!isAnonymous) { - args.emplace_back("is_anonymous", isAnonymous); - } - if (!type.empty()) { - args.emplace_back("type", type); - } - if (allowsMultipleAnswers) { - args.emplace_back("allows_multiple_answers", allowsMultipleAnswers); - } - if (correctOptionId != 0) { - args.emplace_back("correct_option_id", correctOptionId); - } - if (isClosed) { - args.emplace_back("is_closed", isClosed); - } - if (disableNotification) { - args.emplace_back("disable_notification", disableNotification); - } - if (replyToMessageId != 0) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); - } - - return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendPoll", args)); -} - -Message::Ptr Api::sendPoll(std::int64_t chatId, const std::string& question, const std::vector<std::string>& options, - bool disableNotification, std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup) const { - - return sendPoll(chatId, question, options, true, "", false, 0, false, disableNotification, replyToMessageId, replyMarkup); -} - -Message::Ptr Api::sendDice(std::int64_t chatId, bool disableNotification, std::int32_t replyToMessageId, - GenericReply::Ptr replyMarkup) const { - vector<HttpReqArg> args; - args.reserve(4); - - args.emplace_back("chat_id", chatId); - if (disableNotification) { - args.emplace_back("disable_notification", disableNotification); - } - if (replyToMessageId != 0) { - args.emplace_back("reply_to_message_id", replyToMessageId); - } - if (replyMarkup) { - args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); - } - - return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendDice", args)); -} - -Poll::Ptr Api::stopPoll(std::int64_t chatId, std::int64_t messageId, const InlineKeyboardMarkup::Ptr replyMarkup) const { - vector<HttpReqArg> 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)); -} - -bool Api::setMyCommands(const std::vector<BotCommand::Ptr>& commands) const { - vector<HttpReqArg> args; - args.reserve(5); - - string commandsJson = _tgTypeParser.parseArray<BotCommand>(&TgTypeParser::parseBotCommand, commands); - args.emplace_back("commands", commandsJson); - - return sendRequest("setMyCommands",args).get<bool>("",false); -} - -std::vector<BotCommand::Ptr> Api::getMyCommands() const { - return _tgTypeParser.parseJsonAndGetArray<BotCommand>(&TgTypeParser::parseJsonAndGetBotCommand, sendRequest("getMyCommands")); -} - ptree Api::sendRequest(const string& method, const vector<HttpReqArg>& args) const { string url(_url); url += "/bot"; @@ -1313,19 +1323,4 @@ ptree Api::sendRequest(const string& method, const vector<HttpReqArg>& args) con throw TgException("tgbot-cpp library can't parse json response. " + string(e.what())); } } - -string Api::downloadFile(const string& filePath, const std::vector<HttpReqArg>& args) const { - string url(_url); - url += "/file/bot"; - url += _token; - url += "/"; - url += filePath; - - string serverResponse = _httpClient.makeRequest(url, args); - - return serverResponse; -} - - - } |