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

com.taotao.boot.sms.qiniu.QiNiuSendHandler 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.boot.sms.qiniu;

import com.qiniu.http.Response;
import com.qiniu.sms.SmsManager;
import com.qiniu.util.Auth;
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 org.springframework.context.ApplicationEventPublisher;

import java.util.Collection;

/**
 * 七牛云发送处理
 *
 * @author shuigedeng
 * @version 2022.04
 * @since 2022-04-27 17:52:09
 */
public class QiNiuSendHandler extends AbstractSendHandler {

    private final SmsManager smsManager;

    public QiNiuSendHandler(QiNiuProperties properties, ApplicationEventPublisher eventPublisher) {
        super(properties, eventPublisher);
        Auth auth = Auth.create(properties.getAccessKey(), properties.getSecretKey());
        smsManager = new SmsManager(auth);
    }

    @Override
    public String getChannelName() {
        return "qiNiu";
    }

    @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;
        }
        Response response = null;
        try {
            response = smsManager.sendMessage(templateId, phones.toArray(new String[] {}), noticeData.getParams());

            if (response.isOK()) {
                publishSendSuccessEvent(noticeData, phones, response);
                return true;
            }

            LogUtils.debug("send fail, error: {}", response.error);
            publishSendFailEvent(noticeData, phones, new SendFailedException(response.error), response);
            return false;
        } catch (Exception e) {
            LogUtils.debug(e.getLocalizedMessage(), e);
            publishSendFailEvent(noticeData, phones, e, response);
        }

        return false;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy