腾讯云模板API对接

由于腾讯云sdk只提供了短信发送的方法,模板、签名的方法只提供了url需要自己按照短信sdk方法review。所以特此记录。

1.先编写返回值的对象类

public class SmsTemplateResult extends SmsResultBase {

    private int result;                     //  错误码,0表示成功,非0表示失败
    private String errMsg = "";             //	错误消息,result 返回非0时提示的具体错误信息
    private int total;                      //  应用的模版总数,result 为0时有效,分页全量拉取时才有该参数
    private int count;                      //  返回的信息条数,result 为0时有效,信息内容在 data 字段中
    private TemplateData data;
    private ArrayList<TemplateData> dataList = new ArrayList();

    public int getResult() {
        return result;
    }

    public String getErrMsg() {
        return errMsg;
    }

    public TemplateData getData() {
        return data;
    }

    public int getTotal() {
        return total;
    }

    public int getCount() {
        return count;
    }

    public ArrayList<TemplateData> getDataList() {
        return dataList;
    }

    public SmsTemplateResult() {
    }

    @Override
    public SmsTemplateResult parseFromHTTPResponse(HTTPResponse response) throws JSONException {
        JSONObject json = this.parseToJson(response);
        this.result = json.getInt("result");
        this.errMsg = json.getString("errmsg");
        if (json.has("total")) {
            this.total = json.getInt("total");
        }
        if (json.has("count")) {
            this.count = json.getInt("count");
        }
        if (json.has("data")) {
            Object data = json.get("data");
            if (data instanceof JSONObject && json.has("data")) {
                JSONObject jsonObject = (JSONObject) json.get("data");
                TemplateData templateData = new TemplateData().parse(jsonObject);
                this.data = templateData;
            }
            if (data instanceof JSONArray && json.has("data") && !json.isNull("data")) {
                JSONArray jsonData = json.getJSONArray("data");

                for (int i = 0; i < jsonData.length(); ++i) {
                    this.dataList.add((new TemplateData()).parse(jsonData.getJSONObject(i)));
                }
            }
        }
        return this;
    }
}
public class TemplateData {

    private Long id;                //	模板 ID
    private int international;      //	0表示国内短信,1表示国际/港澳台短信,默认为0
    private int status;             //  模板状态,0表示已通过, 1表示待审核, 2表示已拒绝
    private String text;            //	模板内容
    private int type;               //  短信类型,0表示普通短信, 1表示营销短信
    private String title;           //  模板名称
    private String reply;           //  审批信息,如果 status 为2,会说明拒绝原因
    private String apply_time;      //	申请时间
    private String reply_time;      //  审核时间

    public String getTitle() {
        return title;
    }

    public String getReply() {
        return reply;
    }

    public String getApply_time() {
        return apply_time;
    }

    public String getReply_time() {
        return reply_time;
    }

    public Long getId() {
        return id;
    }

    public String getText() {
        return text;
    }

    public int getInternational() {
        return international;
    }

    public int getStatus() {
        return status;
    }

    public int getType() {
        return type;
    }

    public TemplateData parse(JSONObject json) throws JSONException {
        this.id = json.getLong("id");
        if (json.has("international")) {
            this.international = json.getInt("international");
        }
        this.status = json.getInt("status");
        this.text = json.getString("text");
        if (json.has("type")) {
            this.type = json.getInt("type");
        }
        if (json.has("reply")) {
            this.reply = json.getString("reply");
        }
        if (json.has("title")) {
            this.title = json.getString("title");
        }
        if (json.has("apply_time")) {
            this.apply_time = json.getString("apply_time");
        }
        if (json.has("reply_time")) {
            this.reply_time = json.getString("apply_time");
        }

        return this;
    }
}

2.然后写request请求响应(这里只写了增加的方法。其他方法类似可参照)

import com.github.qcloudsms.SmsBase;
import com.github.qcloudsms.SmsSenderUtil;
import com.github.qcloudsms.httpclient.*;
import com.xinpengtech.api.txc.sms.result.SmsTemplateResult;
import org.json.JSONObject;
import org.springframework.data.domain.Pageable;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
public class TemplateAdapter extends SmsBase {

    //腾讯云提供的url
    private String addUrl = "https://yun.tim.qq.com/v5/tlssmssvr/add_template";

    public TemplateAdapter(int appid, String appkey) {
        super(appid, appkey, new DefaultHTTPClient());
    }

    public TemplateAdapter(int appid, String appkey, HTTPClient httpclient) {
        super(appid, appkey, httpclient);
    }

    /**
     * 添加模板
     *
     * @param remark        模板备注
     * @param international 0表示国内短信,1表示国际/港澳台短信,默认为0
     * @param text          模板内容
     * @param title         模板名称
     * @param type          短信类型,0表示普通短信, 1表示营销短信
     */
    public SmsTemplateResult addTemplate(String remark, int international, String text, String title, int type) {

        long random = SmsSenderUtil.getRandom();
        long now = SmsSenderUtil.getCurrentTime();

        JSONObject body = new JSONObject();
        body.put("remark", SmsSenderUtil.isNotEmpty(remark) ? remark : "");
        body.put("international", international);
        body.put("sig", SmsSenderUtil.calculateSignature(this.appkey, random, now));
        body.put("text", text);
        body.put("time", now);
        body.put("title", SmsSenderUtil.isNotEmpty(title) ? title : "");
        body.put("type", type);

        HTTPRequest req = (new HTTPRequest(HTTPMethod.POST, this.addUrl)).addHeader("Conetent-Type", "application/json").addQueryParameter("sdkappid", this.appid).addQueryParameter("random", random).setConnectionTimeout(60000).setRequestTimeout(60000).setBody(body.toString());

        try {
            HTTPResponse res = this.httpclient.fetch(req);
            this.handleError(res);
            return (new SmsTemplateResult()).parseFromHTTPResponse(res);
        } catch (URISyntaxException | IOException | HTTPException var15) {
            throw new RuntimeException("API url has been modified, current url: " + this.addUrl);
        }

    }
}

个人意见:对比腾讯云和阿里云的短信接口,明显阿里云做的好一些,建议使用阿里云。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值