From 2d7d1a269383ec1db99b6759f9c58ba08d1d29fe Mon Sep 17 00:00:00 2001 From: Konstantin Kukin Date: Fri, 30 Dec 2016 11:38:22 +0300 Subject: add Webhook functions --- include/tgbot/Api.h | 18 +++++++++++++++++- include/tgbot/TgTypeParser.h | 14 ++++++++++++-- src/Api.cpp | 36 +++++++++++++++++++++++++++++++++++- 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> 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 - std::vector parseJsonAndGetArray(std::function parseFunc, const boost::property_tree::ptree& data) const { + std::vector parseJsonAndGetArray(std::function parseFunc, const boost::property_tree::ptree& data, const std::string& keyName) const { std::vector result; - for (const std::pair& innerTreeItem : data) { + auto treeItem = data.find(keyName); + if (treeItem == data.not_found()) { + return result; + } + for (const std::pair& innerTreeItem : treeItem->second) { result.push_back(parseFunc(innerTreeItem.second)); } return result; @@ -300,6 +304,8 @@ public: template std::string parseArray(TgTypeToJsonFunc parseFunc, const std::vector>& objects) const { + if (objects.empty()) + return ""; std::string result; result += '['; for (const std::shared_ptr& item : objects) { @@ -313,6 +319,8 @@ public: template std::string parseArray(std::function parseFunc, const std::vector& objects) const { + if (objects.empty()) + return ""; std::string result; result += '['; for (const T& item : objects) { @@ -326,6 +334,8 @@ public: template std::string parse2DArray(TgTypeToJsonFunc parseFunc, const std::vector>>& objects) const { + if (objects.empty()) + return ""; std::string result; result += '['; for (const std::vector>& 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 Api::getUpdates(int32_t offset, int32_t limit, int32_t timeo return TgTypeParser::getInstance().parseJsonAndGetArray(&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 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( + [](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("", false); +} + +WebhookInfo::Ptr Api::getWebhookInfo() const +{ + ptree p = sendRequest("getWebhookInfo"); + + if (!p.get_child_optional("url")) + return nullptr; + + if (p.get("url","")!=string("")) + { + return TgTypeParser::getInstance().parseJsonAndGetWebhookInfo(p); + } + else + { + return nullptr; + } +} + void Api::answerInlineQuery(const std::string& inlineQueryId, const std::vector& results, int32_t cacheTime, bool isPersonal, const std::string& nextOffset) const { vector 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(""); } - , data); + , data, "allowed_updates"); return result; } -- cgit v1.2.3