From 1d238b70e2655e64b1cb1a51357a9eb0c753181d Mon Sep 17 00:00:00 2001 From: llnulldisk <48621230+llnulldisk@users.noreply.github.com> Date: Wed, 5 Oct 2022 13:57:24 +0200 Subject: Escape poll options (#128) --- include/tgbot/tools/StringTools.h | 9 +++++++++ src/Api.cpp | 4 ++-- src/tools/StringTools.cpp | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/include/tgbot/tools/StringTools.h b/include/tgbot/tools/StringTools.h index fa3a2f5..a833fc2 100644 --- a/include/tgbot/tools/StringTools.h +++ b/include/tgbot/tools/StringTools.h @@ -62,6 +62,15 @@ std::string urlEncode(const std::string& value, const std::string& additionalLeg TGBOT_API std::string urlDecode(const std::string& value); +/** + * Escapes a string with illegal characters ("\/) for json + * + * @param value input string + * + * @return An encoded string + */ +std::string escapeJsonString(const std::string& value); + /** * Splits string to smaller substrings which have between them a delimiter. Resulting substrings won't have delimiter. * @param str Source string diff --git a/src/Api.cpp b/src/Api.cpp index ed1f07a..7ceb75e 100644 --- a/src/Api.cpp +++ b/src/Api.cpp @@ -903,7 +903,7 @@ Message::Ptr Api::sendPoll(boost::variant chat args.emplace_back("question", question); args.emplace_back("options", _tgTypeParser.parseArray( [](const std::string& option)->std::string { - return "\"" + option + "\""; + return "\"" + StringTools::escapeJsonString(option) + "\""; }, options)); if (!isAnonymous) { args.emplace_back("is_anonymous", isAnonymous); @@ -1760,7 +1760,7 @@ std::vector Api::getCustomEmojiStickers(const std::vector([] (const std::string& customEmojiId) -> std::string { - return "\"" + customEmojiId + "\""; + return "\"" + StringTools::escapeJsonString(customEmojiId) + "\""; }, customEmojiIds)); return _tgTypeParser.parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetSticker, sendRequest("getCustomEmojiStickers", args)); diff --git a/src/tools/StringTools.cpp b/src/tools/StringTools.cpp index 653047c..3e5e6c4 100644 --- a/src/tools/StringTools.cpp +++ b/src/tools/StringTools.cpp @@ -101,4 +101,22 @@ string urlDecode(const string& value) { return result; } +std::string escapeJsonString(const std::string& value) { + string result; + + for (const char& c : value) { + switch (c) { + case '"': + case '\\': + case '/': + result += '\\'; + break; + } + + result += c; + } + + return result; +} + } -- cgit v1.2.3