From ad8ca77ece2b3185ca546f120dc4f7a13e602aa2 Mon Sep 17 00:00:00 2001 From: JellyBrick Date: Tue, 29 May 2018 21:55:25 +0900 Subject: Bot API 2.2 (Game) update --- src/Api.cpp | 60 ++++++++++++++++++++++++++++++++++++ src/TgTypeParser.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 142 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/Api.cpp b/src/Api.cpp index 269b8a6..dfb5dcc 100644 --- a/src/Api.cpp +++ b/src/Api.cpp @@ -497,6 +497,22 @@ Message::Ptr Api::sendVoice(int64_t chatId, const string& voiceId, const string return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendVoice", args)); } +Message::Ptr Api::sendGame(int64_t chatId, const std::string& gameShortName, int32_t replyToMessageId, const InlineKeyboardMarkup::Ptr replyMarkup, bool disableNotification) const { + vector args; + args.push_back(HttpReqArg("chat_id", chatId)); + args.push_back(HttpReqArg("game_short_name", gameShortName)); + 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("sendGame", 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)); @@ -975,6 +991,50 @@ bool Api::unpinChatMessage(int64_t chatId) const { return sendRequest("unpinChatMessage", args).get("", false); } +Message::Ptr Api::setGameScore(int32_t userId, int32_t score, bool force, bool disableEditMessage, int64_t chatId, int32_t messageId, const std::string& inlineMessageId) const { + vector args; + args.push_back(HttpReqArg("user_id", user_id)); + args.push_back(HttpReqArg("score", score)); + if (force) { + args.push_back(HttpReqArg("force", force)); + } + if (disableEditMessage) { + args.push_back(HttpReqArg("disable_edit_message", disableEditMessage)); + } + if (chatId){ + args.push_back(HttpReqArg("chat_id", chat_id)); + } + if (messageId){ + args.push_back(HttpReqArg("message_id", messageId)); + } + if (!inlineMessageId.empty()){ + args.push_back(HttpReqArg("inline_message_id", inlineMessageId)); + } + return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("setGameScore", args)); +} + +vector Api::getGameHighScores(int32_t userId, int32_t score, bool force, bool disableEditMessage, int64_t chatId, int32_t messageId, const std::string& inlineMessageId) const { + vector args; + args.push_back(HttpReqArg("user_id", userId)); + args.push_back(HttpReqArg("score", score)); + if (force) { + args.push_back(HttpReqArg("force", force)); + } + if (disableEditMessage) { + args.push_back(HttpReqArg("disable_edit_message", disableEditMessage)); + } + 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)); + } + return TgTypeParser::getInstance().parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetGameHighScore, sendRequest("getGameHighScores", args)); +} + void Api::deleteMessage(int64_t chatId, int32_t messageId) const { sendRequest("deleteMessage", { HttpReqArg("chat_id", chatId), HttpReqArg("message_id", messageId) }); } diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index 4ba571f..20ed9a2 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -113,7 +113,7 @@ string TgTypeParser::parseUser(const User::Ptr& object) const { return result; } -MessageEntity::Ptr TgTypeParser::parseJsonAndGetEntity(const ptree& data) const{ +MessageEntity::Ptr TgTypeParser::parseJsonAndGetMessageEntity(const ptree& data) const{ auto result(make_shared()); result->type = data.get("type"); result->offset = data.get("offset"); @@ -138,10 +138,11 @@ Message::Ptr TgTypeParser::parseJsonAndGetMessage(const ptree& data) const { result->editDate = data.get("edit_date", 0); result->authorSignature = data.get("author_signature", ""); result->text = data.get("text", ""); - result->entities = parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetEntity, data, "entities"); - result->captionEntities = parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetEntity, data, "caption_entities"); + result->entities = parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetMessageEntity, data, "entities"); + result->captionEntities = parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetMessageEntity, data, "caption_entities"); result->audio = tryParseJson