Skip to content

Commit

Permalink
added answerPreCheckoutQuery, answerShippingQuery payments api methods
Browse files Browse the repository at this point in the history
  • Loading branch information
baderouaich committed Dec 27, 2023
1 parent 42b715b commit 3fccd2d
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 1 deletion.
34 changes: 34 additions & 0 deletions include/tgbotxx/Api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace tgbotxx {
struct WebhookInfo;
struct Poll;
struct LabeledPrice;
struct ShippingOption;

/// @brief Api Methods https://core.telegram.org/bots/api#available-methods
/// @note All methods in the Bot API are case-insensitive.
Expand Down Expand Up @@ -1501,6 +1502,39 @@ namespace tgbotxx {
bool isFlexible = false) const;


/// @brief If you sent an invoice requesting a shipping address and the parameter isFlexible was specified,
/// the Bot API will send an Update with a shippingQuery field to the bot.
/// Use this method to reply to shipping queries. On success, True is returned.
/// @param shippingQueryId Unique identifier for the query to be answered
/// @param ok Pass 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. Array of ShippingOption, Required if ok is True. A JSON-serialized array of available shipping options.
/// @param errorMessage Optional. Required if ok is False. Error message in human readable form that explains why it is impossible to complete the order (e.g. "Sorry, delivery to your desired address is unavailable').
/// Telegram will display this message to the user.
/// @returns True on success.
/// @throws Exception on failure
/// @ref https://core.telegram.org/bots/api#answershippingquery
bool answerShippingQuery(const std::string& shippingQueryId,
bool ok,
const std::vector<Ptr<ShippingOption>>& shippingOptions = std::vector<Ptr<ShippingOption>>(),
const std::string& errorMessage = "") const;


/// @brief Once the user has confirmed their payment and shipping details, the Bot API sends the final confirmation in the form of an Update with the field preCheckoutQuery.
/// Use this method to respond to such pre-checkout queries. On success, True is returned.
/// @note: The Bot API must receive an answer within 10 seconds after the pre-checkout query was sent.
/// @param preCheckoutQueryId Unique identifier for the query to be answered
/// @param ok Specify True if everything is alright (goods are available, etc.) and the bot is ready to proceed with the order. Use False if there are any problems.
/// @param errorMessage Optional. Required if ok is False. Error message in human readable form that explains the reason for failure to proceed with the checkout
/// (e.g. "Sorry, somebody just bought the last of our amazing black T-shirts while you were busy filling out your payment details. Please choose a different color or garment!").
/// Telegram will display this message to the user.
/// @returns True on success.
/// @throws Exception on failure
/// @ref https://core.telegram.org/bots/api#answerprecheckoutquery
bool answerPreCheckoutQuery(const std::string& preCheckoutQueryId,
bool ok,
const std::string& errorMessage = "") const;


public: /// Updates methods https://core.telegram.org/bots/api#getting-updates
/// @brief Use this method to receive incoming updates using long polling.
/// @param offset Identifier of the first update to be returned. Must be greater by one than the highest
Expand Down
40 changes: 40 additions & 0 deletions include/tgbotxx/objects/ShippingOption.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once
#include <tgbotxx/objects/LabeledPrice.hpp>
#include <tgbotxx/objects/Object.hpp>

namespace tgbotxx {
/// @brief This object represents one shipping option.
/// @ref https://core.telegram.org/bots/api#shippingoption
struct ShippingOption {
ShippingOption() = default;
explicit ShippingOption(const nl::json& json) {
fromJson(json);
}

/// @brief Shipping option identifier
std::string id;

/// @brief Option title
std::string title;

/// @brief List of price portions
std::vector<Ptr<LabeledPrice>> prices;

/// @brief Serializes this object to JSON
/// @returns JSON representation of this object
nl::json toJson() const {
nl::json json = nl::json::object();
OBJECT_SERIALIZE_FIELD(json, "id", id);
OBJECT_SERIALIZE_FIELD(json, "title", title);
OBJECT_SERIALIZE_FIELD_PTR_ARRAY(json, "prices", prices);
return json;
}

/// @brief Deserializes this object from JSON
void fromJson(const nl::json& json) {
OBJECT_DESERIALIZE_FIELD(json, "id", id, "", false);
OBJECT_DESERIALIZE_FIELD(json, "title", title, "", false);
OBJECT_DESERIALIZE_FIELD_PTR_ARRAY(json, "prices", prices, false);
}
};
}
36 changes: 35 additions & 1 deletion src/Api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
#include <tgbotxx/objects/WebAppInfo.hpp>
#include <tgbotxx/objects/WebhookInfo.hpp>
#include <tgbotxx/objects/WriteAccessAllowed.hpp>
#include <tgbotxx/objects/ShippingOption.hpp>

#include <utility>
using namespace tgbotxx;
Expand Down Expand Up @@ -2241,4 +2242,37 @@ std::string Api::createInvoiceLink(const std::string& title,
data.parts.emplace_back("is_flexible", isFlexible);

return sendRequest("createInvoiceLink", data);
}
}

bool Api::answerShippingQuery(const std::string& shippingQueryId,
bool ok,
const std::vector<Ptr<ShippingOption>>& shippingOptions,
const std::string& errorMessage) const {
cpr::Multipart data{};
data.parts.reserve(4);
data.parts.emplace_back("shipping_query_id", shippingQueryId);
data.parts.emplace_back("ok", ok);
if(not shippingOptions.empty()) {
nl::json shippingOptionsJson = nl::json::array();
for(const Ptr<ShippingOption>& opt : shippingOptions)
shippingOptionsJson.push_back(opt->toJson());
data.parts.emplace_back("shipping_options", shippingOptionsJson.dump());
}
if(not errorMessage.empty())
data.parts.emplace_back("error_message", errorMessage);

return sendRequest("answerShippingQuery", data);
}

bool Api::answerPreCheckoutQuery(const std::string& preCheckoutQueryId,
bool ok,
const std::string& errorMessage) const {
cpr::Multipart data{};
data.parts.reserve(3);
data.parts.emplace_back("pre_checkout_query_id", preCheckoutQueryId);
data.parts.emplace_back("ok", ok);
if(not errorMessage.empty())
data.parts.emplace_back("error_message", errorMessage);

return sendRequest("answerPreCheckoutQuery", data);
}

0 comments on commit 3fccd2d

Please sign in to comment.