使用亚马逊Amazon中pinpoint下的SMS发送手机短信
使用的是java2版本的sdk
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>pinpoint</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!--amazon s3-->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.17.100</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
nacos配置:
aws:
pinpoint:
messageType: TRANSACTIONAL #发送的短信类型 指定 TRANSACTIONAL 或 PROMOTIONAL
registeredKeyword: xxxx #与原始短代码相关联的注册关键字。
senderId: Test #发送者ID
appId: xxxx #aws的pinpoint项目的 projectId / applicationId
originationNumber: xxxx #发送源的手机号码
accessKeyId: XXXXXX #accessKeyId
secretAccessKey: XXXXXX #secretAccessKey
region: xxx #区域 一定要看清楚那个区
template: Your verification code is:%s #短信模板
发短信工具类:
package com.oneplus.common.util.aws;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProviderChain;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.pinpoint.PinpointClient;
import software.amazon.awssdk.services.pinpoint.model.*;
import java.util.HashMap;
import java.util.Map;
/**
* @author Survivor
* @create 2021/12/20 18:03
*/
@Slf4j
@Component
public class AwsSNSUtils {
/**
* 发送的短信类型。
* 如果你打算送对时间敏感的内容,指定TRANSACTIONAL。
* 如果你打算送营销相关内容,指定PROMOTIONAL。
*/
@Value("${aws.pinpoint.messageType}")
public String messageType;
/**
* 与原始短代码相关联的注册关键字。
*/
@Value("${aws.pinpoint.registeredKeyword}")
public String registeredKeyword;
/**
* 发送消息时使用的发送者ID。 支持发送者ID
* 因国家或地区而异。 有关详细信息,请参见
* https://docs.aws.amazon.com/pinpoint/latest/userguide/channels-sms-countries.html
*/
@Value("${aws.pinpoint.senderId}")
public String senderId;
/**
* aws的pinpoint项目的 projectId / applicationId
*/
@Value("${aws.pinpoint.appId}")
public String appId;
/**
* 发送手机号
*/
@Value("${aws.pinpoint.originationNumber}")
public String originationNumber;
/**
* accessKeyId
*/
@Value("${aws.pinpoint.accessKeyId}")
public String accessKeyId;
/**
* secretAccessKey
*/
@Value("${aws.pinpoint.secretAccessKey}")
public String secretAccessKey;
/**
* 区域
*/
@Value("${aws.pinpoint.region}")
public String region;
/**
* 短信模板: Your verification code is:%s
*/
@Value("${aws.pinpoint.template}")
public String template;
public boolean sendMessage(String destinationNumber,Integer code){
try {
String message = String.format(template,code);
sendMessage(destinationNumber,message);
return true;
} catch (PinpointException e) {
log.info("发短信报错====>:" + e.awsErrorDetails().errorMessage());
return false;
} catch (Exception e) {
log.info("Exception====>:" + e.getMessage());
e.printStackTrace();
return false;
}
}
private void sendMessage(String destinationNumber, String message) {
PinpointClient pinpoint = PinpointClient.builder()
.credentialsProvider(AwsCredentialsProviderChain.builder()
.addCredentialsProvider(new AwsCredentialsProvider() {
@Override
public AwsCredentials resolveCredentials() {
return AwsBasicCredentials.create(accessKeyId, secretAccessKey);
}
}).build())
.region(Region.of(region))
.build();
sendSmsMessage(pinpoint, message, appId, originationNumber, destinationNumber);
pinpoint.close();
}
private void sendSmsMessage(PinpointClient pinpoint, String message, String appId, String originationNumber, String destinationNumber) {
log.info("进入,发送短信接口");
Map<String, AddressConfiguration> addressMap = new HashMap<String, AddressConfiguration>();
AddressConfiguration addConfig = AddressConfiguration.builder()
.channelType(ChannelType.SMS)
.build();
addressMap.put(destinationNumber, addConfig);
SMSMessage smsMessage = SMSMessage.builder()
.body(message)
.messageType(messageType)
.originationNumber(originationNumber)
.senderId(senderId)
.keyword(registeredKeyword)
.build();
// Create a DirectMessageConfiguration object
DirectMessageConfiguration direct = DirectMessageConfiguration.builder()
.smsMessage(smsMessage)
.build();
MessageRequest msgReq = MessageRequest.builder()
.addresses(addressMap)
.messageConfiguration(direct)
.build();
// create a SendMessagesRequest object
SendMessagesRequest request = SendMessagesRequest.builder()
.applicationId(appId)
.messageRequest(msgReq)
.build();
SendMessagesResponse response = pinpoint.sendMessages(request);
MessageResponse msg = response.messageResponse();
Map map = msg.result();
//Write out the result of sendMessage
map.forEach((k, v) -> log.info((k + ":" + v)));
log.info("发送短信成功!");
}
}
详细文档路径:
https://docs.aws.amazon.com/zh_cn/zh_cn/sdk-for-java/latest/developer-guide/examples-pinpoint.html
https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javav2/example_code/pinpoint/src/main/java/com/example/pinpoint/SendMessage.java