feat: 添加阿里云短信通知器

This commit is contained in:
椰子 2021-07-02 15:01:18 +08:00
parent 0b6c8d56b3
commit a088d6440e
4 changed files with 291 additions and 0 deletions

View File

@ -0,0 +1,100 @@
package com.cicdi.notify.sms.aliyun;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.cicdi.notify.*;
import com.cicdi.notify.sms.SmsProvider;
import com.cicdi.notify.template.TemplateManager;
import java.util.Map;
import java.util.Objects;
public class AliyunSmsNotifier extends AbstractNotifier<AliyunSmsTemplate> {
private final IAcsClient client;
private final int connectTimeout = 1000;
private final int readTimeout = 5000;
private String notifierId;
public String getNotifierId() {
return notifierId;
}
private String domain = "dysmsapi.aliyuncs.com";
private String regionId = "cn-hangzhou";
public AliyunSmsNotifier(NotifierProperties profile, TemplateManager templateManager) {
super(templateManager);
Map<String, Object> config = profile.getConfiguration();
DefaultProfile defaultProfile = DefaultProfile.getProfile(
this.regionId = (String) Objects.requireNonNull(config.get("regionId"), "regionId不能为空"),
(String) Objects.requireNonNull(config.get("accessKeyId"), "accessKeyId不能为空"),
(String) Objects.requireNonNull(config.get("secret"), "secret不能为空")
);
this.client = new DefaultAcsClient(defaultProfile);
this.domain = (String) config.getOrDefault("domain", domain);
this.notifierId = profile.getId();
}
public AliyunSmsNotifier(IClientProfile profile, TemplateManager templateManager) {
this(new DefaultAcsClient(profile), templateManager);
}
public AliyunSmsNotifier(IAcsClient client, TemplateManager templateManager) {
super(templateManager);
this.client = client;
}
@Override
public NotifyType getType() {
return DefaultNotifyType.sms;
}
@Override
public Provider getProvider() {
return SmsProvider.aliyunSms;
}
@Override
public void send(AliyunSmsTemplate template, Map<String, Object> context) {
try {
CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST);
request.setSysDomain(domain);
request.setSysVersion("2017-05-25");
request.setSysAction("SendSms");
request.setSysConnectTimeout(connectTimeout);
request.setSysReadTimeout(readTimeout);
request.putQueryParameter("RegionId", regionId);
request.putQueryParameter("PhoneNumbers", template.getPhoneNumber());
request.putQueryParameter("SignName", template.getSignName());
request.putQueryParameter("TemplateCode", template.getCode());
request.putQueryParameter("TemplateParam", template.createTtsParam(context));
CommonResponse response = client.getCommonResponse(request);
JSONObject json = JSON.parseObject(response.getData());
if (!"ok".equalsIgnoreCase(json.getString("Code"))) {
// TODO 自定义异常
// return new BusinessException(json.getString("Message"), json.getString("Code"));
throw new Exception("json.getString(\"Message\"), json.getString(\"Code\")");
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void close() {
client.shutdown();
}
}

View File

@ -0,0 +1,65 @@
package com.cicdi.notify.sms.aliyun;
import com.alibaba.fastjson.JSON;
import com.cicdi.notify.*;
import com.cicdi.notify.sms.SmsProvider;
import com.cicdi.notify.template.TemplateManager;
import com.cicdi.notify.template.TemplateProperties;
import com.cicdi.notify.template.TemplateProvider;
/**
* 阿里云短信通知服务
* </a>
*/
public class AliyunSmsNotifierProvider implements NotifierProvider, TemplateProvider {
private final TemplateManager templateManager;
public AliyunSmsNotifierProvider(TemplateManager templateManager) {
this.templateManager = templateManager;
}
@Override
public Provider getProvider() {
return SmsProvider.aliyunSms;
}
// public static final DefaultConfigMetadata templateConfig = new DefaultConfigMetadata("阿里云短信模版",
// "https://help.aliyun.com/document_detail/108086.html")
// .add("signName", "签名", "", new StringType())
// .add("code", "模版编码", "", new StringType())
// .add("phoneNumber", "收信人", "", new StringType());
//
// public static final DefaultConfigMetadata notifierConfig = new DefaultConfigMetadata("阿里云API配置"
// ,"https://help.aliyun.com/document_detail/101300.html")
// .add("regionId", "regionId", "regionId", new StringType())
// .add("accessKeyId", "accessKeyId", "", new StringType())
// .add("secret", "secret", "", new StringType());
//
// @Override
// public ConfigMetadata getTemplateConfigMetadata() {
// return templateConfig;
// }
//
// @Override
// public ConfigMetadata getNotifierConfigMetadata() {
// return notifierConfig;
// }
@Override
public AliyunSmsTemplate createTemplate(TemplateProperties properties) {
// TODO 验证工具
//return ValidatorUtils.tryValidate(JSON.parseObject(properties.getTemplate(), AliyunSmsTemplate.class));
return JSON.parseObject(properties.getTemplate(), AliyunSmsTemplate.class);
}
@Override
public NotifyType getType() {
return DefaultNotifyType.sms;
}
@Override
public AliyunSmsNotifier createNotifier(NotifierProperties properties) {
return new AliyunSmsNotifier(properties, templateManager);
}
}

View File

@ -0,0 +1,60 @@
package com.cicdi.notify.sms.aliyun;
import com.alibaba.fastjson.JSON;
import com.cicdi.notify.template.Template;
import java.util.Map;
/**
* 阿里云短信模版
*
* @since 1.3
*/
public class AliyunSmsTemplate implements Template {
//签名名称
private String signName;
//模版编码
private String code;
private String phoneNumber;
private Map<String, String> param;
public String createTtsParam(Map<String, Object> ctx) {
return JSON.toJSONString(ctx);
}
public String getSignName() {
return signName;
}
public void setSignName(String signName) {
this.signName = signName;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public Map<String, String> getParam() {
return param;
}
public void setParam(Map<String, String> param) {
this.param = param;
}
}

View File

@ -0,0 +1,66 @@
package com.cicdi.notify.sms;
import com.cicdi.notify.*;
import com.cicdi.notify.sms.aliyun.AliyunSmsNotifierProvider;
import com.cicdi.notify.template.AbstractTemplateManager;
import com.cicdi.notify.template.Template;
import com.cicdi.notify.template.TemplateManager;
import com.cicdi.notify.template.TemplateProperties;
import org.junit.Test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* @author xueye
*/
public class AliyunSmsTest {
@Test
public void test() {
// 通知器配置管理器
NotifyConfigManager notifyConfigManager = new NotifyConfigManager() {
@Override
public NotifierProperties getNotifyConfig(NotifyType notifyType, String configId) {
NotifierProperties notifierProperties = new NotifierProperties();
notifierProperties.setType(DefaultNotifyType.sms.getId());
notifierProperties.setProvider(SmsProvider.aliyunSms.getId());
notifierProperties.setId("12");
Map<String, Object> config = new HashMap<>();
config.put("regionId", "");
config.put("accessKeyId", "");
config.put("secret", "");
notifierProperties.setConfiguration(config);
return notifierProperties;
}
};
// 模板管理器
TemplateManager templateManager = new AbstractTemplateManager() {
@Override
protected TemplateProperties getProperties(NotifyType type, String id) {
TemplateProperties templateProperties = new TemplateProperties();
templateProperties.setType(DefaultNotifyType.sms.getId());
templateProperties.setProvider(SmsProvider.aliyunSms.getId());
templateProperties.setTemplate("{\"signName\":\"Hello\",\"phoneNumber\":[\"xueye404@qq.com\"],\"code\":\"1322\"}");
return templateProperties;
}
};
NotifierManager notifierManager = new DefaultNotifierManager(notifyConfigManager);
AliyunSmsNotifierProvider aliyunSmsNotifierProvider = new AliyunSmsNotifierProvider(templateManager);
// register
notifierManager.registerProvider(aliyunSmsNotifierProvider);
templateManager.register(aliyunSmsNotifierProvider);
Notifier<Template> notifier = notifierManager.getNotifier(DefaultNotifyType.sms, "123");
notifier.send(templateManager.getTemplate(DefaultNotifyType.sms, "124"), new HashMap<>());
}
}