Search in sources :

Example 1 with SendSmsRequest

use of com.aliyun.dysmsapi20170525.models.SendSmsRequest 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 SendSmsRequest

use of com.aliyun.dysmsapi20170525.models.SendSmsRequest in project tech-pdai-spring-demos by realpdai.

the class SmsServiceImpl method sendSms.

/**
 * send sms.
 *
 * @param pNos          phone num
 * @param templateCode  template code
 * @param signName      sign name
 * @param templateParam template param
 * @throws Exception Exception
 */
public void sendSms(List<String> pNos, String templateCode, String signName, String templateParam) throws Exception {
    Config config = new Config().setAccessKeyId(smsConfig.getAccessKeyId()).setAccessKeySecret(smsConfig.getAccessKeySecret()).setEndpoint(smsConfig.getDomain()).setConnectTimeout(10000).setReadTimeout(10000);
    com.aliyun.dysmsapi20170525.Client client = new com.aliyun.dysmsapi20170525.Client(config);
    for (String pNo : pNos) {
        SendSmsRequest sendSmsRequest = new SendSmsRequest().setPhoneNumbers(pNo).setSignName(signName).setTemplateCode(templateCode).setTemplateParam(templateParam);
        log.info(JsonUtil.toJson(client.sendSms(sendSmsRequest)));
    }
}
Also used : SmsConfig(tech.pdai.springboot.sms.ali.config.SmsConfig) Config(com.aliyun.teaopenapi.models.Config) SendSmsRequest(com.aliyun.dysmsapi20170525.models.SendSmsRequest)

Example 3 with SendSmsRequest

use of com.aliyun.dysmsapi20170525.models.SendSmsRequest 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 4 with SendSmsRequest

use of com.aliyun.dysmsapi20170525.models.SendSmsRequest in project longmarch by yuyueqty.

the class MsgUtil method sendSms.

public static List<MessageResultInfoVO> sendSms(String code, String[] phoneNumber, MsgConf conf) {
    List<MessageResultInfoVO> messageResultInfoVOList = new ArrayList<>();
    /* * 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey */
    // 密钥对在腾讯云搜索即可找到
    Credential cred = new Credential(conf.getSecretId(), conf.getSecretKey());
    // 实例化一个http选项,可选,没有特殊需求可以跳过
    HttpProfile httpProfile = new HttpProfile();
    /* SDK默认使用POST方法。
         * 如果你一定要使用GET方法,可以在这里设置。GET方法无法处理一些较大的请求 */
    httpProfile.setReqMethod("POST");
    /* SDK有默认的超时时间,非必要请不要进行调整*/
    httpProfile.setConnTimeout(60);
    /* SDK会自动指定域名。通常是不需要特地指定域名的,但是如果你访问的是金融区的服务
         * 则必须手动指定域名,例如sms的上海金融区域名: sms.ap-shanghai-fsi.tendentious.com */
    httpProfile.setEndpoint(conf.getDomain());
    /* 非必要步骤:
         * 实例化一个客户端配置对象,可以指定超时时间等配置 */
    ClientProfile clientProfile = new ClientProfile();
    /* SDK默认用TC3-HMAC-SHA256进行签名
         * 非必要请不要修改这个字段 */
    clientProfile.setSignMethod("HmacSHA256");
    clientProfile.setHttpProfile(httpProfile);
    /* 实例化要请求产品(以sms为例)的client对象
         * 第二个参数是地域信息,可以直接填写字符串ap-guangzhou,或者引用预设的常量 */
    SmsClient client = new SmsClient(cred, conf.getRegionId(), clientProfile);
    /* 实例化一个请求对象,根据调用的接口和实际情况,可以进一步设置请求参数 */
    SendSmsRequest req = new SendSmsRequest();
    /* 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId,示例如1400006666 */
    req.setSmsSdkAppId(conf.getSdkAppId());
    /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名,签名信息可登录 [短信控制台] 查看 */
    req.setSignName(conf.getSignName());
    /* 国际/港澳台短信 SenderId: 国内短信填空,默认未开通,如需开通请联系 [sms helper] */
    req.setSenderId("");
    /* 用户的 session 内容: 可以携带用户侧 ID 等上下文信息,server 会原样返回  这个参数需要带上*/
    req.setSessionContext("");
    /* 短信号码扩展号: 默认未开通,如需开通请联系 [sms helper]*/
    req.setExtendCode("");
    /* 模板 ID: 必须填写已审核通过的模板 ID。模板ID可登录 [短信控制台] 查看 */
    req.setTemplateId(conf.getTemplateCode());
    /* 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]
         * 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号 */
    req.setPhoneNumberSet(phoneNumber);
    /* 模板参数: 若无模板参数,则设置为空 */
    String[] templateParamSet = { code, conf.getExpireMinute() };
    System.out.println(code);
    req.setTemplateParamSet(templateParamSet);
    /* 通过 client 对象调用 SendSms 方法发起请求。注意请求方法名与请求对象是对应的
         * 返回的 res 是一个 SendSmsResponse 类的实例,与请求对象对应 */
    try {
        messageResultInfoVOList = MessageResultInfoVO.build(client.SendSms(req));
    } catch (TencentCloudSDKException e) {
        e.printStackTrace();
    }
    return messageResultInfoVOList;
}
Also used : SmsClient(com.tencentcloudapi.sms.v20210111.SmsClient) Credential(com.tencentcloudapi.common.Credential) TencentCloudSDKException(com.tencentcloudapi.common.exception.TencentCloudSDKException) ClientProfile(com.tencentcloudapi.common.profile.ClientProfile) HttpProfile(com.tencentcloudapi.common.profile.HttpProfile) ArrayList(java.util.ArrayList) SendSmsRequest(com.tencentcloudapi.sms.v20210111.models.SendSmsRequest)

Example 5 with SendSmsRequest

use of com.aliyun.dysmsapi20170525.models.SendSmsRequest 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)

Aggregations

SendSmsRequest (com.aliyun.dysmsapi20170525.models.SendSmsRequest)5 SendSmsResponse (com.aliyun.dysmsapi20170525.models.SendSmsResponse)4 Credential (com.tencentcloudapi.common.Credential)4 ClientProfile (com.tencentcloudapi.common.profile.ClientProfile)4 HttpProfile (com.tencentcloudapi.common.profile.HttpProfile)4 SendSmsRequest (com.tencentcloudapi.sms.v20210111.models.SendSmsRequest)4 Client (com.aliyun.dysmsapi20170525.Client)3 TencentCloudSDKException (com.tencentcloudapi.common.exception.TencentCloudSDKException)3 SmsClient (com.tencentcloudapi.sms.v20190711.SmsClient)3 SendSmsRequest (com.tencentcloudapi.sms.v20190711.models.SendSmsRequest)3 SendSmsResponse (com.tencentcloudapi.sms.v20190711.models.SendSmsResponse)3 SmsClient (com.tencentcloudapi.sms.v20210111.SmsClient)3 SendSmsResponseBody (com.aliyun.dysmsapi20170525.models.SendSmsResponseBody)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 Config (com.aliyun.teaopenapi.models.Config)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 TencentSmsParam (com.java3y.austin.handler.domain.TencentSmsParam)1