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");
}
}
阿里大于发送短信工具类
最新推荐文章于 2021-08-13 10:59:28 发布