Last active
January 3, 2025 18:35
-
-
Save taf2/7124ba6dbff9738d46e299fd95c09bd6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CTMApexTextMessage { | |
// Your api keys from account settings | |
private static String apiKey() { return 'your-api-key'; } | |
private static String apiSec() { return 'your-api-sec'; } | |
private static String apiHost() { return 'api.calltrackingmetrics.com'; } | |
private static String apiAccount() { return 'your-account-id'; } | |
private static String apiAuthorization() { | |
return 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf(apiKey() + ':' + apiSec())); | |
} | |
@InvocableMethod(label='Send Text Message') | |
public static CTMTextMessageOutput[] sendTextMessage(List<CTMTextMessageInput> inputs) { | |
List<CTMTextMessageOutput> outputs = new List<CTMTextMessageOutput>(); | |
for (CTMTextMessageInput input : inputs) { | |
CTMTextMessageOutput output = new CTMTextMessageOutput(); | |
try { | |
String recordId = input.recordId; | |
String phoneNumber = input.phoneNumber; | |
String message = input.message; | |
output.recordId = recordId; | |
// Enqueue the future method to perform the callout | |
sendMessage(recordId, phoneNumber, message); | |
output.success = true; // Assume success since callout is asynchronous | |
output.message = 'Text message sent asynchronously'; | |
outputs.add(output); | |
} catch (Exception e) { | |
output.message = e.getMessage(); | |
output.success = false; | |
outputs.add(output); | |
} | |
} | |
// You can handle the outputs list as required | |
System.debug(outputs); | |
return outputs; | |
} | |
@future(callout=true) | |
public static void sendMessage(String recordId, String phone, String message) { | |
Http http = new Http(); | |
HttpRequest req = prepareRequest(recordId, phone, message); | |
http.send(req); | |
} | |
public static String endpoint() { | |
return 'https://' + apiHost() + '/api/v1/accounts/' + apiAccount() + '/sms'; | |
} | |
public static HttpRequest prepareRequest(String Id, String phone, String message) { | |
Map<String, String> payload = new Map<String, String>(); | |
payload.put('salesforce_id', Id); | |
payload.put('to', phone); | |
payload.put('from', '+19542895725'); | |
payload.put('msg', message); | |
HttpRequest req = new HttpRequest(); | |
String url = endpoint(); | |
req.setEndpoint(url); | |
req.setHeader('Content-Type','application/json'); | |
req.setHeader('Authorization',apiAuthorization()); | |
req.setMethod('POST'); | |
req.setBody(JSON.serialize(payload)); | |
return req; | |
} | |
public class CTMTextMessageInput { | |
@InvocableVariable(required=true) | |
public String recordId; | |
@InvocableVariable(required=true) | |
public String phoneNumber; | |
@InvocableVariable(required=true) | |
public String message; | |
} | |
public class CTMTextMessageOutput { | |
@InvocableVariable | |
public String recordId; | |
@InvocableVariable | |
public Boolean success; | |
@InvocableVariable | |
public String message; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@isTest | |
public class CTMApexTextMessageTest { | |
@isTest | |
static void testSendTextMessage() { | |
// Setup | |
CTMApexTextMessage.CTMTextMessageInput input = new CTMApexTextMessage.CTMTextMessageInput(); | |
input.recordId = '001xx000003DGb3AAG'; | |
input.phoneNumber = '+1234567890'; | |
input.message = 'Test message'; | |
List<CTMApexTextMessage.CTMTextMessageInput> inputs = new List<CTMApexTextMessage.CTMTextMessageInput>{ input }; | |
// Mock HTTP Callout | |
Test.setMock(HttpCalloutMock.class, new CTMApexTextMessageMock()); | |
// Test | |
Test.startTest(); | |
List<CTMApexTextMessage.CTMTextMessageOutput> outputs = CTMApexTextMessage.sendTextMessage(inputs); | |
Test.stopTest(); | |
// Assertions | |
System.assertEquals(1, outputs.size()); | |
CTMApexTextMessage.CTMTextMessageOutput output = outputs[0]; | |
System.assertEquals('001xx000003DGb3AAG', output.recordId); | |
System.assertEquals(true, output.success); | |
System.assertEquals('Text message sent asynchronously', output.message); | |
} | |
} | |
// Mock implementation of HttpCalloutMock | |
public class CTMApexTextMessageMock implements HttpCalloutMock { | |
public HTTPResponse respond(HttpRequest req) { | |
HttpResponse res = new HttpResponse(); | |
res.setHeader('Content-Type', 'application/json'); | |
res.setBody('{"success":true}'); | |
res.setStatusCode(200); | |
return res; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment