summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJellyBrick <shlee1503@naver.com>2018-06-22 20:51:07 +0900
committerJellyBrick <shlee1503@naver.com>2018-06-22 20:51:07 +0900
commite252f0aa25bb7087698ed94698a91f9309e61460 (patch)
tree31e56f044530d32760ba50a2162d6a32976512d3
parent03cd6820af477475a196ab85971f4245057841e4 (diff)
Resolved #48, Bot API 3.0 implementation done.
-rw-r--r--include/tgbot/Api.h15
-rw-r--r--include/tgbot/TgTypeParser.h12
-rw-r--r--src/Api.cpp142
3 files changed, 135 insertions, 34 deletions
diff --git a/include/tgbot/Api.h b/include/tgbot/Api.h
index 75b01a5..efa2ad4 100644
--- a/include/tgbot/Api.h
+++ b/include/tgbot/Api.h
@@ -202,7 +202,7 @@ public:
* @param needName Optional. Pass True, if you require the user's full name to complete the order.
* @param needPhoneNumber Optional. Pass True, if you require the user's phone number to complete the order.
* @param needEmail Optional. Pass True, if you require the user's email address to complete the order.
- * @param needShippingAdress Optional. Pass True, if you require the user's shipping address to complete the order.
+ * @param needShippingAddress Optional. Pass True, if you require the user's shipping address to complete the order.
* @param sendPhoneNumberToProvider Optional. Pass True, if user's phone number should be sent to provider.
* @param sendEmailToProvider Optional. Pass True, if user's email address should be sent to provider
* @param isFlexible Optional. Pass True, if the final price depends on the shipping method.
@@ -212,17 +212,18 @@ public:
* @return On success, the sent Message is returned.
*/
Message::Ptr sendInvoice(int64_t chatId, const std::string& title, const std::string& description, const std::string& payload,
- const std::string& providerToken, const std::string& startParameter, const std::string& currency, const std::vector<LabeledPrice>& prices,
- const std::string& providerData, const std::string& photoUrl = "", int32_t photoSize = 0,
- int32_t photoWidth = 0, int32_t photoHeight = 0, bool needName = false,
- bool needPhoneNumber = false, bool needEmail = false, bool needShippingAdress = false,
- bool sendPhoneNumberToProvider = false, bool sendEmailToProvider = false, bool isFlexible = false,
- int32_t replyToMessageId = 0, const GenericReply::Ptr replyMarkup = std::make_shared<GenericReply>(), bool disableNotification = false) const;
+ const std::string& providerToken, const std::string& startParameter, const std::string& currency, const std::vector<LabeledPrice>& prices,
+ const std::string& providerData = "", const std::string& photoUrl = "", int32_t photoSize = 0,
+ int32_t photoWidth = 0, int32_t photoHeight = 0, bool needName = false,
+ bool needPhoneNumber = false, bool needEmail = false, bool needShippingAddress = false,
+ bool sendPhoneNumberToProvider = false, bool sendEmailToProvider = false, bool isFlexible = false,
+ int32_t replyToMessageId = 0, const GenericReply::Ptr replyMarkup = std::make_shared<GenericReply>(), bool disableNotification = false) const;
/**
* @brief Use this method to reply to shipping queries.
*
* If you sent an invoice requesting a shipping address and the parameter isFlexible was specified, the Bot API will send an Update with a shipping_query field to the bot.
+ *
* @param shippingQueryId Unique identifier for the query to be answered.
* @param ok Specify True if delivery to the specified address is possible and False if there are any problems (for example, if delivery to the specified address is not possible)
* @param shippingOptions Optional. Required if ok is True. A JSON-serialized array of available shipping options.
diff --git a/include/tgbot/TgTypeParser.h b/include/tgbot/TgTypeParser.h
index 1784383..dfb10a7 100644
--- a/include/tgbot/TgTypeParser.h
+++ b/include/tgbot/TgTypeParser.h
@@ -418,30 +418,30 @@ private:
}
template<typename T>
- void appendToJson(std::string& json, const std::string& varName, const std::shared_ptr<T>& value) const {
+ inline void appendToJson(std::string& json, const std::string& varName, const std::shared_ptr<T>& value) const {
if (value == nullptr) {
return;
}
json += '"';
json += varName;
- json += "\":";
+ json += R"(":)";
json += value;
json += ',';
}
template<typename T>
- void appendToJson(std::string& json, const std::string& varName, const T& value) const {
+ inline void appendToJson(std::string& json, const std::string& varName, const T& value) const {
json += '"';
json += varName;
- json += "\":";
+ json += R"(":)";
json += value;
json += ',';
}
- void appendToJson(std::string& json, const std::string& varName, const bool& value) const {
+ inline void appendToJson(std::string& json, const std::string& varName, const bool& value) const {
json += '"';
json += varName;
- json += "\":";
+ json += R"(":)";
json += (value ? "true" : "false");
json += ',';
}
diff --git a/src/Api.cpp b/src/Api.cpp
index e6c7c4b..cd9fac8 100644
--- a/src/Api.cpp
+++ b/src/Api.cpp
@@ -229,6 +229,96 @@ Message::Ptr Api::sendDocument(int64_t chatId, const string& document, const str
return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendDocument", args));
}
+Message::Ptr sendInvoice(int64_t chatId, const std::string& title, const std::string& description, const std::string& payload,
+ const std::string& providerToken, const std::string& startParameter, const std::string& currency, const std::vector<LabeledPrice>& prices,
+ const std::string& providerData, const std::string& photoUrl, int32_t photoSize,
+ int32_t photoWidth, int32_t photoHeight, bool needName,
+ bool needPhoneNumber, bool needEmail, bool needShippingAddress,
+ bool sendPhoneNumberToProvider, bool sendEmailToProvider, bool isFlexible,
+ int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const {
+ vector<HttpReqArg> args;
+ args.reserve(23);
+ args.emplace_back("chat_id", chatId);
+ args.emplace_back("title", title);
+ args.emplace_back("description", description);
+ args.emplace_back("payload", payload);
+ args.emplace_back("provider_token", providerToken);
+ args.emplace_back("start_parameter", startParameter);
+ args.emplace_back("currency", currency);
+ args.emplace_back("prices", TgTypeParser::getInstance().parseArray<LabeledPrice>(&TgTypeParser::parseLabeledPrice, prices);
+ if (!providerData.empty()) {
+ args.emplace_back("provider_data", providerData);
+ }
+ if (!photoUrl.empty()) {
+ args.emplace_back("photo_url", photoUrl);
+ }
+ if (photoSize) {
+ args.emplace_back("photo_size", photoSize);
+ }
+ if (photoWidth) {
+ args.emplace_back("photo_width", photoWidth);
+ }
+ if (photoHeight) {
+ args.emplace_back("photo_height", photoHeight);
+ }
+ if (needName) {
+ args.emplace_back("need_name", needName);
+ }
+ if (needPhoneNumber) {
+ args.emplace_back("need_phone_number", needPhoneNumber);
+ }
+ if (needEmail) {
+ args.emplace_back("need_email", needEmail);
+ }
+ if (needShippingAddress) {
+ args.emplace_back("need_shipping_address", needShippingAddress);
+ }
+ if (sendPhoneNumberToProvider) {
+ args.emplace_back("send_phone_number_to_provider", sendPhoneNumberToProvider);
+ }
+ if (sendEmailToProvider) {
+ args.emplace_back("send_email_to_provider", sendEmailToProvider);
+ }
+ if (isFlexible) {
+ args.emplace_back("is_flexible", isFlexible);
+ }
+ if (replyToMessageId) {
+ args.emplace_back("reply_to_message_id", replyToMessageId);
+ }
+ if (replyMarkup) {
+ args.emplace_back("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup));
+ }
+ if (disableNotification){
+ args.emplace_back("disable_notification", disableNotification);
+ }
+ return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendInvoice", args));
+}
+
+bool answerShippingQuery(const std::string& shippingQueryId, bool ok, const std::vector<ShippingOption>& shippingOptions, const std::string& errorMessage) const {
+ vector<HttpReqArg> args;
+ args.reserve(4);
+ args.emplace_back("shipping_query_id", shippingQueryId);
+ args.emplace_back("ok", ok);
+ if (ShippingOptions != nullptr) {
+ args.emplace_back("shipping_options", TgTypeParser::getInstance().parseArray<ShippingOption>(&TgTypeParser::parseShippingOption, shippingOptions));
+ }
+ if (!errorMessage.empty()) {
+ args.emplace_back("error_message", errorMessage);
+ }
+ return sendRequest("answerShippingQuery", args).get<bool>("", false);
+}
+
+bool answerPreCheckoutQuery(const std::string& preCheckoutQueryId, bool ok, const std::string& errorMessage) const {
+ vector<HttpReqArg> args;
+ args.reserve(3);
+ args.emplace_back("shipping_query_id", shippingQueryId);
+ args.emplace_back("ok", ok);
+ if (!errorMessage.empty()) {
+ args.emplace_back("error_message", errorMessage);
+ }
+ return sendRequest("answerPreCheckoutQuery", args).get<bool>("", false);
+}
+
Message::Ptr Api::sendSticker(int64_t chatId, const InputFile::Ptr sticker, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const {
vector<HttpReqArg> args;
args.reserve(5);
@@ -286,8 +376,12 @@ bool Api::createNewStickerSet(int32_t userId, const string& name, const string&
args.emplace_back("title", title);
args.emplace_back("png_sticker", pngSticker->data, true, pngSticker->mimeType, pngSticker->fileName);
args.emplace_back("emojis", emojis);
- args.emplace_back("contains_mask", containsMasks);
- args.emplace_back("mask_position", TgTypeParser::getInstance().parseMaskPosition(maskPosition));
+ if (containsMasks) {
+ args.emplace_back("contains_mask", containsMasks);
+ }
+ if (maskPosition != nullptr) {
+ args.emplace_back("mask_position", TgTypeParser::getInstance().parseMaskPosition(maskPosition));
+ }
return sendRequest("createNewStickerSet", args).get<bool>("", false);
}
@@ -299,8 +393,12 @@ bool Api::createNewStickerSet(int32_t userId, const string& name, const string&
args.emplace_back("title", title);
args.emplace_back("png_sticker", pngSticker);
args.emplace_back("emojis", emojis);
- args.emplace_back("contains_mask", containsMasks);
- args.emplace_back("mask_position", TgTypeParser::getInstance().parseMaskPosition(maskPosition));
+ if (containsMasks) {
+ args.emplace_back("contains_mask", containsMasks);
+ }
+ if (maskPosition != nullptr) {
+ args.emplace_back("mask_position", TgTypeParser::getInstance().parseMaskPosition(maskPosition));
+ }
return sendRequest("createNewStickerSet", args).get<bool>("", false);
}
@@ -312,7 +410,9 @@ bool Api::addStickerToSet(int32_t userId, const string& name, const string& titl
args.emplace_back("title", title);
args.emplace_back("png_sticker", pngSticker->data, true, pngSticker->mimeType, pngSticker->fileName);
args.emplace_back("emojis", emojis);
- args.emplace_back("mask_position", TgTypeParser::getInstance().parseMaskPosition(maskPosition));
+ if (maskPosition != nullptr) {
+ args.emplace_back("mask_position", TgTypeParser::getInstance().parseMaskPosition(maskPosition));
+ }
return sendRequest("addStickerToSet", args).get<bool>("", false);
}
@@ -324,7 +424,9 @@ bool Api::addStickerToSet(int32_t userId, const string& name, const string& titl
args.emplace_back("title", title);
args.emplace_back("png_sticker", pngSticker);
args.emplace_back("emojis", emojis);
- args.emplace_back("mask_position", TgTypeParser::getInstance().parseMaskPosition(maskPosition));
+ if (maskPosition != nullptr) {
+ args.emplace_back("mask_position", TgTypeParser::getInstance().parseMaskPosition(maskPosition));
+ }
return sendRequest("addStickerToSet", args).get<bool>("", false);
}
@@ -848,16 +950,17 @@ vector<Update::Ptr> Api::getUpdates(int32_t offset, int32_t limit, int32_t timeo
void Api::setWebhook(const string& url, const InputFile::Ptr certificate, int32_t maxConnection, const StringArrayPtr &allowedUpdates) const {
vector<HttpReqArg> args;
args.reserve(4);
- if (!url.empty())
+ if (!url.empty()) {
args.emplace_back("url", url);
- if (certificate != nullptr)
+ }
+ if (certificate != nullptr) {
args.emplace_back("certificate", certificate->data, true, certificate->mimeType, certificate->fileName);
- if (maxConnection != 40)
+ }
+ if (maxConnection != 40) {
args.emplace_back("max_connections", maxConnection);
-
- if (allowedUpdates != nullptr)
- {
- string allowedUpdatesJson = TgTypeParser::getInstance().parseArray<string>(
+ }
+ if (allowedUpdates != nullptr) {
+ auto allowedUpdatesJson = TgTypeParser::getInstance().parseArray<string>(
[](const string &s)->string {
return s;
}, *allowedUpdates);
@@ -868,22 +971,19 @@ void Api::setWebhook(const string& url, const InputFile::Ptr certificate, int32_
}
bool Api::deleteWebhook() const {
- ptree p = sendRequest("deleteWebhook");
- return p.get<bool>("", false);
+ return sendRequest("deleteWebhook").get<bool>("", false);
}
WebhookInfo::Ptr Api::getWebhookInfo() const {
ptree p = sendRequest("getWebhookInfo");
- if (!p.get_child_optional("url"))
+ if (!p.get_child_optional("url")) {
return nullptr;
-
- if (p.get<string>("url","") != string(""))
- {
+ }
+ if (p.get<string>("url","") != string("")) {
return TgTypeParser::getInstance().parseJsonAndGetWebhookInfo(p);
}
- else
- {
+ else {
return nullptr;
}
}