Created
April 1, 2024 17:17
-
-
Save taf2/837731bd2d0ffda9657fd2a843ce3ef8 to your computer and use it in GitHub Desktop.
This file contains 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 CTMTextMessage { | |
// Your api keys from account settings | |
private static String apiKey() { return 'your-key'; } | |
private static String apiSec() { return 'your-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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment