阿里大于发送短信工具类

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import com.alibaba.fastjson.JSONObject;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

public class DaYuSmsApiHelper {
   final static Logger LOG = LoggerFactory.getLogger(DaYuSmsApiHelper.class);

   final static String appKey = "23682801";
   final static String appSecret = "49fe5a819c9e4054977bdd51289e438d";
   final static String sign = "22";
   // final static String sendSMSUrl = "https://eco.taobao.com/router/rest";
   final static String sendSMSUrl = "http://gw.api.taobao.com/router/rest";
   
   final static String deliverTempId = "SMS_121010002";
   
   /**
    * 发货通知
    * @param mobile
    * @param recipientName
    * @param courierCompany
    * @param trackingNumber
    */
   public static void sendDeliverSMS(String mobile, String recipientName, String courierCompany, 
         String trackingNumber) {
      JSONObject param = new JSONObject();
      param.put("name", recipientName);
      param.put("com", courierCompany);
      param.put("num", trackingNumber);
      sendSMS(mobile, param, deliverTempId, sign);
   }

   /**
    * 发送短信接口
    */
   private static void sendSMS(String mobile, JSONObject param, String tempId,
         String sign) {
      LOG.debug("阿里大于短信请求内容, mobile:{}, param:{}", mobile, param);
      try {
         TaobaoClient client = new DefaultTaobaoClient(sendSMSUrl, appKey, appSecret);
         AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
         // req.setExtend("123456"); // 回调参数
         req.setSmsType("normal");
         req.setSmsFreeSignName(sign);
         req.setSmsParamString(param.toString());
         req.setRecNum(mobile);
         req.setSmsTemplateCode(tempId);
         AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
         String content = rsp.getBody();
         LOG.debug("阿里大于短信响应内容,response:{}", content);
         
         JSONObject contentJson = JSONObject.parseObject(content);
         String errorResponse = contentJson.getString("error_response");
         if (StringUtils.isNotBlank(errorResponse))
            Assert.isTrue(false, "发送短信失败");
      } catch (ApiException e) {
         LOG.error("", e);
         throw new IllegalArgumentException("发送短信失败");
      }
   }

   public static void main(String[] args) throws Exception {
      sendDeliverSMS("222222", "22222", "顺丰快递", "123456");
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值