某天,接收到阿里云一个通知,短信模板申请报备接口有调整
好了又该换接口了
1.短信服务SDK:Maven依赖搞成最新的要不用不了短信服务_SDK中心-阿里云OpenAPI开发者门户 (aliyun.com)
![](https://i-blog.csdnimg.cn/direct/7be49bc300de4b429b8e634775ca373b.png)
2.阿里云短信模板申请api文档:新街口有3个,本篇只介绍一个
https://help.aliyun.com/zh/sms/developer-reference/api-dysmsapi-2017-05-25-addsmstemplate?spm=a2c4g.11186623.0.0.2308339fhSNYRg
旧代码(阿里云上直接拿过来):
StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
.accessKeyId(accessKeyId)
.accessKeySecret(accessKeySecret)
.build());
// 设置client
AsyncClient client = AsyncClient.builder()
.region("cn-beijing")
.credentialsProvider(provider)
.overrideConfiguration(
ClientOverrideConfiguration.create()
.setEndpointOverride("阿里云模板申请请求域名")
)
.build();
AddSmsTemplateRequest addSmsTemplateRequest = AddSmsTemplateRequest.builder()
.templateType()
.templateName()
.templateContent()
.remark()
.build();
CompletableFuture<AddSmsTemplateResponse> response = client.addSmsTemplate(addSmsTemplateRequest);
client.close();
}
3.CreateSmsTemplate新接口变更:
4.新接口需要加传 1.签名 2. 如果有变量,则要传变量的规则
例如短信中有变量 ${code},此中${code}就是一个变量,需要指定变量的规则
获取签名列表接口:RelatedSignName获取名字(直接抄阿里云代码就行)
/**
* 查询全部短信模板签名
*/
@Override
public List<String> getAllTemplateSign() {
StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
.accessKeyId(accessKeyId)
.accessKeySecret(accessKeySecret)
.build());
AsyncClient client = AsyncClient.builder()
.region("cn-beijing")// 地区
.credentialsProvider(provider)
.overrideConfiguration(
ClientOverrideConfiguration.create()
.setEndpointOverride("阿里云调用域名")
)
.build();
QuerySmsSignListRequest querySmsSignListRequest = QuerySmsSignListRequest.builder()
.build();
CompletableFuture<QuerySmsSignListResponse> response = client.querySmsSignList(querySmsSignListRequest);
QuerySmsSignListResponse resp = null;
try {
resp = response.get();
} catch (Exception e) {
// 异常不展示了
} finally {
client.close();
}
if (null == resp) {
return Collections.emptyList();
}
QuerySmsSignListResponseBody body = resp.getBody();
List<QuerySmsSignListResponseBody.SmsSignList> smsSignList = body.getSmsSignList();
if (CollUtil.isEmpty(smsSignList)) {
return Collections.emptyList();
}
List<String> collect = smsSignList.stream().map(QuerySmsSignListResponseBody.SmsSignList::getSignName).collect(Collectors.toList());
return collect;
}
TemplateRule参数:模板变量规则,我点进了实例文档,通知短信。
我需要”其他类通知“的变量参数,小伙伴们需要什么都可以选:
也就是说,需要你搞一个这种格式的字符串丢到参数里
Map<String, String> snoJson1 = new HashMap<>();
snoJson1.put("code", "others");
String str=JSONUtil.toJsonStr(snoJson1);// Hutool包工具类
createSmsTemplateRequest = CreateSmsTemplateRequest.builder()
// 通知类短信
.templateType(1)
.templateName(模板名)
.templateContent(模板内容:我用的包含短链的 域名/${code} )
.relatedSignName(签名,就是你发送短信的标题公司比如:【支付宝】您已到账,这个【支付宝】就是)
.remark(备注信息)
.templateRule(str)
// Request-level configuration rewrite, can set Http request parameters, etc.
// .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders()))
.build();
...........