新版仿ok交易所-后端
1
zyy
2025-09-18 8f6cab15fc562f1a0279f918ed29cf4312f06374
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.yami.trading.common.manager.sms;
 
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;
import org.springframework.stereotype.Component;
 
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
 
@Component
public class GoToneSmsUtils {
    private static final String API_ENDPOINT = "https://app.gotonesms.ink/api/v3/sms/send";
    private static final String apiToken ="68351|IjAi46FybmyAApdg58vweaw4JcePvRpBFeEClLlYa7c00e24";
    private static final String senderId ="SignOTP";
 
 
    public String sendSms(String recipient, String message) throws IOException {
        return sendSms(recipient, senderId, message, null, null);
    }
 
    public String sendSms(String recipient, String senderId, String message) throws IOException {
        return sendSms(recipient, senderId, message, null, null);
    }
 
    public String sendSms(String recipient, String senderId, String message,
                          LocalDateTime scheduleTime, String dltTemplateId) throws IOException {
        // 构建JSON请求体
        StringBuilder jsonBody = new StringBuilder();
        jsonBody.append("{")
                .append("\"recipient\": \"").append(escapeJson(recipient)).append("\",")
                .append("\"sender_id\": \"").append(escapeJson(senderId)).append("\",")
                .append("\"type\": \"plain\",")
                .append("\"message\": \"").append(escapeJson(message)).append("\"");
 
        if (scheduleTime != null) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
            jsonBody.append(",\"schedule_time\": \"").append(scheduleTime.format(formatter)).append("\"");
        }
 
        if (dltTemplateId != null && !dltTemplateId.isEmpty()) {
            jsonBody.append(",\"dlt_template_id\": \"").append(escapeJson(dltTemplateId)).append("\"");
        }
 
        jsonBody.append("}");
 
        // 发送请求并获取响应
        HttpResponse response = Request.Post(API_ENDPOINT)
                .addHeader("Authorization", "Bearer " + apiToken)
                .addHeader("Content-Type", "application/json")
                .addHeader("Accept", "application/json")
                .bodyString(jsonBody.toString(), ContentType.APPLICATION_JSON)
                .execute()
                .returnResponse();
 
        // 读取响应内容
        return org.apache.http.util.EntityUtils.toString(response.getEntity());
    }
 
    private String escapeJson(String value) {
        if (value == null) {
            return "";
        }
        return value.replace("\\", "\\\\")
                .replace("\"", "\\\"")
                .replace("\b", "\\b")
                .replace("\f", "\\f")
                .replace("\n", "\\n")
                .replace("\r", "\\r")
                .replace("\t", "\\t");
    }
 
    public static void main(String[] args) {
        GoToneSmsUtils sender = new GoToneSmsUtils();
 
        try {
            String response = sender.sendSms(
                    "8617769309156",
                    "SignOTP",
                    "Your verification code is 123456."
            );
 
            // 输出响应结果
            System.out.println("发送结果: " + response);
            String responseWithChinese = StringEscapeUtils.unescapeJava(response);
            System.out.println("中文发送结果: " + responseWithChinese);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}