Skip to content

Commit 87302c8

Browse files
me-hemUmesh Bhatt
authored andcommitted
API added to update an existing sms and tested successfully
1 parent 707ff90 commit 87302c8

File tree

5 files changed

+141
-4
lines changed

5 files changed

+141
-4
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ int main() {
88

99
Appwrite appwrite(projectId, apiKey);
1010

11-
std::string messageId = "6b309k4016e14b8";
11+
std::string messageId = "6b309gbdfjhfjsk4016e14b8";
1212
std::string subject = "Hello from C++ Appwrite SDK!";
1313
std::string content =
1414
"Testing SMS message creation with topics, users, and targets.";
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 = "6b309gbdfjhfjsk4016e14b8";
12+
std::string subject = "Hello from C++ Appwrite SDK!";
13+
std::string content =
14+
"Testing SMS message updation with topics, users, and targets.";
15+
16+
std::vector<std::string> topics = {};
17+
std::vector<std::string> users = {};
18+
std::vector<std::string> targets = {};
19+
20+
auto now = std::chrono::system_clock::now();
21+
auto future_time = now + std::chrono::minutes(5);
22+
auto time_t = std::chrono::system_clock::to_time_t(future_time);
23+
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
24+
future_time.time_since_epoch()) %
25+
1000;
26+
27+
std::stringstream ss;
28+
ss << std::put_time(std::gmtime(&time_t), "%Y-%m-%dT%H:%M:%S");
29+
ss << "." << std::setfill('0') << std::setw(3) << ms.count() << "+00:00";
30+
std::string scheduled_at = ss.str();
31+
32+
bool draft = true;
33+
34+
try {
35+
std::string response = appwrite.getMessaging().updateSms(
36+
messageId, topics, users, targets, content, draft, scheduled_at);
37+
std::cout << "SMS Message updated!\nResponse: " << response
38+
<< std::endl;
39+
} catch (const AppwriteException &ex) {
40+
std::cerr << "Exception: " << ex.what() << std::endl;
41+
}
42+
43+
return 0;
44+
}

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)