From 23553bc251ca673137b684082a6f84062f053bd2 Mon Sep 17 00:00:00 2001 From: nitanmarcel Date: Tue, 19 Nov 2019 21:31:44 +0200 Subject: API 4.2 --- src/Api.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/TgTypeParser.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/Api.cpp b/src/Api.cpp index 6b41947..ae6ecee 100644 --- a/src/Api.cpp +++ b/src/Api.cpp @@ -276,6 +276,7 @@ bool Api::answerPreCheckoutQuery(const std::string& preCheckoutQueryId, bool ok, return sendRequest("answerPreCheckoutQuery", args).get("", false); } + Message::Ptr Api::sendSticker(int64_t chatId, const boost::variant sticker, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const { vector args; args.reserve(5); @@ -1143,6 +1144,43 @@ void Api::deleteMessage(int64_t chatId, int32_t messageId) const { sendRequest("deleteMessage", { HttpReqArg("chat_id", chatId), HttpReqArg("message_id", messageId) }); } +Message::Ptr Api::sendPoll(int64_t chat_id, std::string question, std::vector options, bool disable_notification, int32_t reply_to_message_id,GenericReply::Ptr reply_markup) const { + vector args; + args.reserve(6); + args.emplace_back("chat_id", chat_id); + args.emplace_back("question", question); + std::string json_array; + json_array = "["; + for(const auto &arr : options){ + json_array += "\"" + arr + "\"" + ","; + } + json_array.pop_back(); + json_array += "]"; + args.emplace_back("options", json_array); + + if (disable_notification){ + args.emplace_back("disable_notification", disable_notification); + } + if (reply_to_message_id!=0){ + args.emplace_back("reply_to_message_id", reply_to_message_id); + } + if (reply_markup){ + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(reply_markup)); + } + return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendPoll", args)); +} + +Poll::Ptr Api::stopPoll(int64_t chatId, int64_t messageId, const InlineKeyboardMarkup::Ptr replyMarkup) const { + vector args; + args.reserve(3); + args.emplace_back("chat_id", chatId); + args.emplace_back("message_id", messageId); + if (replyMarkup){ + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); + } + return _tgTypeParser.parseJsonAndGetPoll(sendRequest("stopPoll", args)); +} + ptree Api::sendRequest(const string& method, const vector& args) const { string url = "https://api.telegram.org/bot"; url += _token; diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index 4e31c94..528993b 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -144,6 +144,7 @@ Message::Ptr TgTypeParser::parseJsonAndGetMessage(const ptree& data) const { result->forwardFromChat = tryParseJson(&TgTypeParser::parseJsonAndGetChat, data, "forward_from_chat"); result->forwardFromMessageId = data.get("forward_from_message_id", 0); result->forwardSignature = data.get("forward_signature", ""); + result->forwardSenderName = data.get("forward_sender_name", ""); result->forwardDate = data.get("forward_date", 0); result->replyToMessage = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "reply_to_message"); result->editDate = data.get("edit_date", 0); @@ -161,6 +162,7 @@ Message::Ptr TgTypeParser::parseJsonAndGetMessage(const ptree& data) const { result->voice = tryParseJson(&TgTypeParser::parseJsonAndGetVoice, data, "voice"); result->contact = tryParseJson(&TgTypeParser::parseJsonAndGetContact, data, "contact"); result->location = tryParseJson(&TgTypeParser::parseJsonAndGetLocation, data, "location"); + result->poll = tryParseJson(&TgTypeParser::parseJsonAndGetPoll, data, "poll"); result->newChatMember = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "new_chat_participant"); result->newChatMembers = parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetUser, data, "new_chat_members"); result->leftChatMember = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "left_chat_participant"); @@ -194,6 +196,7 @@ string TgTypeParser::parseMessage(const Message::Ptr& object) const { appendToJson(result, "forward_from_chat", parseChat(object->forwardFromChat)); appendToJson(result, "forward_from_message_id", object->forwardFromMessageId); appendToJson(result, "forward_signature", object->forwardSignature); + appendToJson(result, "forward_sender_name", object->forwardSenderName); appendToJson(result, "forward_date", object->forwardDate); appendToJson(result, "reply_to_message", parseMessage(object->replyToMessage)); appendToJson(result, "edit_date", object->editDate); @@ -208,6 +211,7 @@ string TgTypeParser::parseMessage(const Message::Ptr& object) const { appendToJson(result, "voice", parseVoice(object->voice)); appendToJson(result, "contact", parseContact(object->contact)); appendToJson(result, "location", parseLocation(object->location)); + appendToJson(result, "poll", parsePoll(object->poll)); appendToJson(result, "new_chat_member", parseUser(object->newChatMember)); appendToJson(result, "new_chat_members", parseArray(&TgTypeParser::parseUser, object->newChatMembers)); appendToJson(result, "left_chat_member", parseUser(object->leftChatMember)); @@ -385,6 +389,50 @@ string TgTypeParser::parseMaskPosition(const MaskPosition::Ptr& object) const { return result; } +Poll::Ptr TgTypeParser::parseJsonAndGetPoll(const ptree& data) const { + auto result(make_shared()); + result->id = data.get("id", 0); + result->question = data.get("question", ""); + result->options = tryParseJson(&TgTypeParser::parseJsonAndGetPollOption, data, "options"); + result->is_closed = data.get("is_closed"); + return result; +} + +string TgTypeParser::parsePoll(const Poll::Ptr& object) const { + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "id", object->id); + appendToJson(result, "question", object->question); + appendToJson(result, "options", parsePollOption(object->options)); + appendToJson(result, "is_closed", object->is_closed); + removeLastComma(result); + result += '}'; + return result; +} + + PollOption::Ptr TgTypeParser::parseJsonAndGetPollOption(const ptree& data) const { + auto result(make_shared()); + result->text = data.get("text", ""); + result->voter_count = data.get("voter_count", 0); + return result; +} + +string TgTypeParser::parsePollOption(const PollOption::Ptr& object) const { + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "text", object->text); + appendToJson(result, "voter_count", object->voter_count); + removeLastComma(result); + result += '}'; + return result; +} + Video::Ptr TgTypeParser::parseJsonAndGetVideo(const ptree& data) const { auto result(make_shared