From 4c34c9623735cdd7d11c333e54ad8723abb1be8b Mon Sep 17 00:00:00 2001 From: Florian Scheibner Date: Thu, 12 Nov 2015 14:27:07 +0100 Subject: Fix getMessages call do not request "result" subitem twice --- include/tgbot/TgTypeParser.h | 9 +++++++++ src/Api.cpp | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/tgbot/TgTypeParser.h b/include/tgbot/TgTypeParser.h index 759dc2f..f7c9791 100644 --- a/include/tgbot/TgTypeParser.h +++ b/include/tgbot/TgTypeParser.h @@ -109,6 +109,15 @@ public: return (this->*parseFunc)(treeItem->second); } + template + std::vector> parseJsonAndGetArray(JsonToTgTypeFunc parseFunc, const boost::property_tree::ptree& data) const { + std::vector> result; + for (const std::pair& innerTreeItem : data) { + result.push_back((this->*parseFunc)(innerTreeItem.second)); + } + return result; + } + template std::vector> parseJsonAndGetArray(JsonToTgTypeFunc parseFunc, const boost::property_tree::ptree& data, const std::string& keyName) const { std::vector> result; diff --git a/src/Api.cpp b/src/Api.cpp index 7e05621..9c8259f 100644 --- a/src/Api.cpp +++ b/src/Api.cpp @@ -246,7 +246,7 @@ vector Api::getUpdates(int32_t offset, int32_t limit, int32_t timeo if (timeout) { args.push_back(HttpReqArg("timeout", timeout)); } - return TgTypeParser::getInstance().parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetUpdate, sendRequest("getUpdates", args), "result"); + return TgTypeParser::getInstance().parseJsonAndGetArray(&TgTypeParser::parseJsonAndGetUpdate, sendRequest("getUpdates", args)); } void Api::setWebhook(const string& url) const { -- cgit v1.2.3 From 61cd5d3c264fd497af4df4e114d67dc7ead16a58 Mon Sep 17 00:00:00 2001 From: Florian Scheibner Date: Thu, 12 Nov 2015 14:28:12 +0100 Subject: Parse commands correctly previously the last letter of a command was removed when a space was present e.g. "/add hello" was parsed as command = "ad" --- include/tgbot/EventHandler.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/include/tgbot/EventHandler.h b/include/tgbot/EventHandler.h index 1ec2cbe..e4fac7a 100644 --- a/include/tgbot/EventHandler.h +++ b/include/tgbot/EventHandler.h @@ -38,7 +38,13 @@ public: inline void handleUpdate(const Update::Ptr& update) const { _broadcaster->broadcastAnyMessage(update->message); if (StringTools::startsWith(update->message->text, "/")) { - std::string command = update->message->text.substr(1, update->message->text.find(' ') - 2); + unsigned long spacePosition = update->message->text.find(' '); + std::string command; + if (spacePosition != update->message->text.npos) { + command = update->message->text.substr(1, spacePosition - 1); + } else { + command = update->message->text.substr(1, update->message->text.size() - 1); + } if (!_broadcaster->broadcastCommand(command, update->message)) { _broadcaster->broadcastUnknownCommand(update->message); } -- cgit v1.2.3 From e96015a3043f7da0359dc8887b543803041ceef9 Mon Sep 17 00:00:00 2001 From: Florian Scheibner Date: Thu, 12 Nov 2015 14:28:53 +0100 Subject: Output boolean values correctly as json previously 1 was printed instead of true fixes showing custom keyboards --- include/tgbot/TgTypeParser.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/tgbot/TgTypeParser.h b/include/tgbot/TgTypeParser.h index f7c9791..07eb1b6 100644 --- a/include/tgbot/TgTypeParser.h +++ b/include/tgbot/TgTypeParser.h @@ -187,6 +187,17 @@ private: json += ','; } + void appendToJson(std::string& json, const std::string& varName, const bool& value) const { + if (value == 0) { + return; + } + json += '"'; + json += varName; + json += "\":"; + json += (value ? "true" : "false"); + json += ','; + } + void appendToJson(std::string& json, const std::string& varName, const std::string& value) const; }; -- cgit v1.2.3 From 2f857ce94ae9fe80bb0d0bd96aa40e262a24f30a Mon Sep 17 00:00:00 2001 From: Florian Scheibner Date: Thu, 12 Nov 2015 14:29:42 +0100 Subject: Remove leading \r\n\r\n before actual json string --- src/net/HttpParser.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/net/HttpParser.cpp b/src/net/HttpParser.cpp index 7c4f9e2..4b57bc1 100644 --- a/src/net/HttpParser.cpp +++ b/src/net/HttpParser.cpp @@ -196,11 +196,15 @@ string HttpParser::parseHttp(bool isRequest, const string& data, map