本文主要是介绍使用亚马逊Amazon中pinpoint下的SMS发送手机短信,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
使用亚马逊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 或 PROMOTIONALregisteredKeyword: xxxx #与原始短代码相关联的注册关键字。senderId: Test #发送者IDappId: xxxx #aws的pinpoint项目的 projectId / applicationIdoriginationNumber: xxxx #发送源的手机号码accessKeyId: XXXXXX #accessKeyIdsecretAccessKey: XXXXXX #secretAccessKeyregion: 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() {@Overridepublic 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 objectDirectMessageConfiguration direct = DirectMessageConfiguration.builder().smsMessage(smsMessage).build();MessageRequest msgReq = MessageRequest.builder().addresses(addressMap).messageConfiguration(direct).build();// create a SendMessagesRequest objectSendMessagesRequest 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 sendMessagemap.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
这篇关于使用亚马逊Amazon中pinpoint下的SMS发送手机短信的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!