com.fivefaces.cloud.sms.AWSSNSSMSService Maven / Gradle / Ivy
package com.fivefaces.cloud.sms;
import com.amazonaws.services.sns.AmazonSNS;
import com.amazonaws.services.sns.model.MessageAttributeValue;
import com.amazonaws.services.sns.model.PublishRequest;
import com.amazonaws.services.sns.model.PublishResult;
import com.fivefaces.cloud.Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Profile("SMS_AWS_SNS")
@Slf4j
@Service
public class AWSSNSSMSService implements SMSService {
private final Utils utils;
private AmazonSNS snsClient;
public AWSSNSSMSService(Utils utils) {
this.utils = utils;
}
@Override
public void sendSms(String phoneNumber, String message) {
Map smsAttributes = new HashMap<>();
smsAttributes.put("AWS.SNS.SMS.SenderID", new MessageAttributeValue().withStringValue(utils.getSmsSenderId()).withDataType("String"));
smsAttributes.put("AWS.SNS.SMS.MaxPrice", new MessageAttributeValue().withStringValue(utils.getAwsMaxPrice()).withDataType("Number"));
smsAttributes.put("AWS.SNS.SMS.SMSType", new MessageAttributeValue().withStringValue("Transactional").withDataType("String"));
PublishResult publishResult = sendSMSMessage(getSNSClient(), message, phoneNumber, smsAttributes);
log.debug("SMS Publish result: " + publishResult.toString());
}
private PublishResult sendSMSMessage(AmazonSNS snsClient, String message, String phoneNumber, Map smsAttributes) {
return snsClient.publish(new PublishRequest()
.withMessage(message)
.withPhoneNumber(phoneNumber)
.withMessageAttributes(smsAttributes));
}
private AmazonSNS getSNSClient() {
if (snsClient == null) {
snsClient = utils.generateSNSClient();
}
return snsClient;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy