Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines +52 to +53
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No changes to be made in this file

23 changes: 23 additions & 0 deletions examples/messaging/messages/createSms.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "Appwrite.hpp"
#include <iostream>

int main() {
std::string projectId = "<your_project_id>";
std::string apiKey = "<your_api_key>";

Appwrite appwrite(projectId, apiKey);

std::string recipient = "<recipient_number>"; // 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;
}
13 changes: 12 additions & 1 deletion include/classes/Messaging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "enums/HttpStatus.hpp"
#include "exceptions/AppwriteException.hpp"
#include <string>
#include <vector>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is vector imported ?


/**
* @class Messaging
Expand Down Expand Up @@ -138,7 +139,17 @@ class Messaging {
const std::string& content,
const std::vector<std::string>& topics = {},
const std::vector<std::string>& 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
Expand Down
33 changes: 32 additions & 1 deletion src/services/Messaging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

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<std::string> 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);
}
}