diff options
-rw-r--r-- | include/tgbot/Api.h | 18 | ||||
-rw-r--r-- | include/tgbot/TgTypeParser.h | 14 | ||||
-rw-r--r-- | src/Api.cpp | 36 | ||||
-rw-r--r-- | src/TgTypeParser.cpp | 2 |
4 files changed, 65 insertions, 5 deletions
diff --git a/include/tgbot/Api.h b/include/tgbot/Api.h index 0a2b7bd..928a017 100644 --- a/include/tgbot/Api.h +++ b/include/tgbot/Api.h @@ -37,6 +37,7 @@ #include "tgbot/types/Update.h" #include "tgbot/types/InlineQueryResult.h" #include "tgbot/types/Venue.h" +#include "tgbot/types/WebhookInfo.h" namespace TgBot { @@ -48,6 +49,8 @@ class Bot; */ class Api { +typedef std::shared_ptr<std::vector<std::string>> StringArrayPtr; + friend class Bot; public: @@ -318,7 +321,20 @@ public: * Ports currently supported for Webhooks: 443, 80, 88, 8443. * @param url Optional. HTTPS url to send updates to. Use an empty string to remove webhook integration. */ - void setWebhook(const std::string& url = "", const InputFile::Ptr& certificate = nullptr) const; + void setWebhook(const std::string& url = "", const InputFile::Ptr& certificate = nullptr, int32_t maxConnection = 40, const StringArrayPtr &allowedUpdates = nullptr) const; + + /** + * Use this method to remove webhook integration if you decide to switch back to getUpdates. + * Returns True on success. Requires no parameters. + */ + bool deleteWebhook() const; + + /** + * Use this method to get current webhook status. + * Requires no parameters. On success, returns a WebhookInfo object. + * If the bot is using getUpdates, will return an object with the url field empty. + */ + WebhookInfo::Ptr getWebhookInfo() const; /** * Use this method to send answers to an inline query. diff --git a/include/tgbot/TgTypeParser.h b/include/tgbot/TgTypeParser.h index 48f7338..89c6dec 100644 --- a/include/tgbot/TgTypeParser.h +++ b/include/tgbot/TgTypeParser.h @@ -260,9 +260,13 @@ public: } template<typename T> - std::vector<T> parseJsonAndGetArray(std::function<T(const boost::property_tree::ptree&)> parseFunc, const boost::property_tree::ptree& data) const { + std::vector<T> parseJsonAndGetArray(std::function<T(const boost::property_tree::ptree&)> parseFunc, const boost::property_tree::ptree& data, const std::string& keyName) const { std::vector<T> result; - for (const std::pair<const std::string, boost::property_tree::ptree>& innerTreeItem : data) { + auto treeItem = data.find(keyName); + if (treeItem == data.not_found()) { + return result; + } + for (const std::pair<const std::string, boost::property_tree::ptree>& innerTreeItem : treeItem->second) { result.push_back(parseFunc(innerTreeItem.second)); } return result; @@ -300,6 +304,8 @@ public: template<typename T> std::string parseArray(TgTypeToJsonFunc<T> parseFunc, const std::vector<std::shared_ptr<T>>& objects) const { + if (objects.empty()) + return ""; std::string result; result += '['; for (const std::shared_ptr<T>& item : objects) { @@ -313,6 +319,8 @@ public: template<typename T> std::string parseArray(std::function<T(const T&)> parseFunc, const std::vector<T>& objects) const { + if (objects.empty()) + return ""; std::string result; result += '['; for (const T& item : objects) { @@ -326,6 +334,8 @@ public: template<typename T> std::string parse2DArray(TgTypeToJsonFunc<T> parseFunc, const std::vector<std::vector<std::shared_ptr<T>>>& objects) const { + if (objects.empty()) + return ""; std::string result; result += '['; for (const std::vector<std::shared_ptr<T>>& item : objects) { diff --git a/src/Api.cpp b/src/Api.cpp index 54cb692..c4baa2b 100644 --- a/src/Api.cpp +++ b/src/Api.cpp @@ -381,16 +381,50 @@ vector<Update::Ptr> Api::getUpdates(int32_t offset, int32_t limit, int32_t timeo return TgTypeParser::getInstance().parseJsonAndGetArray<Update>(&TgTypeParser::parseJsonAndGetUpdate, sendRequest("getUpdates", args)); } -void Api::setWebhook(const string& url, const InputFile::Ptr& certificate) const { +void Api::setWebhook(const string& url, const InputFile::Ptr& certificate, int32_t maxConnection, const StringArrayPtr &allowedUpdates) const { vector<HttpReqArg> args; if (!url.empty()) args.push_back(HttpReqArg("url", url)); if (certificate != nullptr) args.push_back(HttpReqArg("certificate", certificate->data, true, certificate->mimeType, certificate->fileName)); + if (maxConnection!=40) + args.push_back(HttpReqArg("max_connections", maxConnection)); + + if (allowedUpdates!=nullptr) + { + string allowedUpdatesJson = TgTypeParser::getInstance().parseArray<std::string>( + [](const std::string &s)->std::string { + return s; + }, *allowedUpdates); + args.push_back(HttpReqArg("allowed_updates", allowedUpdatesJson)); + } sendRequest("setWebhook", args); } +bool Api::deleteWebhook() const +{ + ptree p = sendRequest("deleteWebhook"); + return p.get<bool>("", false); +} + +WebhookInfo::Ptr Api::getWebhookInfo() const +{ + ptree p = sendRequest("getWebhookInfo"); + + if (!p.get_child_optional("url")) + return nullptr; + + if (p.get<string>("url","")!=string("")) + { + return TgTypeParser::getInstance().parseJsonAndGetWebhookInfo(p); + } + else + { + return nullptr; + } +} + void Api::answerInlineQuery(const std::string& inlineQueryId, const std::vector<InlineQueryResult::Ptr>& results, int32_t cacheTime, bool isPersonal, const std::string& nextOffset) const { vector<HttpReqArg> args; diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index 6b0914b..71da2ac 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -1370,7 +1370,7 @@ WebhookInfo::Ptr TgTypeParser::parseJsonAndGetWebhookInfo(const boost::property_ [](const boost::property_tree::ptree& innerData)->std::string { return innerData.get<std::string>(""); } - , data); + , data, "allowed_updates"); return result; } |