All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.taotao.cloud.sms.yunpian.YunPianSendHandler Maven / Gradle / Ivy

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.cloud.sms.yunpian;

import com.taotao.cloud.common.utils.log.LogUtils;
import com.taotao.cloud.sms.common.exception.SendFailedException;
import com.taotao.cloud.sms.common.handler.AbstractSendHandler;
import com.taotao.cloud.sms.common.model.NoticeData;
import com.taotao.cloud.sms.common.utils.StringUtils;
import com.yunpian.sdk.YunpianClient;
import com.yunpian.sdk.model.Result;
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;
import org.springframework.context.ApplicationEventPublisher;

/**
 * 云片网发送处理
 *
 * @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);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy