短信平台有哪些
最为著名的三大平台:华为短信、阿里短信、腾讯短信;根据各家官网的接入文档进行使用
短信平台接入流程
需要具备哪些信息
各家所需信息不太相同,大致如下:
1.app key
2.app 秘钥
3.app 签名
3.发送者id
4.模版,替换符
5.调用接口
6.异步回调接口
华为云接入代码
/*** 异步发送华为云短信*/@Async@Overridepublic void sendSmsMsg(SsoSms sms) throws Exception {if (sms.getPhone() == null || sms.getPhone().length() < 11) {log.info("手机号不符合规则:" + sms.getPhone());}//必填,全局号码格式(包含国家码),示例:+8615123456789,多个号码之间用英文逗号分隔String receiver = sms.getPhone(); //短信接收人号码/*** 选填,使用无变量模板时请赋空值 String templateParas = "";* 单变量模板示例:模板内容为"您的验证码是${1}"时,templateParas可填写为"[\"369751\"]"* 双变量模板示例:模板内容为"您有${1}件快递请到${2}领取"时,templateParas可填写为"[\"3\",\"人民公园正门\"]"* 模板中的每个变量都必须赋值,且取值不能为空* 查看更多模板和变量规范:产品介绍>模板和变量规范*/String templateParas = "[" + sms.getCode() + "]"; //模板变量,此处以单变量验证码短信为例,请客户自行生成6位验证码,并定义为字符串类型,以杜绝首位0丢失的问题(例如:002569变成了2569)。//请求Body,不携带签名名称时,signature请填nullString body = SmsUtils.buildRequestBody(sender, receiver, templateId, templateParas, statusCallBack, signature);if (null == body || body.isEmpty()) {log.error(sms.getPhone() + ":消息体为空");return;}//请求Headers中的X-WSSE参数值String wsseHeader = SmsUtils.buildWsseHeader(appKey, appSecret);if (null == wsseHeader || wsseHeader.isEmpty()) {log.error(sms.getPhone() + ":消息头为空");return;}//为防止因HTTPS证书认证失败造成API调用失败,需要先忽略证书信任问题CloseableHttpClient client = HttpClients.custom().setSSLContext(new SSLContextBuilder().loadTrustMaterial(null,(x509CertChain, authType) -> true).build()).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build();HttpResponse response = client.execute(RequestBuilder.create("POST")//请求方法POST.setUri(url).addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded").addHeader(HttpHeaders.AUTHORIZATION, SmsUtils.AUTH_HEADER_VALUE).addHeader("X-WSSE", wsseHeader).setEntity(new StringEntity(body)).build());String result = EntityUtils.toString(response.getEntity());log.info("返回码:" + response.getStatusLine().getStatusCode() + "|返回体:" + result); //打印响应消息实体sms.setCode(response.getStatusLine().getStatusCode() + "");sms.setMessage(result);sms.setBizId("弃用");ssoSmsMapper.updateById(sms);}
阿里云接入代码
/*** 异步发送阿里云短信** @param sms* @return*/@Async@Overridepublic SendSmsResponse sendSmsMsg(Sms sms) {if (sms.getSignName() == null) {sms.setSignName(this.signName);}if (sms.getTemplateCode() == null) {sms.setTemplateCode(this.templateCode);}// 阿里云短信官网demo代码SendSmsRequest request = new SendSmsRequest();request.setMethod(MethodType.POST);request.setPhoneNumbers(sms.getPhone());request.setSignName(sms.getSignName());request.setTemplateCode(sms.getTemplateCode());request.setTemplateParam(sms.getParams());request.setOutId(sms.getId().toString());SendSmsResponse response = null;try {response = acsClient.getAcsResponse(request);if (response != null) {log.info("发送短信结果:code:{},message:{},requestId:{},bizId:{}", response.getCode(), response.getMessage(),response.getRequestId(), response.getBizId());sms.setCode(response.getCode());sms.setMessage(response.getMessage());sms.setBizId(response.getBizId());}} catch (ClientException e) {e.printStackTrace();}update(sms);return response;}
腾讯云接入代码
/*** 异步发送腾讯云短信*/@Async@Overridepublic SmsSingleSenderResult sendSmsMsg(SsoSms sms) {if (sms.getSignName() == null) {sms.setSignName(this.txYunAppId + "");}if (sms.getTemplateCode() == null) {sms.setTemplateCode(this.templateCode + "");}SmsSingleSender e = new SmsSingleSender(txYunAppId, txYunAppKey);SmsSingleSenderResult result = null;try {ArrayList<String> params = new ArrayList<>();params.add(sms.getCode());result = e.sendWithParam("86", sms.getPhone(), templateCode, params, (String)null, "", "");log.info("发送短信结果:result:{},message:{},bizId:{}", result.result, result.errMsg, result.sid);} catch (JSONException e1) {e1.printStackTrace();} catch (HTTPException e1) {e1.printStackTrace();} catch (IOException e1) {e1.printStackTrace();}ssoSmsMapper.updateById(sms);return result;}