From 6b694797f2e20ca8e74d36156e92abceaeb4dcdb Mon Sep 17 00:00:00 2001 From: SaidSafwan Date: Thu, 31 Jul 2025 00:51:42 +0545 Subject: [PATCH] feat: add createSms API to Messaging service with example --- CMakeLists.txt | 3 +++ examples/messaging/messages/createSms.cpp | 23 ++++++++++++++++ include/classes/Messaging.hpp | 13 ++++++++- src/services/Messaging.cpp | 33 ++++++++++++++++++++++- 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 examples/messaging/messages/createSms.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d31750..b758e4f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,3 +48,6 @@ set(HEADERS install(DIRECTORY include/ DESTINATION /usr/local/include/AppwriteSDK) install(FILES ${HEADERS} DESTINATION /usr/local/include/AppwriteSDK) install(TARGETS AppwriteSDK ARCHIVE DESTINATION /usr/local/lib) + +add_executable(example-messaging-create-sms examples/messaging/messages/createSms.cpp) +target_link_libraries(example-messaging-create-sms AppwriteSDK) \ No newline at end of file diff --git a/examples/messaging/messages/createSms.cpp b/examples/messaging/messages/createSms.cpp new file mode 100644 index 0000000..22816f4 --- /dev/null +++ b/examples/messaging/messages/createSms.cpp @@ -0,0 +1,23 @@ +#include "Appwrite.hpp" +#include + +int main() { + std::string projectId = ""; + std::string apiKey = ""; + + Appwrite appwrite(projectId, apiKey); + + std::string recipient = ""; // e.g., +911234567890 + std::string message = "Hello from C++ Appwrite SDK! (SMS Test)"; + + try { + std::string response = appwrite.getMessaging().createSms( + recipient, message + ); + std::cout << "SMS Message Created!\nResponse: " << response << std::endl; + } catch (const AppwriteException& ex) { + std::cerr << "Exception: " << ex.what() << std::endl; + } + + return 0; +} diff --git a/include/classes/Messaging.hpp b/include/classes/Messaging.hpp index b2f40c6..ffd4467 100644 --- a/include/classes/Messaging.hpp +++ b/include/classes/Messaging.hpp @@ -9,6 +9,7 @@ #include "enums/HttpStatus.hpp" #include "exceptions/AppwriteException.hpp" #include +#include /** * @class Messaging @@ -138,7 +139,17 @@ class Messaging { const std::string& content, const std::vector& topics = {}, const std::vector& targets = {}); - + /** + * @brief Create a new SMS message. + * + * Sends an SMS message to a specific recipient phone number. + * + * @param recipient Phone number of the recipient (e.g., +911234567890). + * @param message Text message content. + * @return JSON response. + */ + std::string createSms(const std::string &recipient, + const std::string &message); private: std::string projectId; ///< Project ID std::string apiKey; ///< API Key diff --git a/src/services/Messaging.cpp b/src/services/Messaging.cpp index f0414eb..b4d9b17 100644 --- a/src/services/Messaging.cpp +++ b/src/services/Messaging.cpp @@ -442,4 +442,35 @@ std::string Messaging::createMessage(const std::string& messageId, throw AppwriteException("Error creating email message. Status code: " + std::to_string(statusCode) + "\n\nResponse: " + response); } -} \ No newline at end of file +} + +std::string Messaging::createSms(const std::string &recipient, + const std::string &message) { + if (recipient.empty()) { + throw AppwriteException("Missing required parameter: 'recipient'"); + } + if (message.empty()) { + throw AppwriteException("Missing required parameter: 'message'"); + } + + std::string url = Config::API_BASE_URL + "/messaging/messages/sms"; + + std::string payload = + R"({"recipient":")" + Utils::escapeJsonString(recipient) + + R"(","content":")" + Utils::escapeJsonString(message) + R"("})"; + + std::vector headers = Config::getHeaders(projectId); + headers.push_back("X-Appwrite-Key: " + apiKey); + headers.push_back("Content-Type: application/json"); + + std::string response; + int statusCode = Utils::postRequest(url, payload, headers, response); + + if (statusCode == HttpStatus::CREATED || statusCode == HttpStatus::OK) { + return response; + } else { + throw AppwriteException("Error creating SMS message. Status code: " + + std::to_string(statusCode) + + "\n\nResponse: " + response); + } +}