Skip to content

Commit 5a5980e

Browse files
me-hemUmesh Bhatt
andauthored
Added createSms method to create a new message (#137)
* Implemented createSms method and tested successfully * fix: formatted code using clang-format * changed hardcoded time to local time --------- Co-authored-by: Umesh Bhatt <hatgrey@localhost.localdomain>
1 parent b0179be commit 5a5980e

File tree

4 files changed

+300
-138
lines changed

4 files changed

+300
-138
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,10 @@ listProviderLogs: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/listProviderLogs.cp
284284
@mkdir -p ./$(TESTS_DIR)
285285
$(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/listProviderLogs $(SRCS) $(EXAMPLES_DIR)/messaging/messages/listProviderLogs.cpp $(LDFLAGS)
286286

287+
createSms: $(SRCS) $(EXAMPLES_DIR)/messaging/messages/createSms.cpp
288+
@mkdir -p ./$(TESTS_DIR)
289+
$(CXX) $(CXXFLAGS) -o ./$(TESTS_DIR)/createSms $(SRCS) $(EXAMPLES_DIR)/messaging/messages/createSms.cpp $(LDFLAGS)
290+
287291
# Messaging - Topics
288292
getTopic: $(SRCS) $(EXAMPLES_DIR)/messaging/topics/getTopic.cpp
289293
@mkdir -p ./$(TESTS_DIR)
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 = "6b309k4016e14b8";
12+
std::string subject = "Hello from C++ Appwrite SDK!";
13+
std::string content =
14+
"Testing SMS message creation 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().createSms(
36+
messageId, content, topics, users, targets, draft, scheduled_at);
37+
std::cout << "SMS Message Created!\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: 95 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -116,30 +116,31 @@ class Messaging {
116116
const std::string &name,
117117
const std::string &targetId,
118118
const std::string &subscriberId);
119-
/**
119+
/**
120120
* @brief Creates a new push notification message.
121-
*
122-
* Sends a push notification to specified users, topics, or both.
121+
*
122+
* Sends a push notification to specified users, topics, or both.
123123
*
124124
* @param messageId A unique Id for the message.
125125
* @param title Title of the push notification.
126126
* @param body Body content of the push notification.
127-
* @param topicId A list of topic IDs to which the notification should be sent.
128-
* @param userId A list of user IDs to which the notification should be sent.
127+
* @param topicId A list of topic IDs to which the notification should be
128+
* sent.
129+
* @param userId A list of user IDs to which the notification should be
130+
* sent.
129131
* @param draft If true, saves the message as a draft.
130-
*
132+
*
131133
* @return JSON response.
132-
*/
134+
*/
133135
std::string createPush(const std::string &messageId,
134-
const std::string &title,
135-
const std::string &body,
136-
const std::vector<std::string> &topicId= {},
137-
const std::vector<std::string> &userId = {},
138-
bool draft = false);
139-
140-
/**
136+
const std::string &title, const std::string &body,
137+
const std::vector<std::string> &topicId = {},
138+
const std::vector<std::string> &userId = {},
139+
bool draft = false);
140+
141+
/**
141142
* @brief Create a new email message.
142-
*
143+
*
143144
* Sends a new email message to specific topics and/or target recipients.
144145
* At least one of `topics` or `targets` must be provided.
145146
*
@@ -150,30 +151,56 @@ class Messaging {
150151
* @param targets List of target recipients (e.g., email:userId) (optional).
151152
* @return JSON response.
152153
*/
153-
std::string createMessage(const std::string& messageId,
154-
const std::string& subject,
155-
const std::string& content,
156-
const std::vector<std::string>& topics = {},
157-
const std::vector<std::string>& targets = {});
154+
std::string createMessage(const std::string &messageId,
155+
const std::string &subject,
156+
const std::string &content,
157+
const std::vector<std::string> &topics = {},
158+
const std::vector<std::string> &targets = {});
159+
160+
/**
161+
* @brief Create a new sms message.
162+
*
163+
* @param messageId Unique ID for the message.
164+
* @param content SMS Content.
165+
* @param topics List of topic IDs (optional).
166+
* @param users List of User IDs (optional).
167+
* @param targets List of target IDs (optional).
168+
* @param draft If true, saves the message as a draft.
169+
* @param scheduled_at Scheduled delivery time for message.
170+
* @return JSON response.
171+
*/
172+
std::string createSms(const std::string &messageId,
173+
const std::string &content,
174+
const std::vector<std::string> &topics = {},
175+
const std::vector<std::string> &users = {},
176+
const std::vector<std::string> &targets = {},
177+
bool draft = false,
178+
const std::string &scheduled_at = "");
158179

159-
/**
160-
* @brief Updates an existing push notification message.
161-
*
162-
* Modifies the title and body of an existing push message.
180+
/**
181+
* @brief Updates an existing push notification
182+
* message.
163183
*
164-
* @param messageId The ID of the message to update.
165-
* @param title New title of the push notification.
166-
* @param body New body content of the push notification.
167-
* @param topicId List of topic IDs to update the message.
168-
* @param userId List of user IDs to update the message.
184+
* Modifies the title and body of an existing push
185+
* message.
186+
*
187+
* @param messageId The ID of the message to
188+
* update.
189+
* @param title New title of the push
190+
* notification.
191+
* @param body New body content of the push
192+
* notification.
193+
* @param topicId List of topic IDs to update the
194+
* message.
195+
* @param userId List of user IDs to update the
196+
* message.
169197
* @return JSON response
170-
*/
198+
*/
171199
std::string updatePush(const std::string &messageId,
172-
const std::string &title,
173-
const std::string &body,
174-
const std::vector<std::string> &topicId = {},
175-
const std::vector<std::string> &userId = {});
176-
200+
const std::string &title, const std::string &body,
201+
const std::vector<std::string> &topicId = {},
202+
const std::vector<std::string> &userId = {});
203+
177204
/**
178205
* @brief List all providers.
179206
* @param queries Optional query filters
@@ -189,77 +216,81 @@ class Messaging {
189216
*/
190217
std::string listProviderLogs(const std::string &providerId,
191218
Queries &queries);
192-
219+
193220
/**
194-
* @brief Create a new Firebase Cloud Messaging provider.
221+
* @brief Create a new Firebase Cloud Messaging
222+
* provider.
195223
* @param providerId A unique Id for the provider.
196224
* @param name provider name.
197-
* @param service_account_json FCM service account JSON..
198-
* @param enabled Whether the provider should be active immediately after creation.
225+
* @param service_account_json FCM service account
226+
* JSON..
227+
* @param enabled Whether the provider should be
228+
* active immediately after creation.
199229
* @return JSON response.
200-
*/
230+
*/
201231
std::string createFcmProvider(std::string &providerId, std::string name,
202232
std::string service_account_json,
203233
bool enabled);
204-
234+
205235
/**
206236
* @brief Delete a provider.
207237
* @param providerId ID of the provider
208238
* @return JSON response
209239
*/
210240
std::string deleteProvider(const std::string &providerId);
211-
241+
212242
/**
213243
* @brief Get a specific provider by ID.
214244
* @param providerId ID of the provider
215245
* @return JSON string of the provider details
216246
*/
217247
std::string getProvider(const std::string &providerId);
218-
248+
219249
/**
220-
* @brief List all message logs with optional filters.
250+
* @brief List all message logs with optional
251+
* filters.
221252
* @param messageId ID of the message
222253
* @param queries Query parameters for filtering
223254
* @return JSON string of messageLog list
224255
*/
225256
std::string listMessageLogs(const std::string &messageId, Queries &queries);
226-
227-
/**
257+
258+
/**
228259
* @brief Delete a message by its ID.
229260
* @param messageId ID of the message.
230261
* @return JSON response.
231262
*/
232263

233264
std::string deleteMessages(const std::string &messageId);
234-
235-
/**
265+
266+
/**
236267
* @brief Update an email message by its ID.
237268
* @class updateEmail
238-
*
239-
* This method belongs to the updateEmail class and provides the functionality
240-
* to update the subject and content of an existing email message via the
241-
* Appwrite Messaging API.
242-
*
269+
*
270+
* This method belongs to the updateEmail class
271+
* and provides the functionality to update the
272+
* subject and content of an existing email
273+
* message via the Appwrite Messaging API.
274+
*
243275
* @param messageId Unique message identifier
244276
* @param subject New subject of the email
245-
* @param content Updated content/body of the email
277+
* @param content Updated content/body of the
278+
* email
246279
* @return JSON response string from the server
247-
* @throws AppwriteException if parameters are invalid or request fails
248-
*/
249-
std::string updateEmail(
250-
const std::string& messageId,
251-
const std::string& subject,
252-
const std::string& content
253-
);
254-
280+
* @throws AppwriteException if parameters are
281+
* invalid or request fails
282+
*/
283+
std::string updateEmail(const std::string &messageId,
284+
const std::string &subject,
285+
const std::string &content);
255286

256287
/**
257288
* @brief List all targets for a given message.
258289
* @param messageId ID of the message.
259290
* @param queries Optional query filters.
260291
* @return JSON response.
261-
*/
262-
std::string listTargets(const std::string &messageId,
292+
*/
293+
std::string listTargets(const std::string &messageId,
263294
const std::vector<std::string> &queries = {});
264295

265296
/**

0 commit comments

Comments
 (0)