前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >短信发送接口 – www.yuntongxun.com

短信发送接口 – www.yuntongxun.com

作者头像
前Thoughtworks-杨焱
发布2021-12-08 09:05:21
3.2K0
发布2021-12-08 09:05:21
举报
文章被收录于专栏:杨焱的专栏
代码语言:javascript
复制
public class RonglianPhoneCodeProvider {


    Logger logger = LoggerFactory.getLogger(this.getClass());
    private String sId = ""; //用户id
    private String appId = ""; //应用id
    private String token = ""; //用户token
    private String respUrl = ""; //语音短信结果回调url
    private boolean sanbox = true; //是否是调试环境

    public void setSanbox(boolean sanbox) {
        this.sanbox = sanbox;
    }

    public void setRespUrl(String respUrl) {
        this.respUrl = respUrl;
    }

    public void setsId(String sId) {
        this.sId = sId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public int sendSmsMsg(String phone, String msgTemplateId, String[] data) {
        HashMap<String, Object> result = null;

        CCPRestSDK restAPI = new CCPRestSDK();
        if (sanbox) {
            restAPI.init("sandboxapp.cloopen.com", "8883");// 初始化服务器地址和端口,格式如下,服务器地址不需要写https://
        } else {
            restAPI.init("app.cloopen.com", "8883");// 初始化服务器地址和端口,格式如下,服务器地址不需要写https://
        }
        restAPI.setAccount(sId, token);// 初始化主帐号和主帐号TOKEN
        restAPI.setAppId(appId);// 初始化应用ID

//        TODO
        result = restAPI.sendTemplateSMS(phone, "1", data);
        if ("000000".equals(result.get("statusCode"))) { //成功
            return 1;
        } else {
            //异常返回输出错误码和错误信息
            String statusCode = result.get("statusCode").toString();
            String statusMsg = result.get("statusMsg").toString();
            this.onSmsMsgFailed(phone, msgTemplateId, statusCode, statusMsg);
            return -1;
        }
    }


    public int sendVoiceNumber(String phone, String number) {

        HashMap<String, Object> result = null;

        CCPRestSDK restAPI = new CCPRestSDK();
        if (sanbox) {
            restAPI.init("sandboxapp.cloopen.com", "8883");// 初始化服务器地址和端口,格式如下,服务器地址不需要写https://
        } else {
            restAPI.init("app.cloopen.com", "8883");// 初始化服务器地址和端口,格式如下,服务器地址不需要写https://
        }
        restAPI.setAccount(sId, token);// 初始化主帐号和主帐号TOKEN
        restAPI.setAppId(appId);// 初始化应用ID


        String times = String.valueOf(3);
        result = restAPI.voiceVerify(String.valueOf(number), phone, null, times, respUrl, "zh", "");
        if ("000000".equals(result.get("statusCode"))) { //成功
            return 1;
        } else {
            //异常返回输出错误码和错误信息
            String statusCode = result.get("statusCode").toString();
            String statusMsg = result.get("statusMsg").toString();
            this.onVoiceNumberFailed(phone, number, statusCode, statusMsg);
            return -1;
        }
    }

    public void onSmsMsgFailed(String phone, String msg, String errorCode, String errorMsg) {
        logger.error("短信【内容】消息[" + phone + "," + msg + "] 发送失败(" + errorCode + "):" + errorMsg);
        if (errorCode.equals("111142") || errorCode.equals("111158")
                || errorCode.equals("160014")) { //欠费了,需要通知管理员缴费

        }
    }

    public void onVoiceNumberFailed(String phone, String number, String errorCode, String errorMsg) {
        logger.error("语音【验证码】消息[" + phone + "," + number + "] 发送失败(" + errorCode + "):" + errorMsg);
        if (errorCode.equals("111142") || errorCode.equals("111158")
                || errorCode.equals("160014")) { //欠费了,需要通知管理员缴费

        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2015-10-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
短信
腾讯云短信(Short Message Service,SMS)可为广大企业级用户提供稳定可靠,安全合规的短信触达服务。用户可快速接入,调用 API / SDK 或者通过控制台即可发送,支持发送验证码、通知类短信和营销短信。国内验证短信秒级触达,99%到达率;国际/港澳台短信覆盖全球200+国家/地区,全球多服务站点,稳定可靠。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档