Skip to content

Commit 1479887

Browse files
authored
Added updateSms method to update an existing sms message (#142)
* API added to update an existing sms and tested successfully * fix: removed typo from updateSms
1 parent 707ff90 commit 1479887

File tree

5 files changed

+137
-7
lines changed

5 files changed

+137
-7
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ createEmail: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/createEmail.cpp
291291
@mkdir -p ./$(TESTS_DIR)
292292
$(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/createEmail $(SRCS) $(EXAMPLES_DIR)/messaging/messages/createEmail.cpp $(LDFLAGS)
293293

294+
updateSms: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/updateSms.cpp
295+
@mkdir -p ./$(TESTS_DIR)
296+
$(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/updateSms $(SRCS) $(EXAMPLES_DIR)/messaging/messages/updateSms.cpp $(LDFLAGS)
297+
294298
# Messaging - Topics
295299
getTopic: $(SRCS) $(EXAMPLES_DIR)/messaging/topics/getTopic.cpp
296300
@mkdir -p ./$(TESTS_DIR)

examples/messaging/messages/createSms.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ int main() {
88

99
Appwrite appwrite(projectId, apiKey);
1010

11-
std::string messageId = "6b309k4016e14b8";
12-
std::string subject = "Hello from C++ Appwrite SDK!";
13-
std::string content =
14-
"Testing SMS message creation with topics, users, and targets.";
11+
std::string messageId = "msg001";
12+
std::string content = "Testing SMS message creation.";
1513

1614
std::vector<std::string> topics = {};
1715
std::vector<std::string> users = {};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "Appwrite.hpp"
2+
#include <chrono>
3+
#include <iostream>
4+
5+
int main() {
6+
std::string projectId = "";
7+
std::string apiKey = "";
8+
9+
Appwrite appwrite(projectId, apiKey);
10+
11+
std::string messageId = "msg001";
12+
std::string content = "Testing SMS message updation.";
13+
14+
std::vector<std::string> topics = {};
15+
std::vector<std::string> users = {};
16+
std::vector<std::string> targets = {};
17+
18+
auto now = std::chrono::system_clock::now();
19+
auto future_time = now + std::chrono::minutes(5);
20+
auto time_t = std::chrono::system_clock::to_time_t(future_time);
21+
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
22+
future_time.time_since_epoch()) %
23+
1000;
24+
25+
std::string scheduled_at = "";
26+
27+
bool draft = true;
28+
29+
try {
30+
std::string response = appwrite.getMessaging().updateSms(
31+
messageId, topics, users, targets, content, draft, scheduled_at);
32+
std::cout << "SMS Message updated!\nResponse: " << response
33+
<< std::endl;
34+
} catch (const AppwriteException &ex) {
35+
std::cerr << "Exception: " << ex.what() << std::endl;
36+
}
37+
38+
return 0;
39+
}

include/classes/Messaging.hpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,24 @@ class Messaging {
206206
const std::vector<std::string> &attachments = {},
207207
bool draft = false, bool html = false,
208208
const std::string &scheduled_at = "");
209+
/**
210+
* @brief Update an existing sms message.
211+
*
212+
* @param messageId Unique ID for the message.
213+
* @param topics List of topic IDs (optional).
214+
* @param users List of User IDs (optional).
215+
* @param targets List of target IDs (optional).
216+
* @param content SMS Content.
217+
* @param draft If true, saves the message as a draft.
218+
* @param scheduled_at Scheduled delivery time for message.
219+
* @return JSON response.
220+
*/
221+
std::string updateSms(const std::string &messageId,
222+
const std::vector<std::string> &topics = {},
223+
const std::vector<std::string> &users = {},
224+
const std::vector<std::string> &targets = {},
225+
const std::string &content = "", bool draft = false,
226+
const std::string &scheduled_at = "");
209227

210228
/**
211229
* @brief Updates an existing push notification
@@ -323,15 +341,15 @@ class Messaging {
323341
std::string listTargets(const std::string &messageId,
324342
const std::vector<std::string> &queries = {});
325343

326-
/**
344+
/**
327345
* @brief List all logs for a given topic.
328346
* @param topicID ID of the message.
329347
* @param queries Optional query filters.
330348
* @return JSON response.
331-
*/
349+
*/
332350
std::string listTopicLogs(const std::string &topicId,
333351
const std::vector<std::string> &queries = {});
334-
352+
335353
private:
336354
std::string projectId; ///< Project ID
337355
std::string apiKey; ///< API Key

src/services/Messaging.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,77 @@ std::string Messaging::createSms(const std::string &messageId,
548548
}
549549
}
550550

551+
// Added method to update an existing SMS message
552+
std::string Messaging::updateSms(const std::string &messageId,
553+
const std::vector<std::string> &topics,
554+
const std::vector<std::string> &users,
555+
const std::vector<std::string> &targets,
556+
const std::string &content, bool draft,
557+
const std::string &scheduled_at) {
558+
if (messageId.empty()) {
559+
throw AppwriteException("Missing required parameter: 'messageId'");
560+
}
561+
562+
if (content.empty()) {
563+
throw AppwriteException("Missing required parameter: 'content'");
564+
}
565+
566+
std::string payload = "{";
567+
568+
payload += R"("topics":[)";
569+
for (size_t i = 0; i < topics.size(); ++i) {
570+
payload += "\"" + Utils::escapeJsonString(topics[i]) + "\"";
571+
if (i != topics.size() - 1)
572+
payload += ",";
573+
}
574+
payload += "]";
575+
576+
payload += R"(,"users":[)";
577+
for (size_t i = 0; i < users.size(); ++i) {
578+
payload += "\"" + Utils::escapeJsonString(users[i]) + "\"";
579+
if (i != users.size() - 1)
580+
payload += ",";
581+
}
582+
payload += "]";
583+
584+
payload += R"(,"targets":[)";
585+
for (size_t i = 0; i < targets.size(); ++i) {
586+
payload += "\"" + Utils::escapeJsonString(targets[i]) + "\"";
587+
if (i != targets.size() - 1)
588+
payload += ",";
589+
}
590+
payload += "]";
591+
592+
payload += R"(,"content":")" + Utils::escapeJsonString(content) + R"(")";
593+
594+
payload += std::string(R"(,"draft":)") + (draft ? "true" : "false");
595+
596+
if (!scheduled_at.empty()) {
597+
payload += R"(,"scheduledAt":")" +
598+
Utils::escapeJsonString(scheduled_at) + "\"";
599+
}
600+
601+
payload += "}";
602+
603+
std::string url = Config::API_BASE_URL + "/messaging/messages/sms/" +
604+
Utils::urlEncode(messageId);
605+
606+
std::vector<std::string> headers = Config::getHeaders(projectId);
607+
headers.push_back("X-Appwrite-Key: " + apiKey);
608+
headers.push_back("Content-Type: application/json");
609+
610+
std::string response;
611+
int statusCode = Utils::patchRequest(url, payload, headers, response);
612+
613+
if (statusCode == HttpStatus::OK) {
614+
return response;
615+
} else {
616+
throw AppwriteException("Error updating sms message. Status code: " +
617+
std::to_string(statusCode) +
618+
"\n\nResponse: " + response);
619+
}
620+
}
621+
551622
// Added method to create a new email message.
552623
std::string Messaging::createEmail(
553624
const std::string &messageId, const std::string &subject,

0 commit comments

Comments
 (0)