diff options
-rw-r--r-- | include/tgbot/EventHandler.h | 8 | ||||
-rw-r--r-- | include/tgbot/TgTypeParser.h | 20 | ||||
-rw-r--r-- | src/Api.cpp | 2 | ||||
-rw-r--r-- | src/net/HttpParser.cpp | 12 |
4 files changed, 36 insertions, 6 deletions
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); } diff --git a/include/tgbot/TgTypeParser.h b/include/tgbot/TgTypeParser.h index 759dc2f..07eb1b6 100644 --- a/include/tgbot/TgTypeParser.h +++ b/include/tgbot/TgTypeParser.h @@ -110,6 +110,15 @@ public: } template<typename T> + std::vector<std::shared_ptr<T>> parseJsonAndGetArray(JsonToTgTypeFunc<T> parseFunc, const boost::property_tree::ptree& data) const { + std::vector<std::shared_ptr<T>> result; + for (const std::pair<const std::string, boost::property_tree::ptree>& innerTreeItem : data) { + result.push_back((this->*parseFunc)(innerTreeItem.second)); + } + return result; + } + + template<typename T> std::vector<std::shared_ptr<T>> parseJsonAndGetArray(JsonToTgTypeFunc<T> parseFunc, const boost::property_tree::ptree& data, const std::string& keyName) const { std::vector<std::shared_ptr<T>> result; auto treeItem = data.find(keyName); @@ -178,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; }; 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<Update::Ptr> Api::getUpdates(int32_t offset, int32_t limit, int32_t timeo if (timeout) { args.push_back(HttpReqArg("timeout", timeout)); } - return TgTypeParser::getInstance().parseJsonAndGetArray<Update>(&TgTypeParser::parseJsonAndGetUpdate, sendRequest("getUpdates", args), "result"); + return TgTypeParser::getInstance().parseJsonAndGetArray<Update>(&TgTypeParser::parseJsonAndGetUpdate, sendRequest("getUpdates", args)); } void Api::setWebhook(const string& url) const { 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<string, str string HttpParser::parseHttp(bool isRequest, const string& data) { size_t headerEnd = data.find("\r\n\r\n"); - if (headerEnd == data.npos) { + if (headerEnd != data.npos) { + headerEnd += 4; + } else { headerEnd = data.find("\n\n"); - } - if (headerEnd == data.npos) { - headerEnd = 0; + if (headerEnd != data.npos) { + headerEnd += 2; + } else { + headerEnd = 0; + } } return data.substr(headerEnd); } |