This commit is contained in:
2025-01-05 16:17:52 +08:00
parent 178ad6a65e
commit 10afdb3280
11 changed files with 249 additions and 2 deletions

View File

@@ -0,0 +1,76 @@
package com.ycwl.basic.notify.adapters;
import com.ycwl.basic.notify.entity.NotifyContent;
import com.ycwl.basic.notify.entity.ServerChanConfig;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ServerChanNotifyAdapter implements INotifyAdapter {
ServerChanConfig config;
@Override
public void loadConfig(Map<String, String> _config) {
ServerChanConfig config = new ServerChanConfig();
config.setKey(_config.get("key"));
config.checkEverythingOK();
this.config = config;
}
@Override
public void send(NotifyContent notifyContent) {
scSend(notifyContent.getTitle(), notifyContent.getContent(), config.getKey());
}
public static String scSend(String title, String content, String key) {
try {
String api;
// 判断 sendkey 是否以 "sctp" 开头,并提取数字部分拼接 URL
if (key.startsWith("sctp")) {
Pattern pattern = Pattern.compile("sctp(\\d+)t");
Matcher matcher = pattern.matcher(key);
if (matcher.find()) {
String num = matcher.group(1);
api = "https://" + num + ".push.ft07.com/send/" + key +".send";
} else {
throw new IllegalArgumentException("Invalid sendkey format for sctp");
}
} else {
api = "https://sctapi.ftqq.com/" + key + ".send";
}
String body = "text=" + URLEncoder.encode(title, "UTF-8") + "&desp=" + URLEncoder.encode(content, "UTF-8");
URL apiUrl = new URL(api);
HttpURLConnection httpConnection = (HttpURLConnection) apiUrl.openConnection();
httpConnection.setRequestMethod("POST");
httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpConnection.setDoOutput(true);
DataOutputStream bodyWriter = new DataOutputStream(httpConnection.getOutputStream());
bodyWriter.writeBytes(body);
bodyWriter.flush();
bodyWriter.close();
int responseCode = httpConnection.getResponseCode();
BufferedReader reader = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
StringBuilder responseText = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
responseText.append(line);
}
reader.close();
return responseText.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}