/* * Copyright (c) 2015 Oleg Morozenkov * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include "tgbot/Api.h" #include "tgbot/TgTypeParser.h" #include "tgbot/TgException.h" #include "tgbot/net/HttpClient.h" using namespace std; using namespace boost::property_tree; namespace TgBot { Api::Api(const string& token) : _token(token) { } User::Ptr Api::getMe() const { return TgTypeParser::getInstance().parseJsonAndGetUser(sendRequest("getMe")); } Message::Ptr Api::sendMessage(int64_t chatId, const string& text, bool disableWebPagePreview, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("text", text)); if (disableWebPagePreview) { args.push_back(HttpReqArg("disable_web_page_preview", disableWebPagePreview)); } if (disableNotification){ args.push_back(HttpReqArg("disable_notification", disableNotification)); } if (replyToMessageId) { args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId)); } if (replyMarkup) { args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup))); } if (!parseMode.empty()) { args.push_back(HttpReqArg("parse_mode", parseMode)); } return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendMessage", args)); } Message::Ptr Api::forwardMessage(int64_t chatId, int64_t fromChatId, int32_t messageId, bool disableNotification) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("from_chat_id", fromChatId)); args.push_back(HttpReqArg("message_id", messageId)); if (disableNotification){ args.push_back(HttpReqArg("disable_notification", disableNotification)); } return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("forwardMessage", args)); } Message::Ptr Api::sendPhoto(int64_t chatId, const InputFile::Ptr photo, const string& caption, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("photo", photo->data, true, photo->mimeType, photo->fileName)); if (!caption.empty()) { args.push_back(HttpReqArg("caption", caption)); } if (replyToMessageId) { args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId)); } if (replyMarkup) { args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup))); } if (!parseMode.empty()) { args.push_back(HttpReqArg("parse_mode", parseMode)); } if (disableNotification){ args.push_back(HttpReqArg("disable_notification", disableNotification)); } return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendPhoto", args)); } Message::Ptr Api::sendPhoto(int64_t chatId, const string& photoId, const string& caption, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("photo", photoId)); if (!caption.empty()) { args.push_back(HttpReqArg("caption", caption)); } if (replyToMessageId) { args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId)); } if (replyMarkup) { args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup))); } if (!parseMode.empty()) { args.push_back(HttpReqArg("parse_mode", parseMode)); } if (disableNotification){ args.push_back(HttpReqArg("disable_notification", disableNotification)); } return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendPhoto", args)); } Message::Ptr Api::sendAudio(int64_t chatId, const InputFile::Ptr audio, const string &caption, int32_t duration, const string& performer, const string& title, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("audio", audio->data, true, audio->mimeType, audio->fileName)); if (!caption.empty()) { args.push_back(HttpReqArg("caption", caption)); } if (duration) { args.push_back(HttpReqArg("duration", duration)); } if (!performer.empty()){ args.push_back(HttpReqArg("performer", performer)); } if (!title.empty()){ args.push_back(HttpReqArg("title", title)); } if (replyToMessageId) { args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId)); } if (replyMarkup) { args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup))); } if (!parseMode.empty()) { args.push_back(HttpReqArg("parse_mode", parseMode)); } if (disableNotification){ args.push_back(HttpReqArg("disable_notification", disableNotification)); } return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendAudio", args)); } Message::Ptr Api::sendAudio(int64_t chatId, const string& audioId, const string &caption, int32_t duration, const string& performer, const string& title, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("audio", audioId)); if (!caption.empty()) { args.push_back(HttpReqArg("caption", caption)); } if (duration) { args.push_back(HttpReqArg("duration", duration)); } if (!performer.empty()){ args.push_back(HttpReqArg("performer", performer)); } if (!title.empty()){ args.push_back(HttpReqArg("title", title)); } if (replyToMessageId) { args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId)); } if (replyMarkup) { args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup))); } if (!parseMode.empty()) { args.push_back(HttpReqArg("parse_mode", parseMode)); } if (disableNotification){ args.push_back(HttpReqArg("disable_notification", disableNotification)); } return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendAudio", args)); } Message::Ptr Api::sendDocument(int64_t chatId, const InputFile::Ptr document, const string &caption, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("document", document->data, true, document->mimeType, document->fileName)); if (!caption.empty()) { args.push_back(HttpReqArg("caption", caption)); } if (replyToMessageId) { args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId)); } if (replyMarkup) { args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup))); } if (!parseMode.empty()) { args.push_back(HttpReqArg("parse_mode", parseMode)); } if (disableNotification){ args.push_back(HttpReqArg("disable_notification", disableNotification)); } return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendDocument", args)); } Message::Ptr Api::sendDocument(int64_t chatId, const string& document, const string &caption, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("document", document)); if (!caption.empty()) { args.push_back(HttpReqArg("caption", caption)); } if (replyToMessageId) { args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId)); } if (replyMarkup) { args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup))); } if (!parseMode.empty()) { args.push_back(HttpReqArg("parse_mode", parseMode)); } if (disableNotification){ args.push_back(HttpReqArg("disable_notification", disableNotification)); } return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendDocument", args)); } Message::Ptr Api::sendSticker(int64_t chatId, const InputFile::Ptr sticker, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("sticker", sticker->data, true, sticker->mimeType, sticker->fileName)); if (replyToMessageId) { args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId)); } if (replyMarkup) { args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup))); } if (disableNotification){ args.push_back(HttpReqArg("disable_notification", disableNotification)); } return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendSticker", args)); } Message::Ptr Api::sendSticker(int64_t chatId, const string& stickerId, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("sticker", stickerId)); if (replyToMessageId) { args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId)); } if (replyMarkup) { args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup))); } if (disableNotification){ args.push_back(HttpReqArg("disable_notification", disableNotification)); } return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendSticker", args)); } StickerSet::Ptr Api::getStickerSet(const string& name) const { vector args; args.push_back(HttpReqArg("name", name)); return TgTypeParser::getInstance().parseJsonAndGetStickerSet(sendRequest("getStickerSet", args)); } File::Ptr Api::uploadStickerFile(int32_t userId, const InputFile::Ptr pngSticker) const { vector args; args.push_back(HttpReqArg("user_id", userId)); args.push_back(HttpReqArg("png_sticker", pngSticker->data, true, pngSticker->mimeType, pngSticker->fileName)); return TgTypeParser::getInstance().parseJsonAndGetFile(sendRequest("uploadStickerFile", args)); } bool Api::createNewStickerSet(int32_t userId, const string& name, const string& title, InputFile::Ptr pngSticker, const string& emojis, bool containsMasks, MaskPosition::Ptr maskPosition) const { vector args; args.push_back(HttpReqArg("user_id", userId)); args.push_back(HttpReqArg("name", name)); args.push_back(HttpReqArg("title", title)); args.push_back(HttpReqArg("png_sticker", pngSticker->data, true, pngSticker->mimeType, pngSticker->fileName)); args.push_back(HttpReqArg("emojis", emojis)); args.push_back(HttpReqArg("contains_mask", containsMasks)); args.push_back(HttpReqArg("mask_position", TgTypeParser::getInstance().parseMaskPosition(maskPosition))); return sendRequest("createNewStickerSet", args).get("", false); } bool Api::createNewStickerSet(int32_t userId, const string& name, const string& title, const string& pngSticker, const string& emojis, bool containsMasks, MaskPosition::Ptr maskPosition) const { vector args; args.push_back(HttpReqArg("user_id", userId)); args.push_back(HttpReqArg("name", name)); args.push_back(HttpReqArg("title", title)); args.push_back(HttpReqArg("png_sticker", pngSticker)); args.push_back(HttpReqArg("emojis", emojis)); args.push_back(HttpReqArg("contains_mask", containsMasks)); args.push_back(HttpReqArg("mask_position", TgTypeParser::getInstance().parseMaskPosition(maskPosition))); return sendRequest("createNewStickerSet", args).get("", false); } bool Api::addStickerToSet(int32_t userId, const string& name, const string& title, InputFile::Ptr pngSticker, const string& emojis, MaskPosition::Ptr maskPosition) const { vector args; args.push_back(HttpReqArg("user_id", userId)); args.push_back(HttpReqArg("name", name)); args.push_back(HttpReqArg("title", title)); args.push_back(HttpReqArg("png_sticker", pngSticker->data, true, pngSticker->mimeType, pngSticker->fileName)); args.push_back(HttpReqArg("emojis", emojis)); args.push_back(HttpReqArg("mask_position", TgTypeParser::getInstance().parseMaskPosition(maskPosition))); return sendRequest("addStickerToSet", args).get("", false); } bool Api::addStickerToSet(int32_t userId, const string& name, const string& title, const string& pngSticker, const string& emojis, MaskPosition::Ptr maskPosition) const { vector args; args.push_back(HttpReqArg("user_id", userId)); args.push_back(HttpReqArg("name", name)); args.push_back(HttpReqArg("title", title)); args.push_back(HttpReqArg("png_sticker", pngSticker)); args.push_back(HttpReqArg("emojis", emojis)); args.push_back(HttpReqArg("mask_position", TgTypeParser::getInstance().parseMaskPosition(maskPosition))); return sendRequest("addStickerToSet", args).get("", false); } bool Api::setStickerPositionInSet(const string& sticker, uint32_t position) const { vector args; args.push_back(HttpReqArg("sticker", sticker)); args.push_back(HttpReqArg("position", position)); return sendRequest("setStickerPositionInSet", args).get("", false); } bool Api::deleteStickerPositionInSet(const string& sticker) const { vector args; args.push_back(HttpReqArg("sticker", sticker)); return sendRequest("setStickerPositionInSet", args).get("", false); } Message::Ptr Api::sendVideo(int64_t chatId, const InputFile::Ptr video, bool supportsStreaming, int32_t duration, int32_t width, int32_t height, const string &caption, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("video", video->data, true, video->mimeType, video->fileName)); if (supportsStreaming) { args.push_back(HttpReqArg("supports_streaming", supportsStreaming)); } if (duration) { args.push_back(HttpReqArg("duration", duration)); } if (width) { args.push_back(HttpReqArg("width", width)); } if (height) { args.push_back(HttpReqArg("height", height)); } if (!caption.empty()) { args.push_back(HttpReqArg("caption", caption)); } if (replyToMessageId) { args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId)); } if (replyMarkup) { args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup))); } if (!parseMode.empty()) { args.push_back(HttpReqArg("parse_mode", parseMode)); } if (disableNotification){ args.push_back(HttpReqArg("disable_notification", disableNotification)); } return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendVideo", args)); } Message::Ptr Api::sendVideo(int64_t chatId, const string& videoId, bool supportsStreaming, int32_t duration, int32_t width, int32_t height, const string &caption, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("video", videoId)); if (supportsStreaming) { args.push_back(HttpReqArg("supports_streaming", supportsStreaming)); } if (duration) { args.push_back(HttpReqArg("duration", duration)); } if (width) { args.push_back(HttpReqArg("width", width)); } if (height) { args.push_back(HttpReqArg("height", height)); } if (!caption.empty()) { args.push_back(HttpReqArg("caption", caption)); } if (replyToMessageId) { args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId)); } if (replyMarkup) { args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup))); } if (!parseMode.empty()) { args.push_back(HttpReqArg("parse_mode", parseMode)); } if (disableNotification){ args.push_back(HttpReqArg("disable_notification", disableNotification)); } return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendVideo", args)); } Message::Ptr Api::sendVideoNote(int64_t chatId, const InputFile::Ptr videoNote, int64_t replyToMessageId, bool disableNotification, int32_t duration, int32_t length, const GenericReply::Ptr replyMarkup) { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("video_note", videoNote)); if (disableNotification) { args.push_back(HttpReqArg("disable_notification", disableNotification)); } if (duration) { args.push_back(HttpReqArg("duration", duration)); } if (length) { args.push_back(HttpReqArg("length", length)); } if (replyMarkup) { args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup))); } if (replyToMessageId) { args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId)); } return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendVoiceNote", args)); } Message::Ptr Api::sendVideoNote(int64_t chatId, const string &videoNote, int64_t replyToMessageId, bool disableNotification, int32_t duration, int32_t length, const GenericReply::Ptr replyMarkup) { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("video_note", videoNote)); if (disableNotification) { args.push_back(HttpReqArg("disable_notification", disableNotification)); } if (duration) { args.push_back(HttpReqArg("duration", duration)); } if (length) { args.push_back(HttpReqArg("length", length)); } if (replyMarkup) { args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup))); } if (replyToMessageId) { args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId)); } return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendVoiceNote", args)); } vector Api::sendMediaGroup(int64_t chatId, const vector& media, bool disableNotification, int32_t replyToMessageId) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); string mediaJson = TgTypeParser::getInstance().parseArray(&TgTypeParser::parseInputMedia, media); args.push_back(HttpReqArg("media", mediaJson)); args.push_back(HttpReqArg("disable_notification", disableNotification)); args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId)); return TgTypeParser::getInstance().parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetMessage, sendRequest("sendMediaGroup", args)); } Message::Ptr Api::sendVoice(int64_t chatId, const InputFile::Ptr voice, const string &caption, int duration, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("voice", voice->data, true, voice->mimeType, voice->fileName)); if (!caption.empty()) { args.push_back(HttpReqArg("caption", caption)); } if (duration){ args.push_back(HttpReqArg("duration", duration)); } if (replyToMessageId) { args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId)); } if (replyMarkup) { args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup))); } if (!parseMode.empty()) { args.push_back(HttpReqArg("parse_mode", parseMode)); } if (disableNotification){ args.push_back(HttpReqArg("disable_notification", disableNotification)); } return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendVoice", args)); } Message::Ptr Api::sendVoice(int64_t chatId, const string& voiceId, const string &caption, int duration, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("voice", voiceId)); if (!caption.empty()) { args.push_back(HttpReqArg("caption", caption)); } if (duration){ args.push_back(HttpReqArg("duration", duration)); } if (replyToMessageId) { args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId)); } if (replyMarkup) { args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup))); } if (!parseMode.empty()) { args.push_back(HttpReqArg("parse_mode", parseMode)); } if (disableNotification){ args.push_back(HttpReqArg("disable_notification", disableNotification)); } return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendVoice", args)); } Message::Ptr Api::sendLocation(int64_t chatId, float latitude, float longitude, uint32_t livePeriod, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("latitude", latitude)); args.push_back(HttpReqArg("longitude", longitude)); if (livePeriod) { args.push_back(HttpReqArg("live_period", livePeriod)); } if (replyToMessageId) { args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId)); } if (replyMarkup) { args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup))); } if (disableNotification){ args.push_back(HttpReqArg("disable_notification", disableNotification)); } return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendLocation", args)); } Message::Ptr Api::editMessageLiveLocation(float latitude, float longitude, int64_t chatId, int32_t messageId, int32_t inlineMessageId, const InlineKeyboardMarkup::Ptr replyMarkup) const { vector args; args.push_back(HttpReqArg("latitude", latitude)); args.push_back(HttpReqArg("longitude", longitude)); if (chatId) { args.push_back(HttpReqArg("chat_id", chatId)); } if (messageId) { args.push_back(HttpReqArg("message_id", messageId)); } if (inlineMessageId) { args.push_back(HttpReqArg("inline_message_id", inlineMessageId)); } if (replyMarkup) { args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseInlineKeyboardMarkup(replyMarkup))); } return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("editMessageLiveLocation", args)); } Message::Ptr Api::stopMessageLiveLocation(int64_t chatId, int32_t messageId, int32_t inlineMessageId, const InlineKeyboardMarkup::Ptr replyMarkup) const { vector args; if (chatId) { args.push_back(HttpReqArg("chat_id", chatId)); } if (messageId) { args.push_back(HttpReqArg("message_id", messageId)); } if (inlineMessageId) { args.push_back(HttpReqArg("inline_message_id", inlineMessageId)); } if (replyMarkup) { args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseInlineKeyboardMarkup(replyMarkup))); } return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("editMessageLiveLocation", args)); } bool Api::setChatStickerSet(int64_t chatId, const string& stickerSetName) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("sticker_set_name ", stickerSetName)); return sendRequest("setChatStickerSet", args).get("", false); } bool Api::deleteChatStickerSet(int64_t chatId) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); return sendRequest("deleteChatStickerSet", args).get("", false); } Message::Ptr Api::sendVenue(int64_t chatId, float latitude, float longitude, const string& title, const string& address, const string& foursquareId, bool disableNotification, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("latitude", latitude)); args.push_back(HttpReqArg("longitude", longitude)); args.push_back(HttpReqArg("title", title)); args.push_back(HttpReqArg("address", address)); if (!foursquareId.empty()) { args.push_back(HttpReqArg("foursquare_id", foursquareId)); } if (replyToMessageId) { args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId)); } if (replyMarkup) { args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup))); } if (disableNotification){ args.push_back(HttpReqArg("disable_notification", disableNotification)); } return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendVenue", args)); } Message::Ptr Api::sendContact(int64_t chatId, const string& phoneNumber, const string& firstName, const string& lastName, bool disableNotification, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("phone_number", phoneNumber)); args.push_back(HttpReqArg("first_name", firstName)); args.push_back(HttpReqArg("last_name", lastName)); if (replyToMessageId) { args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId)); } if (replyMarkup) { args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup))); } if (disableNotification){ args.push_back(HttpReqArg("disable_notification", disableNotification)); } return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendContact", args)); } void Api::sendChatAction(int64_t chatId, const string& action) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("action", action)); sendRequest("sendChatAction", args); } UserProfilePhotos::Ptr Api::getUserProfilePhotos(int32_t userId, int32_t offset, int32_t limit) const { vector args; args.push_back(HttpReqArg("user_id", userId)); if (offset) { args.push_back(HttpReqArg("offset", offset)); } limit = max(1, min(100, limit)); args.push_back(HttpReqArg("limit", limit)); return TgTypeParser::getInstance().parseJsonAndGetUserProfilePhotos(sendRequest("getUserProfilePhotos", args)); } File::Ptr Api::getFile(const string &fileId) const { vector args; args.push_back(HttpReqArg("file_id", fileId)); return TgTypeParser::getInstance().parseJsonAndGetFile(sendRequest("getFile", args)); } bool Api::leaveChat(int64_t chatId) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); return sendRequest("leaveChat", args).get("", false); } Chat::Ptr Api::getChat(int64_t chatId) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); return TgTypeParser::getInstance().parseJsonAndGetChat(sendRequest("getChat", args)); } vector Api::getChatAdministrators(int64_t chatId) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); return TgTypeParser::getInstance().parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetChatMember, sendRequest("getChatAdministrators", args)); } int32_t Api::getChatMembersCount(int64_t chatId) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); return sendRequest("getChatMembersCount", args).get("", 0); } bool Api::answerCallbackQuery(const string & callbackQueryId, const string & text, bool showAlert, const string &url, int32_t cacheTime) const { vector args; args.push_back(HttpReqArg("callback_query_id", callbackQueryId)); if (!text.empty()) { args.push_back(HttpReqArg("text", text)); } if (showAlert) { args.push_back(HttpReqArg("show_alert", showAlert)); } if (!url.empty()) { args.push_back(HttpReqArg("url", url)); } if (cacheTime) { args.push_back(HttpReqArg("cache_time", cacheTime)); } return sendRequest("answerCallbackQuery", args).get("", false); } Message::Ptr Api::editMessageText(const string& text, int64_t chatId, int32_t messageId, const string& inlineMessageId, const string& parseMode, bool disableWebPagePreview, const GenericReply::Ptr replyMarkup) const { vector args; args.push_back(HttpReqArg("text", text)); if (chatId) { args.push_back(HttpReqArg("chat_id", chatId)); } if (messageId) { args.push_back(HttpReqArg("message_id", messageId)); } if (!inlineMessageId.empty()) { args.push_back(HttpReqArg("inline_message_id", inlineMessageId)); } if (!parseMode.empty()) { args.push_back(HttpReqArg("parse_mode", parseMode)); } if (disableWebPagePreview) { args.push_back(HttpReqArg("disable_web_page_preview", disableWebPagePreview)); } if (replyMarkup) { args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup))); } ptree p = sendRequest("editMessageText", args); if (p.get_child_optional("message_id")) { return TgTypeParser::getInstance().parseJsonAndGetMessage(p); } else { return nullptr; } } Message::Ptr Api::editMessageCaption(int64_t chatId, int32_t messageId, const string& caption, const string& inlineMessageId, const GenericReply::Ptr replyMarkup) const { vector args; if (chatId) { args.push_back(HttpReqArg("chat_id", chatId)); } if (messageId) { args.push_back(HttpReqArg("message_id", messageId)); } if (!caption.empty()) { args.push_back(HttpReqArg("caption", caption)); } if (!inlineMessageId.empty()) { args.push_back(HttpReqArg("inline_message_id", inlineMessageId)); } if (replyMarkup) { args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup))); } ptree p = sendRequest("editMessageCaption", args); if (p.get_child_optional("message_id")) { return TgTypeParser::getInstance().parseJsonAndGetMessage(p); } else { return nullptr; } } Message::Ptr Api::editMessageReplyMarkup(int64_t chatId, int32_t messageId, const string& inlineMessageId, const GenericReply::Ptr replyMarkup) const { vector args; if (chatId) { args.push_back(HttpReqArg("chat_id", chatId)); } if (messageId) { args.push_back(HttpReqArg("message_id", messageId)); } if (!inlineMessageId.empty()) { args.push_back(HttpReqArg("inline_message_id", inlineMessageId)); } if (replyMarkup) { args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup))); } ptree p = sendRequest("editMessageReplyMarkup", args); if (p.get_child_optional("message_id")) { return TgTypeParser::getInstance().parseJsonAndGetMessage(p); } else { return nullptr; } } ChatMember::Ptr Api::getChatMember(int64_t chatId, int32_t userId) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("user_id", userId)); return TgTypeParser::getInstance().parseJsonAndGetChatMember(sendRequest("getChatMember", args)); } vector Api::getUpdates(int32_t offset, int32_t limit, int32_t timeout, const StringArrayPtr &allowedUpdates) const { vector args; if (offset) { args.push_back(HttpReqArg("offset", offset)); } limit = max(1, min(100, limit)); args.push_back(HttpReqArg("limit", limit)); if (timeout) { args.push_back(HttpReqArg("timeout", timeout)); } if (allowedUpdates != nullptr) { string allowedUpdatesJson = TgTypeParser::getInstance().parseArray( [](const string &s)->string { return s; }, *allowedUpdates); args.push_back(HttpReqArg("allowed_updates", allowedUpdatesJson)); } return TgTypeParser::getInstance().parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetUpdate, sendRequest("getUpdates", args)); } void Api::setWebhook(const string& url, const InputFile::Ptr certificate, int32_t maxConnection, const StringArrayPtr &allowedUpdates) const { vector args; if (!url.empty()) args.push_back(HttpReqArg("url", url)); if (certificate != nullptr) args.push_back(HttpReqArg("certificate", certificate->data, true, certificate->mimeType, certificate->fileName)); if (maxConnection != 40) args.push_back(HttpReqArg("max_connections", maxConnection)); if (allowedUpdates != nullptr) { string allowedUpdatesJson = TgTypeParser::getInstance().parseArray( [](const string &s)->string { return s; }, *allowedUpdates); args.push_back(HttpReqArg("allowed_updates", allowedUpdatesJson)); } sendRequest("setWebhook", args); } bool Api::deleteWebhook() const { ptree p = sendRequest("deleteWebhook"); return p.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::getInstance().parseJsonAndGetWebhookInfo(p); } else { return nullptr; } } bool Api::answerInlineQuery(const string& inlineQueryId, const std::vector& results, int32_t cacheTime, bool isPersonal, const string& nextOffset, const string& switchPmText, const string& switchPmParameter) const { vector args; args.push_back(HttpReqArg("inline_query_id", inlineQueryId)); string resultsJson = TgTypeParser::getInstance().parseArray(&TgTypeParser::parseInlineQueryResult, results); args.push_back(HttpReqArg("results", resultsJson)); if (cacheTime) { args.push_back(HttpReqArg("cache_time", cacheTime)); } if (isPersonal) { args.push_back(HttpReqArg("is_personal", isPersonal)); } if (!nextOffset.empty()) { args.push_back(HttpReqArg("next_offset", nextOffset)); } if (!switchPmText.empty()) { args.push_back(HttpReqArg("switch_pm_text", switchPmText)); } if (!switchPmParameter.empty()) { args.push_back(HttpReqArg("switch_pm_parameter", switchPmParameter)); } return sendRequest("answerInlineQuery", args).get("", false); } bool Api::kickChatMember(int64_t chatId, int32_t userId, uint64_t untilDate) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("user_id", userId)); if (untilDate) { args.push_back(HttpReqArg("until_date", untilDate)); } return sendRequest("kickChatMember", args).get("", false); } bool Api::unbanChatMember(int64_t chatId, int32_t userId) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("user_id", userId)); return sendRequest("unbanChatMember", args).get("", false); } bool Api::restrictChatMember(int64_t chatId, int32_t userId, uint64_t untilDate, bool canSendMessages, bool canSendMediaMessages, bool canSendOtherMessages, bool canAddWebPagePreviews) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("user_id", userId)); if (untilDate) { args.push_back(HttpReqArg("until_date", untilDate)); } if (canSendMessages) { args.push_back(HttpReqArg("can_send_messages", canSendMessages)); } if (canSendMediaMessages) { args.push_back(HttpReqArg("can_send_media_messages", canSendMediaMessages)); } if (canSendOtherMessages) { args.push_back(HttpReqArg("can_send_other_messages", canSendOtherMessages)); } if (canAddWebPagePreviews) { args.push_back(HttpReqArg("can_add_web_page_previews", canAddWebPagePreviews)); } return sendRequest("restrictChatMember", args).get("", false); } bool Api::promoteChatMember(int64_t chatId, int32_t userId, bool canChangeInfo, bool canPostMessages, bool canEditMessages, bool canDeleteMessages, bool canInviteUsers, bool canPinMessages, bool canPromoteMembers) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("user_id", userId)); if (canChangeInfo) { args.push_back(HttpReqArg("can_change_info", canChangeInfo)); } if (canPostMessages) { args.push_back(HttpReqArg("can_post_messages", canPostMessages)); } if (canEditMessages) { args.push_back(HttpReqArg("can_edit_messages", canEditMessages)); } if (canDeleteMessages) { args.push_back(HttpReqArg("can_delete_messages", canDeleteMessages)); } if (canInviteUsers) { args.push_back(HttpReqArg("can_invite_users", canInviteUsers)); } if (canPinMessages) { args.push_back(HttpReqArg("can_pin_messages", canPinMessages)); } if (canPromoteMembers) { args.push_back(HttpReqArg("can_promote_members", canPromoteMembers)); } return sendRequest("promoteChatMember", args).get("", false); } string Api::exportChatInviteLink(int64_t chatId) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); return sendRequest("exportChatInviteLink", args).get("", ""); } bool Api::setChatPhoto(int64_t chatId, const InputFile::Ptr photo) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("photo", photo->data, true, photo->mimeType, photo->fileName)); return sendRequest("setChatPhoto", args).get("", false); } bool Api::deleteChatPhoto(int64_t chatId) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); return sendRequest("deleteChatPhoto", args).get("", false); } bool Api::setChatTitle(int64_t chatId, const string& title) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("title", title)); return sendRequest("setChatTitle", args).get("", false); } bool Api::setChatDescription(int64_t chatId, const string& description) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("description", description)); return sendRequest("setChatDescription", args).get("", false); } bool Api::pinChatMessage(int64_t chatId, int32_t messageId, bool disableNotification) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); args.push_back(HttpReqArg("message_id", messageId)); if (disableNotification) { args.push_back(HttpReqArg("disable_notification", disableNotification)); } return sendRequest("pinChatMessage", args).get("", false); } bool Api::unpinChatMessage(int64_t chatId) const { vector args; args.push_back(HttpReqArg("chat_id", chatId)); return sendRequest("unpinChatMessage", args).get("", false); } void Api::deleteMessage(int64_t chatId, int32_t messageId) const { sendRequest("deleteMessage", { HttpReqArg("chat_id", chatId), HttpReqArg("message_id", messageId) }); } ptree Api::sendRequest(const string& method, const vector& args) const { string url = "https://api.telegram.org/bot"; url += _token; url += "/"; url += method; string serverResponse = HttpClient::getInstance().makeRequest(url, args); if (!serverResponse.compare(0, 6, "")) { throw TgException("tgbot-cpp library have got html page instead of json response. Maybe you entered wrong bot token."); } ptree result = TgTypeParser::getInstance().parseJson(serverResponse); try { if (result.get("ok", false)) { return result.get_child("result"); } else { throw TgException(result.get("description", "")); } } catch (boost::property_tree::ptree_error& e) { 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 = "https://api.telegram.org/file/bot"; url += _token; url += "/"; url += filePath; string serverResponse = HttpClient::getInstance().makeRequest(url, args); return serverResponse; } }