com.taotao.boot.sms.yunpian.YunPianSendHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of taotao-boot-starter-sms-yunpian Show documentation
Show all versions of taotao-boot-starter-sms-yunpian Show documentation
taotao-boot-starter-sms-yunpian
The newest version!
/*
* Copyright (c) 2020-2030, Shuigedeng ([email protected] & https://blog.taotaocloud.top/).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.taotao.boot.sms.yunpian;
import com.taotao.boot.common.utils.log.LogUtils;
import com.taotao.boot.sms.common.exception.SendFailedException;
import com.taotao.boot.sms.common.handler.AbstractSendHandler;
import com.taotao.boot.sms.common.model.NoticeData;
import com.taotao.boot.sms.common.utils.StringUtils;
import com.yunpian.sdk.YunpianClient;
import com.yunpian.sdk.model.Result;
import org.springframework.context.ApplicationEventPublisher;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* 云片网发送处理
*
* @author shuigedeng
* @version 2022.04
* @since 2022-04-27 17:52:33
*/
public class YunPianSendHandler extends AbstractSendHandler {
private final YunpianClient client;
public YunPianSendHandler(YunPianProperties properties, ApplicationEventPublisher eventPublisher) {
super(properties, eventPublisher);
client = new YunpianClient(properties.getApikey()).init();
}
@Override
public String getChannelName() {
return "yunPian";
}
@Override
public boolean send(NoticeData noticeData, Collection phones) {
String type = noticeData.getType();
String templateId = properties.getTemplates(type);
if (templateId == null) {
LogUtils.debug("templateId invalid");
publishSendFailEvent(noticeData, phones, new SendFailedException("templateId invalid"), null);
return false;
}
Map params = noticeData.getParams();
StringBuilder paramsStringBuilder = new StringBuilder();
if (params != null && !params.isEmpty()) {
boolean firstParam = true;
for (Map.Entry entry : params.entrySet()) {
if (!firstParam) {
paramsStringBuilder.append("&");
}
paramsStringBuilder.append(getEncodeValue("#" + entry.getKey() + "#"));
paramsStringBuilder.append("=");
paramsStringBuilder.append(getEncodeValue(entry.getValue()));
firstParam = false;
}
}
String mobileString = StringUtils.join(phones, ",");
Map data = new HashMap<>(8);
data.put("apikey", properties.getApikey());
data.put("mobile", mobileString);
data.put("tpl_id", templateId);
data.put("tpl_value", paramsStringBuilder.toString());
Result> result;
if (phones.size() > 1) {
//noinspection deprecation
result = client.sms().batch_send(data);
} else {
//noinspection deprecation
result = client.sms().single_send(data);
}
boolean succeed = Objects.equals(result.getCode(), 0);
if (succeed) {
publishSendSuccessEvent(noticeData, phones, result);
} else {
LogUtils.debug("send fail: {}", result.getMsg());
publishSendFailEvent(noticeData, phones, new SendFailedException(result.getMsg()), result);
}
return succeed;
}
private String getEncodeValue(String value) {
try {
return URLEncoder.encode(value, StandardCharsets.UTF_8);
} catch (Exception e) {
throw new RuntimeException(e.getLocalizedMessage(), e);
}
}
}