Search in sources :

Example 1 with SendSmsResponse

use of com.aliyun.dysmsapi20170525.models.SendSmsResponse in project tansci by typ1805.

the class AliSmsServiceImpl method send.

/**
 * @MonthName: send
 * @Description: 发短信
 * @Author: tanyp
 * @Date: 2021/6/7 14:50
 * @Param: [dto]
 * @return: MessageVo
 */
@Override
public MessageVo send(MessageDto dto) {
    try {
        log.info("======发送短信开始,请求参数:{}", JSON.toJSON(dto));
        Client client = createClient();
        // 组装请求对象
        SendSmsRequest request = new SendSmsRequest();
        // 外部流水扩展字段
        String outId = UUID.randomUUID().toString();
        request.setOutId(outId);
        // 支持对多个手机号码发送短信,手机号码之间以英文逗号(,)分隔。上限为1000个手机号码。批量调用相对于单条调用及时性稍有延迟。
        request.setPhoneNumbers(dto.getPhone());
        // 短信签名名称
        request.setSignName(smsConfig.getSignName());
        // 短信模板ID
        request.setTemplateCode(dto.getCode());
        // 短信模板变量对应的实际值,JSON格式。如果JSON中需要带换行符,请参照标准的JSON协议处理。
        request.setTemplateParam(JSON.toJSONString(dto.getParam()));
        // 发送短信
        SendSmsResponse res = client.sendSms(request);
        TemplateDetails details = new TemplateDetails();
        MessageVo message = MessageVo.builder().build();
        if (Objects.equals(Constants.NEWS_SUCCESS_CODE, res.body.getCode())) {
            log.info("======发送短信成功,返回值:{}", JSON.toJSON(res.body));
            message.setCode(Constants.NEWS_SUCCESS_CODE);
            message.setMessage(Constants.NEWS_SUCCESS_MESSAGE);
            details.setState(0);
        } else {
            log.info("======发送短信失败,返回值:{}", JSON.toJSON(res.body));
            message.setCode(Constants.NEWS_FAIL_CODE);
            message.setMessage(Constants.NEWS_FAIL_MESSAGE);
            details.setState(1);
        }
        details.setCode(dto.getCode());
        details.setContent(JSON.toJSONString(request));
        details.setSendTime(LocalDateTime.now());
        templateDetailsService.save(details);
        return message;
    } catch (Exception e) {
        log.error("======发送短信异常:{}", e.getMessage());
        e.printStackTrace();
        return MessageVo.builder().code(Constants.NEWS_FAIL_CODE).message(Constants.NEWS_FAIL_MESSAGE).build();
    }
}
Also used : TemplateDetails(com.tansci.domain.message.TemplateDetails) Client(com.aliyun.dysmsapi20170525.Client) MessageVo(com.tansci.domain.message.vo.MessageVo)

Example 2 with SendSmsResponse

use of com.aliyun.dysmsapi20170525.models.SendSmsResponse in project ballcat by ballcat-projects.

the class TencentSenderImpl method send.

@Override
public SmsSenderResult send(SmsSenderParams sp) {
    try {
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint(tencent.getEndpoint());
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        SmsClient client = new SmsClient(cred, tencent.getRegion(), clientProfile);
        Map<String, Object> json = new HashMap<>(5);
        json.put("PhoneNumberSet", sp.getPhoneNumbers());
        json.put("SmsSdkAppid", tencent.getSdkId());
        if (tencent.getTemplateId() != null) {
            json.put("TemplateID", tencent.getTemplateId());
        }
        if (StrUtil.isNotEmpty(tencent.getSign())) {
            json.put("Sign", tencent.getSign());
        }
        if (!sp.getTemplateParam().isEmpty()) {
            json.put("TemplateParamSet", sp.getTemplateParam());
        }
        SendSmsRequest req = SendSmsRequest.fromJsonString(om.writeValueAsString(json), SendSmsRequest.class);
        SendSmsResponse resp = client.SendSms(req);
        return SmsSenderResult.generate(SendSmsRequest.toJsonString(resp), sp.toString(), sp.getPhoneNumbers());
    } catch (TencentCloudSDKException | JsonProcessingException e) {
        return errRet(TypeEnum.TENCENT, sp.getPhoneNumbers(), "腾讯云平台发送短信出现异常!", e);
    }
}
Also used : SmsClient(com.tencentcloudapi.sms.v20190711.SmsClient) TencentCloudSDKException(com.tencentcloudapi.common.exception.TencentCloudSDKException) ClientProfile(com.tencentcloudapi.common.profile.ClientProfile) HashMap(java.util.HashMap) HttpProfile(com.tencentcloudapi.common.profile.HttpProfile) SendSmsRequest(com.tencentcloudapi.sms.v20190711.models.SendSmsRequest) SendSmsResponse(com.tencentcloudapi.sms.v20190711.models.SendSmsResponse) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with SendSmsResponse

use of com.aliyun.dysmsapi20170525.models.SendSmsResponse in project cloud-sdk by mizhousoft.

the class AliyunSendSmsClient method send.

/**
 * {@inheritDoc}
 */
@Override
public void send(String phoneNumber, Map<String, String> paramMap, CloudSmsTemplate smsTemplate) throws CloudSDKException {
    if (null == smsTemplate) {
        throw new CloudSDKException("Sms template is null.");
    }
    String signName = smsTemplate.getSignName();
    String templateId = smsTemplate.getTemplateId().toString();
    SendSmsRequest request = new SendSmsRequest();
    request.setPhoneNumbers(phoneNumber);
    request.setSignName(signName);
    request.setTemplateCode(templateId);
    try {
        if (null != paramMap) {
            String templateParam = JSONUtils.toJSONString(paramMap);
            request.setTemplateParam(templateParam);
        }
        SendSmsResponse result = this.client.sendSms(request);
        SendSmsResponseBody body = result.getBody();
        if (null == body || !"ok".equalsIgnoreCase(body.getCode())) {
            throw new CloudSDKException("Send sms failed, code is " + body.code + ", message is " + body.getMessage() + ", request id is " + body.getRequestId() + ", bizId is " + body.getBizId() + '.');
        }
    } catch (Exception e) {
        throw new CloudSDKException("Send sms failed.", e);
    }
}
Also used : CloudSDKException(com.mizhousoft.cloudsdk.CloudSDKException) SendSmsRequest(com.aliyun.dysmsapi20170525.models.SendSmsRequest) SendSmsResponseBody(com.aliyun.dysmsapi20170525.models.SendSmsResponseBody) SendSmsResponse(com.aliyun.dysmsapi20170525.models.SendSmsResponse) CloudSDKException(com.mizhousoft.cloudsdk.CloudSDKException)

Example 4 with SendSmsResponse

use of com.aliyun.dysmsapi20170525.models.SendSmsResponse in project MaxKey by dromara.

the class SmsOtpAuthnTencentCloud method produce.

@Override
public boolean produce(UserInfo userInfo) {
    // 手机号
    String mobile = userInfo.getMobile();
    if (mobile != null && !mobile.equals("")) {
        try {
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            SmsClient client = new SmsClient(cred, "ap-beijing", clientProfile);
            String token = this.genToken(userInfo);
            String params = "{\"PhoneNumberSet\":[\"" + mobile + "\"]," + "\"TemplateID\":\"" + templateId + "\",\"Sign\":\"" + sign + "\"," + "\"TemplateParamSet\":[\"" + token + "\",\"" + this.interval + "\"]," + "\"SmsSdkAppid\":\"" + smsSdkAppid + "\"}";
            SendSmsRequest req = SendSmsRequest.fromJsonString(params, SendSmsRequest.class);
            SendSmsResponse resp = client.SendSms(req);
            logger.debug("responseString " + SendSmsRequest.toJsonString(resp));
            if (resp.getSendStatusSet()[0].getCode().equalsIgnoreCase("Ok")) {
                this.optTokenStore.store(userInfo, token, userInfo.getMobile(), OtpTypes.SMS);
                return true;
            }
        } catch (Exception e) {
            logger.error(" produce code error ", e);
        }
    }
    return false;
}
Also used : SmsClient(com.tencentcloudapi.sms.v20190711.SmsClient) Credential(com.tencentcloudapi.common.Credential) ClientProfile(com.tencentcloudapi.common.profile.ClientProfile) HttpProfile(com.tencentcloudapi.common.profile.HttpProfile) SendSmsRequest(com.tencentcloudapi.sms.v20190711.models.SendSmsRequest) SendSmsResponse(com.tencentcloudapi.sms.v20190711.models.SendSmsResponse)

Example 5 with SendSmsResponse

use of com.aliyun.dysmsapi20170525.models.SendSmsResponse in project knife-starter by 1120023921.

the class KnifeSmsAliServiceImpl method sendSms.

@Override
public AliSmsResult sendSms(AliSms aliSms) {
    try {
        final Client client = knifeSmsAliSenderContext.getClient();
        final SendSmsRequest sendSmsRequest = new SendSmsRequest();
        BeanUtils.copyProperties(aliSms, sendSmsRequest);
        final SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
        return AliSmsResult.builder().msgId(aliSms.getMsgId()).code(sendSmsResponse.getBody().getCode()).errMsg(sendSmsResponse.getBody().getMessage()).build();
    } catch (Exception e) {
        return AliSmsResult.builder().msgId(aliSms.getMsgId()).code("-1").errMsg(e.getMessage()).build();
    }
}
Also used : SendSmsRequest(com.aliyun.dysmsapi20170525.models.SendSmsRequest) Client(com.aliyun.dysmsapi20170525.Client) SendSmsResponse(com.aliyun.dysmsapi20170525.models.SendSmsResponse)

Aggregations

SendSmsRequest (com.aliyun.dysmsapi20170525.models.SendSmsRequest)4 SendSmsResponse (com.aliyun.dysmsapi20170525.models.SendSmsResponse)4 Client (com.aliyun.dysmsapi20170525.Client)3 Credential (com.tencentcloudapi.common.Credential)3 ClientProfile (com.tencentcloudapi.common.profile.ClientProfile)3 HttpProfile (com.tencentcloudapi.common.profile.HttpProfile)3 SmsClient (com.tencentcloudapi.sms.v20190711.SmsClient)3 SendSmsRequest (com.tencentcloudapi.sms.v20190711.models.SendSmsRequest)3 SendSmsResponse (com.tencentcloudapi.sms.v20190711.models.SendSmsResponse)3 SendSmsResponseBody (com.aliyun.dysmsapi20170525.models.SendSmsResponseBody)2 TencentCloudSDKException (com.tencentcloudapi.common.exception.TencentCloudSDKException)2 SmsClient (com.tencentcloudapi.sms.v20210111.SmsClient)2 SendSmsRequest (com.tencentcloudapi.sms.v20210111.models.SendSmsRequest)2 SendSmsResponse (com.tencentcloudapi.sms.v20210111.models.SendSmsResponse)2 SneakyThrows (lombok.SneakyThrows)2 CollUtil (cn.hutool.core.collection.CollUtil)1 ArrayUtil (cn.hutool.core.util.ArrayUtil)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TencentSmsParam (com.java3y.austin.handler.domain.TencentSmsParam)1 CloudSDKException (com.mizhousoft.cloudsdk.CloudSDKException)1