|
|
@@ -0,0 +1,240 @@
|
|
|
+package com.izouma.awesomeAdmin.utils;
|
|
|
+
|
|
|
+
|
|
|
+import com.fasterxml.jackson.databind.json.JsonMapper;
|
|
|
+import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
|
|
+import com.izouma.awesomeAdmin.pojo.OutboundMessageRequest;
|
|
|
+import com.izouma.awesomeAdmin.pojo.Suggestions;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import org.apache.commons.codec.binary.Base64;
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
|
+import org.apache.http.conn.ssl.TrustStrategy;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import javax.net.ssl.SSLContext;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.security.cert.X509Certificate;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.ZoneId;
|
|
|
+import java.time.ZoneOffset;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Locale;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+public class CspUtil {
|
|
|
+ private final static String username = "njzm01";
|
|
|
+ private final static String password = "mHCSE$EGSxQ@&DuNsM04";
|
|
|
+ private final static String chatBotId = "10650511101001400001@botplatform.rcs.chinamobile.com";
|
|
|
+ private final static String serverRoot = "https://api.5gcsp.mas.10086.cn/ocsp/developer";
|
|
|
+ private final static String fileServerRoot = "https://api.5gcsp.mas.10086.cn/ocsp/fileservice";
|
|
|
+ private static RestTemplate restTemplate;
|
|
|
+
|
|
|
+ private static RestTemplate getRestTemplate() {
|
|
|
+ if (restTemplate == null) {
|
|
|
+ try {
|
|
|
+ TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
|
|
|
+
|
|
|
+ SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
|
|
|
+ .loadTrustMaterial(null, acceptingTrustStrategy)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
|
|
|
+
|
|
|
+ CloseableHttpClient httpClient = HttpClients.custom()
|
|
|
+ .setSSLSocketFactory(csf)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ HttpComponentsClientHttpRequestFactory requestFactory =
|
|
|
+ new HttpComponentsClientHttpRequestFactory();
|
|
|
+
|
|
|
+ requestFactory.setHttpClient(httpClient);
|
|
|
+ restTemplate = new RestTemplate(requestFactory);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return restTemplate;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getToken() {
|
|
|
+ return DigestUtils.sha256Hex(password);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getBasic() {
|
|
|
+ String date = getDate();
|
|
|
+ return Base64.encodeBase64String((username + ":" + DigestUtils.sha256Hex(getToken() + date))
|
|
|
+ .getBytes(StandardCharsets.UTF_8));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getDate() {
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter
|
|
|
+ .ofPattern("EEE, dd MMM yyyy HH:mm:ss z")
|
|
|
+ .withZone(ZoneId.of("GMT+0"))
|
|
|
+ .withLocale(Locale.ENGLISH);
|
|
|
+ return formatter.format(LocalDateTime.now(ZoneOffset.UTC));
|
|
|
+ }
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ public static void sendTemplate(String templateId, String... tel) {
|
|
|
+ OutboundMessageRequest request = new OutboundMessageRequest();
|
|
|
+ request.setDestinationAddress(Arrays.asList(tel));
|
|
|
+ request.setContentType("static-template");
|
|
|
+ request.setBodyText("{\"templateID\": \"" + templateId + "\"}");
|
|
|
+ request.setContributionID(UUID.randomUUID().toString());
|
|
|
+
|
|
|
+ XmlMapper xmlMapper = new XmlMapper();
|
|
|
+ xmlMapper.writeValueAsString(request);
|
|
|
+ String xmlString = xmlMapper.writeValueAsString(request);
|
|
|
+
|
|
|
+ String url = serverRoot + "/messaging/group/template/outbound/sip:" + chatBotId + "/requests";
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.add("Authorization", "Basic " + getBasic());
|
|
|
+ headers.add("Date", getDate());
|
|
|
+
|
|
|
+ headers.setContentType(MediaType.APPLICATION_XML);
|
|
|
+ HttpEntity<String> formEntity = new HttpEntity<>(xmlString, headers);
|
|
|
+ ResponseEntity<String> responseEntity = getRestTemplate().postForEntity(url, formEntity, String.class);
|
|
|
+ System.out.println(responseEntity);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ public static void sendNormal(String... tel) {
|
|
|
+ OutboundMessageRequest request = new OutboundMessageRequest();
|
|
|
+ request.setDestinationAddress(Arrays.asList(tel));
|
|
|
+ request.setContentType("text/plain");
|
|
|
+ request.setBodyText("hello 5g");
|
|
|
+ request.setContributionID(UUID.randomUUID().toString());
|
|
|
+
|
|
|
+ XmlMapper xmlMapper = new XmlMapper();
|
|
|
+ xmlMapper.writeValueAsString(request);
|
|
|
+ String xmlString = xmlMapper.writeValueAsString(request);
|
|
|
+
|
|
|
+ String url = serverRoot + "/messaging/group/plain/outbound/sip:" + chatBotId + "/requests";
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.add("Authorization", "Basic " + getBasic());
|
|
|
+ headers.add("Date", getDate());
|
|
|
+
|
|
|
+ headers.setContentType(MediaType.APPLICATION_XML);
|
|
|
+ HttpEntity<String> formEntity = new HttpEntity<>(xmlString, headers);
|
|
|
+ ResponseEntity<String> responseEntity = getRestTemplate().postForEntity(url, formEntity, String.class);
|
|
|
+ System.out.println(responseEntity);
|
|
|
+ }
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ public static void sendTextWithSuggestion(String... tel) {
|
|
|
+ OutboundMessageRequest request = new OutboundMessageRequest();
|
|
|
+ request.setDestinationAddress(Arrays.asList(tel));
|
|
|
+ request.setContentType("multipart/mixed; boundary=\"next\"");
|
|
|
+ String body = "";
|
|
|
+ body = addTextBody(body, "hello 5g");
|
|
|
+ body = addSuggestionBody(body, new Suggestions()
|
|
|
+ .addReply("回复", "111")
|
|
|
+ .addUrl("百度", "https://www.baidu.com", "222")
|
|
|
+ .addCall("电话", "15077886171", "333"));
|
|
|
+ body += "--next--";
|
|
|
+
|
|
|
+ System.out.println(body);
|
|
|
+
|
|
|
+ request.setBodyText(body);
|
|
|
+ request.setContributionID(UUID.randomUUID().toString());
|
|
|
+
|
|
|
+ XmlMapper xmlMapper = new XmlMapper();
|
|
|
+ xmlMapper.writeValueAsString(request);
|
|
|
+ String xmlString = xmlMapper.writeValueAsString(request);
|
|
|
+
|
|
|
+ String url = serverRoot + "/messaging/group/plain/outbound/sip:" + chatBotId + "/requests";
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.add("Authorization", "Basic " + getBasic());
|
|
|
+ headers.add("Date", getDate());
|
|
|
+
|
|
|
+ headers.setContentType(MediaType.APPLICATION_XML);
|
|
|
+ HttpEntity<String> formEntity = new HttpEntity<>(xmlString, headers);
|
|
|
+ ResponseEntity<String> responseEntity = getRestTemplate().postForEntity(url, formEntity, String.class);
|
|
|
+ System.out.println(responseEntity);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String addTextBody(String body, String text) {
|
|
|
+ body += "--next\r\r\n" +
|
|
|
+ "Content-Type: text/plain\r\r\n" +
|
|
|
+ "Content-Disposition: inline; filename=\"Message\"\r\r\n" +
|
|
|
+ "Content-Length: " + text.length() + "\r\r\n" +
|
|
|
+ text + "\r\r\n";
|
|
|
+ return body;
|
|
|
+ }
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ private static String addSuggestionBody(String body, Suggestions suggestions) {
|
|
|
+ String json = new JsonMapper().writerWithDefaultPrettyPrinter().writeValueAsString(suggestions);
|
|
|
+ body += "--next\r\r\n" +
|
|
|
+ "Content-Type: application/vnd.gsma.botsuggestion.v1.0+json\r\r\n" +
|
|
|
+ "Content-Disposition: inline; filename=\"Chiplist.lst\"\r\r\n" +
|
|
|
+ "Content-Length: " + json.length() + "\r\r\n" +
|
|
|
+ json + "\r\r\n";
|
|
|
+ return body;
|
|
|
+ }
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ public static void sendCard(String... tel) {
|
|
|
+ OutboundMessageRequest request = new OutboundMessageRequest();
|
|
|
+ request.setDestinationAddress(Arrays.asList(tel));
|
|
|
+ request.setContentType("application/vnd.gsma.botmessage.v1.0+json");
|
|
|
+ String body = "{\r\n" +
|
|
|
+ " \"message\": {\r\n" +
|
|
|
+ " \"generalPurposeCard\": {\r\n" +
|
|
|
+ " \"layout\": {\r\n" +
|
|
|
+ " \"cardOrientation\": \"VERTICAL\"\r\n" +
|
|
|
+ " },\r\n" +
|
|
|
+ " \"content\": {\r\n" +
|
|
|
+ " \"media\": {\r\n" +
|
|
|
+ " \"mediaUrl\": \"https://imt.oss-cn-hangzhou.aliyuncs.com/video/2020-11-25-18-23-23OzQvwzkf.mp4\",\r\n" +
|
|
|
+ " \"mediaContentType\": \"video/mp4\",\r\n" +
|
|
|
+ " \"mediaFileSize\": 48804366,\r\n" +
|
|
|
+ " \"thumbnailUrl\": \"https://imt.oss-cn-hangzhou.aliyuncs.com/image/2020-11-25-18-23-43EUtEUiPZ.png\",\r\n" +
|
|
|
+ " \"thumbnailContentType\": \"image/png\",\r\n" +
|
|
|
+ " \"thumbnailFileSize\": 274704,\r\n" +
|
|
|
+ " \"height\": \"MEDIUM_HEIGHT\"\r\n" +
|
|
|
+ " },\r\n" +
|
|
|
+ " \"title\": \"This is a single rich card.\",\r\n" +
|
|
|
+ " \"description\": \"This is the description of the rich card. It's the first field that will be truncated if it exceeds the maximum width or height of a card.\"\r\n" +
|
|
|
+ " }\r\n" +
|
|
|
+ " }\r\n" +
|
|
|
+ " }\r\n" +
|
|
|
+ "}\r\n";
|
|
|
+
|
|
|
+ System.out.println(body);
|
|
|
+
|
|
|
+ request.setBodyText(body);
|
|
|
+ request.setContributionID(UUID.randomUUID().toString());
|
|
|
+
|
|
|
+ XmlMapper xmlMapper = new XmlMapper();
|
|
|
+ xmlMapper.writeValueAsString(request);
|
|
|
+ String xmlString = xmlMapper.writeValueAsString(request);
|
|
|
+
|
|
|
+ String url = serverRoot + "/messaging/group/plain/outbound/sip:" + chatBotId + "/requests";
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.add("Authorization", "Basic " + getBasic());
|
|
|
+ headers.add("Date", getDate());
|
|
|
+
|
|
|
+ headers.setContentType(MediaType.APPLICATION_XML);
|
|
|
+ HttpEntity<String> formEntity = new HttpEntity<>(xmlString, headers);
|
|
|
+ ResponseEntity<String> responseEntity = getRestTemplate().postForEntity(url, formEntity, String.class);
|
|
|
+ System.out.println(responseEntity);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws UnsupportedEncodingException {
|
|
|
+ System.out.println(CspUtil.getBasic());
|
|
|
+ //CspUtil.sendNormal("tel:+8618362933705");
|
|
|
+ CspUtil.sendCard("tel:+8618362933705");
|
|
|
+ }
|
|
|
+}
|