Sample Code for Sending SMS Messages and Logging Responses in SFMC Using QueueMO API
Introduction
In this tutorial, we will walk through the process of sending SMS messages to mobile numbers stored in a data extension and logging the responses in another data extension using Salesforce Marketing Cloud (SFMC). We will be using the QueueMO API for sending SMS messages. This guide will cover creating the necessary data extensions, writing the script, and ensuring everything works seamlessly.
Step 1: Create Data Extensions
1.1 Data Extension for Mobile Numbers
First, we need a data extension to store the mobile numbers to which we will send SMS messages. This data extension should have the following fields:
- mobile_number (Text, Primary Key)
- subscriberkey (Text)
- Processed (Boolean)
Example:
Data Extension Name: SMS_Mobile_Numbers
Fields:
- mobile_number (Text, Primary Key)
- subscriberkey (Text)
- Processed (Boolean)
1.2 Data Extension for Logging Responses
Next, we need a data extension to log the responses from the SMS messages. This data extension should have the following fields:
- subscriberkey (Text)
- mobile_number (Text)
- response (Text)
- timestamp (Date)
Example:
Data Extension Name: SMS_Response_Log
Fields:
- subscriberkey (Text)
- mobile_number (Text)
- response (Text)
- timestamp (Date)
Step 2: Write the Script
Now, let's write the script to send SMS messages using the QueueMO API and log the responses. Below is the complete script:
<script runat="server">
Platform.Load("Core", "1");
// Function to refresh token
function refreshToken() {
var url = 'https://your-auth-url.auth.marketingcloudapis.com/v2/token';
var payload = {
"client_id": "your-client-id",
"client_secret": "your-client-secret",
"grant_type": "client_credentials"
};
var response = HTTP.Post(url, "application/json", Stringify(payload));
var tokenResponse = Platform.Function.ParseJSON(response.Response[0]);
Write("Token Response: " + Stringify(tokenResponse));
return "Bearer " + tokenResponse.access_token;
}
// Function to send SMS using QueueMO API
function sendSMS(mobileNumber, token) {
var url = "https://your-rest-url.rest.marketingcloudapis.com/sms/v1/queueMO/";
var payload = {
"mobileNumbers": [mobileNumber],
"shortCode": "your-shortcode",
"messageText": "your-message-text"
};
var headerNames = ["Authorization"];
var headerValues = [token];
var response = HTTP.Post(url, "application/json", Stringify(payload), headerNames, headerValues);
Write("SMS Response: " + Stringify(response));
Write("SMS Response Status Code: " + response.StatusCode);
Write("SMS Response Headers: " + Stringify(response.Headers));
Write("SMS Response Body: " + response.Response[0]);
return response;
}
// Refresh token once at the beginning
var token;
try {
token = refreshToken();
Write("Token refreshed successfully: " + token);
} catch (e) {
Write("Error refreshing token: " + Stringify(e));
}
// Retrieve mobile numbers from data extension where Processed is false
var contacts = DataExtension.Init("SMS_Mobile_Numbers").Rows.Lookup(["Processed"], [false]);
var contactCount = contacts.length;
Write("Number of contacts retrieved: " + contactCount);
// Log Data Extension
var logDE = DataExtension.Init("SMS_Response_Log");
for (var i = 0; i < contactCount; i++) {
var contact = contacts[i];
var mobile_number = contact.mobile_number;
var subscriberkey = contact.subscriberkey;
var response;
Write("Processing contact: " + mobile_number);
try {
response = sendSMS(mobile_number, token);
Write("SMS sent to " + mobile_number + ". Response: " + Stringify(response));
} catch (e) {
Write("Error sending SMS to " + mobile_number + ": " + Stringify(e));
response = {
"error": Stringify(e)
};
}
// Update the Processed field to True
try {
DataExtension.Init("SMS_Mobile_Numbers").Rows.Update({ "Processed": true }, ["mobile_number"], [mobile_number]);
Write("Updated Processed field for " + mobile_number);
} catch (e) {
Write("Error updating Processed field for " + mobile_number + ": " + Stringify(e));
}
// Log the attempt
try {
logDE.Rows.Add({
"subscriberkey": subscriberkey,
"mobile_number": mobile_number,
"response": Stringify(response),
"timestamp": Platform.Function.Now()
});
Write("Logged attempt for " + mobile_number);
} catch (e) {
Write("Error logging attempt for " + mobile_number + ": " + Stringify(e));
}
}
</script>
Conclusion
By following these steps, you can set up a system in Salesforce Marketing Cloud to send SMS messages using the QueueMO API to mobile numbers stored in a data extension and log the responses in another data extension. This script ensures that the SMS messages are sent efficiently and the responses are logged for future reference.
Feel free to customize the placeholders (your-auth-url
, your-client-id
, your-client-secret
, your-rest-url
, your-shortcode
, your-message-text
, SMS_Mobile_Numbers
, SMS_Response_Log
) to fit your specific requirements.
Comments
Post a Comment