使用亚马逊Amazon中pinpoint下的SMS发送手机短信

2024-02-27 05:58

本文主要是介绍使用亚马逊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发送手机短信的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/751403

相关文章

Pandas使用SQLite3实战

《Pandas使用SQLite3实战》本文主要介绍了Pandas使用SQLite3实战,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1 环境准备2 从 SQLite3VlfrWQzgt 读取数据到 DataFrame基础用法:读

JSON Web Token在登陆中的使用过程

《JSONWebToken在登陆中的使用过程》:本文主要介绍JSONWebToken在登陆中的使用过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录JWT 介绍微服务架构中的 JWT 使用结合微服务网关的 JWT 验证1. 用户登录,生成 JWT2. 自定义过滤

Java中StopWatch的使用示例详解

《Java中StopWatch的使用示例详解》stopWatch是org.springframework.util包下的一个工具类,使用它可直观的输出代码执行耗时,以及执行时间百分比,这篇文章主要介绍... 目录stopWatch 是org.springframework.util 包下的一个工具类,使用它

Java使用Curator进行ZooKeeper操作的详细教程

《Java使用Curator进行ZooKeeper操作的详细教程》ApacheCurator是一个基于ZooKeeper的Java客户端库,它极大地简化了使用ZooKeeper的开发工作,在分布式系统... 目录1、简述2、核心功能2.1 CuratorFramework2.2 Recipes3、示例实践3

springboot security使用jwt认证方式

《springbootsecurity使用jwt认证方式》:本文主要介绍springbootsecurity使用jwt认证方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录前言代码示例依赖定义mapper定义用户信息的实体beansecurity相关的类提供登录接口测试提供一

go中空接口的具体使用

《go中空接口的具体使用》空接口是一种特殊的接口类型,它不包含任何方法,本文主要介绍了go中空接口的具体使用,具有一定的参考价值,感兴趣的可以了解一下... 目录接口-空接口1. 什么是空接口?2. 如何使用空接口?第一,第二,第三,3. 空接口几个要注意的坑坑1:坑2:坑3:接口-空接口1. 什么是空接

springboot security快速使用示例详解

《springbootsecurity快速使用示例详解》:本文主要介绍springbootsecurity快速使用示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录创www.chinasem.cn建spring boot项目生成脚手架配置依赖接口示例代码项目结构启用s

Python如何使用__slots__实现节省内存和性能优化

《Python如何使用__slots__实现节省内存和性能优化》你有想过,一个小小的__slots__能让你的Python类内存消耗直接减半吗,没错,今天咱们要聊的就是这个让人眼前一亮的技巧,感兴趣的... 目录背景:内存吃得满满的类__slots__:你的内存管理小助手举个大概的例子:看看效果如何?1.

java中使用POI生成Excel并导出过程

《java中使用POI生成Excel并导出过程》:本文主要介绍java中使用POI生成Excel并导出过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求说明及实现方式需求完成通用代码版本1版本2结果展示type参数为atype参数为b总结注:本文章中代码均为

Spring Boot3虚拟线程的使用步骤详解

《SpringBoot3虚拟线程的使用步骤详解》虚拟线程是Java19中引入的一个新特性,旨在通过简化线程管理来提升应用程序的并发性能,:本文主要介绍SpringBoot3虚拟线程的使用步骤,... 目录问题根源分析解决方案验证验证实验实验1:未启用keep-alive实验2:启用keep-alive扩展建