package com.farriver.bwf.service.config; import com.farriver.bwf.common.utilities.HttpUtil; import com.tencentcloudapi.common.Credential; import com.tencentcloudapi.common.exception.TencentCloudSDKException; import com.tencentcloudapi.common.profile.ClientProfile; import com.tencentcloudapi.common.profile.HttpProfile; import com.tencentcloudapi.sms.v20210111.SmsClient; import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest; import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse; import jakarta.annotation.Resource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.client.RestTemplate; public class SMSMessageConfig { private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class); @Resource private RestTemplate restTemplate; // @RequestMapping(value = "/sendsmsTest", method = RequestMethod.GET) // public ApiData sendsmsTest() throws IOException { // //单发短信API // String url = "https://open.ucpaas.com/ol/sms/sendsms"; // JSONObject jsonObject = new JSONObject(); // //基础配置,在开发平台认证后获取 // jsonObject.put("sid","ad024f8****************05d1614"); // jsonObject.put("token","5ddbf62d4d****************e27402c"); // jsonObject.put("appid","0ceaca4708****************76ec45f"); // //模板ID,在开发平台创建模板对应的模板ID // jsonObject.put("templateid", "432116"); // //模板对应的参数,参数之间拼接用逗号作为间隔符 // jsonObject.put("param", "1315,500"); // //要发送的手机号 // jsonObject.put("mobile", "用户的手机号"); // //用户透传ID,随状态报告返回,可以不填写 // jsonObject.put("uid",""); // String json = JSONObject.toJSONString(jsonObject); // //使用restTemplate进行访问远程服务 // HttpHeaders headers = new HttpHeaders(); // headers.setContentType(MediaType.APPLICATION_JSON_UTF8); // HttpEntity httpEntity = new HttpEntity(json, headers); // String result = restTemplate.postForObject(url, httpEntity, String.class); // return result; // JSONObject json = new JSONObject(); // json.put("PhoneNumberSet", ["+8618511122266", "+8618511122233"]); // json.put("SmsSdkAppId", "1400006666"); // json.put("SignName", "腾讯云"); // json.put("TemplateId", "1234"); // json.put("TemplateParamSet", ["12345"]); // json.put("SessionContext", "test"); // // String params = json.toJSONString(); // // String url = "sms.tencentcloudapi.com"; // // CloseableHttpClient httpClient = HttpClients.createDefault(); // // HttpPost httpPost = new HttpPost(url); // RequestConfig defaultRequestConfig = RequestConfig // .custom() // .setSocketTimeout(5000) // .setConnectTimeout(5000) // .setConnectionRequestTimeout(5000) // .build(); // httpPost.setConfig(defaultRequestConfig); // httpPost.addHeader("Content-Type", "application/json;charset=UTF-8"); // httpPost.addHeader("X-TC-Action", "SendSms"); // httpPost.addHeader("x-forwarded-for", HttpUtil.GetDynamicIP()); // httpPost.setEntity(new StringEntity(params, "UTF-8")); // // CloseableHttpResponse response = null; // // try { // response = httpClient.execute(httpPost); // StatusLine statusline = response.getStatusLine(); // logger.info("statusline: " + statusline); // // int statusCode = statusline.getStatusCode(); // logger.info("statusCode: " + statusCode); // // org.apache.http.HttpEntity entity = response.getEntity(); // String result = EntityUtils.toString(entity, "UTF-8"); // logger.info("result: " + result); // // return new ApiData(statusCode, statusline.toString(), result, null); // } catch (Exception e) { // return ApiData.error(e.getMessage(), e); // } finally { // if (response != null) response.close(); // httpClient.close(); // } // } // @RequestMapping(value = "/sendBatchsmsTest", method = RequestMethod.GET) // public String sendBatchsmsTest() { // //群发短信API // String url = "https://open.ucpaas.com/ol/sms/sendsms_batch"; // JSONObject jsonObject = new JSONObject(); // //基础配置,在开发平台认证后获取 // jsonObject.put("sid", "ad024f8****************05d1614"); // jsonObject.put("token", "5ddbf62d4d****************e27402c"); // jsonObject.put("appid", "0ceaca4708****************76ec45f"); // //模板ID,在开发平台创建模板对应的模板ID // jsonObject.put("templateid", "432116"); // //模板对应的参数,参数之间拼接用逗号作为间隔符 // jsonObject.put("param", "1315,500"); // //群发多个手机号之间要用逗号作为间隔符 // jsonObject.put("mobile", "用户的手机号A,用户的手机号B"); // //用户透传ID,随状态报告返回,可以不填写 // jsonObject.put("uid", ""); // String json = JSONObject.toJSONString(jsonObject); // //使用restTemplate进行访问远程服务 // HttpHeaders headers = new HttpHeaders(); // headers.setContentType(MediaType.APPLICATION_JSON_UTF8); // HttpEntity httpEntity = new HttpEntity(json, headers); // String result = restTemplate.postForObject(url, httpEntity, String.class); // return result; // } public static void send() { try{ // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密 // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取 Credential cred = new Credential("SecretId", "SecretKey"); // 实例化一个http选项,可选的,没有特殊需求可以跳过 HttpProfile httpProfile = new HttpProfile(); httpProfile.setEndpoint("sms.tencentcloudapi.com"); // 实例化一个client选项,可选的,没有特殊需求可以跳过 ClientProfile clientProfile = new ClientProfile(); clientProfile.setHttpProfile(httpProfile); // 实例化要请求产品的client对象,clientProfile是可选的 SmsClient client = new SmsClient(cred, "", clientProfile); // 实例化一个请求对象,每个接口都会对应一个request对象 SendSmsRequest req = new SendSmsRequest(); String[] phoneNumberSet1 = {"18222052509"}; req.setPhoneNumberSet(phoneNumberSet1); req.setSmsSdkAppId("1400583314"); req.setSignName("testsign"); req.setTemplateId("腾讯云"); // 返回的resp是一个SendSmsResponse的实例,与请求对象对应 SendSmsResponse resp = client.SendSms(req); // 输出json格式的字符串回包 System.out.println(SendSmsResponse.toJsonString(resp)); } catch (TencentCloudSDKException e) { System.out.println(e.toString()); } } }