短信发送接口 – SubMail

public class SubMailSmsSender {

    private static Logger logger = LoggerFactory.getLogger(SubMailSmsSender.class);
    private String submailSendUrl;
    private String submailAppid;
    private String submailAppkey;

    public SubMailSmsSender(String submailSendUrl, String submailAppid, String submailAppkey) {
        this.submailSendUrl = submailSendUrl;
        this.submailAppid = submailAppid;
        this.submailAppkey = submailAppkey;
    }

    ObjectMapper objectMapper = new ObjectMapper();

    /**
     * SUBMAIL 的短信一对多(即1条API请求发送多个号码,并可以灵活控制每个联系人的文本变量)和群发 API 。调用方需要确保 templateId 正确, smsList 中至少有一条
     *
     * @param templateId 短信模板
     * @param multi      批量发送列表json字符串
     * @return 返回发送成功的接收方手机号列表
     */
    public List<String> multixsend(String templateId, String multi) {
        String info = null;
        boolean success = false;
        List<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("appid", submailAppid));
        params.add(new BasicNameValuePair("project", templateId));
        params.add(new BasicNameValuePair("multi", multi));
        params.add(new BasicNameValuePair("signature", submailAppkey));

        try {
            List<String> successPhoneList = new ArrayList<>();
            HttpResponse post = HttpClientHelper.INSTANCE.post(submailSendUrl, params, "utf-8", null, null);
            info = EntityUtils.toString(post.getEntity(), "utf-8");
            if (logger.isDebugEnabled()) {
                logger.debug("SUBMAIL 返回:" + info);
            }
            JsonNode jsonNode = objectMapper.readTree(info);
            Iterator<JsonNode> iterator = jsonNode.iterator();
            while (iterator.hasNext()) {
                JsonNode next = iterator.next();
                String status = next.get("status").asText();
                String to = next.get("to").asText();
                if ("success".equals(status)) {
                    successPhoneList.add(to);
                    if (logger.isInfoEnabled()) {
                        logger.info("成功发送短信给[" + to + "]");
                    }
                } else {
                    String logContent = "发送短信给[" + to + "]失败";
                    if (logger.isInfoEnabled()) {
                        logger.error(logContent);
                    }
                }
            }
            return successPhoneList;
        } catch (IOException e) {
            logger.error("请求 SubMail 服务器发送短信产生IO 错误", e);
            return Collections.emptyList();
        }
    }

    /**
     * SUBMAIL 的短信一对多(即1条API请求发送多个号码,并可以灵活控制每个联系人的文本变量)和群发 API 。调用方需要确保 templateId 正确, smsList 中至少有一条
     *
     * @param templateId 短信模板
     * @param smsList    批量发送列表
     * @return 返回发送成功的接收方手机号列表
     */
    public List<String> multixsend(String templateId, List<SubMailSms> smsList) {

        if (smsList == null || smsList.isEmpty()) {
            return Collections.emptyList();
        }
        String multi = null;
        try {
            multi = objectMapper.writeValueAsString(smsList);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return this.multixsend(templateId, multi);
    }

    /**
     * 短信发送信息
     */
    static class SubMailSms {
        private String to;
        private Map<String, String> vars;

        public String getTo() {
            return to;
        }

        public void setTo(String to) {
            this.to = to;
        }

        public Map<String, String> getVars() {
            return vars;
        }

        public void setVars(Map<String, String> vars) {
            this.vars = vars;
        }
    }
}

 

Leave a Comment

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据