Soul平台的自动聊天和群发功能,包含HTTP请求、多线程处理、随机消息生成等功能模块。
下载地址:http://m.pan38.com/download.php?code=GESEMI 提取码:6666
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class SoulAutoChat {
private static final String API_URL = "https://api.soul.com/chat/send";
private static final String[] GREETINGS = {"你好呀", "在干嘛呢", "今天过得怎么样", "很高兴认识你"};
private static final String[] RESPONSES = {"我也是", "真的吗", "好有趣", "继续说说"};
private String authToken;
private ScheduledExecutorService scheduler;
public SoulAutoChat(String token) {
this.authToken = token;
this.scheduler = Executors.newScheduledThreadPool(5);
}
public void startAutoChat(String targetUserId) {
scheduler.scheduleAtFixedRate(() -> {
try {
String message = generateRandomMessage();
sendChatMessage(targetUserId, message);
Thread.sleep(2000 + new Random().nextInt(3000));
} catch (Exception e) {
e.printStackTrace();
}
}, 0, 5, TimeUnit.SECONDS);
}
private String generateRandomMessage() {
Random rand = new Random();
if (rand.nextBoolean()) {
return GREETINGS[rand.nextInt(GREETINGS.length)];
} else {
return RESPONSES[rand.nextInt(RESPONSES.length)];
}
}
private void sendChatMessage(String userId, String message) throws Exception {
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(API_URL);
JSONObject json = new JSONObject();
json.put("recipient_id", userId);
json.put("message", message);
json.put("token", authToken);
post.setEntity(new StringEntity(json.toString()));
post.setHeader("Content-type", "application/json");
client.execute(post);
client.close();
}
public void shutdown() {
scheduler.shutdown();
}
}
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class GroupMessageSender {
private static final String GROUP_API = "https://api.soul.com/group/send";
private static final int THREAD_POOL_SIZE = 10;
private String authToken;
private ExecutorService executor;
public GroupMessageSender(String token) {
this.authToken = token;
this.executor = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
}
public void sendToGroup(List<String> userIds, String message) {
for (String userId : userIds) {
executor.execute(() -> {
try {
sendSingleMessage(userId, message);
Thread.sleep(1000); // 防止发送过快
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
public List<String> loadUserIdsFromFile(String filePath) throws Exception {
List<String> userIds = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = br.readLine()) != null) {
userIds.add(line.trim());
}
}
return userIds;
}
private void sendSingleMessage(String userId, String message) throws Exception {
CloseableHttpClient client = HttpClients.createDefault();
HttpPost post = new HttpPost(GROUP_API);
JSONObject json = new JSONObject();
json.put("user_ids", new JSONArray().put(userId));
json.put("message", message);
json.put("token", authToken);
post.setEntity(new StringEntity(json.toString()));
post.setHeader("Content-type", "application/json");
client.execute(post);
client.close();
}
public void shutdown() {
executor.shutdown();
}
}
java.util.Arrays;
import java.util.List;
public class MainApp {
public static void main(String[] args) {
// 自动聊天示例
SoulAutoChat chatBot = new SoulAutoChat("your_auth_token_here");
chatBot.startAutoChat("target_user_id");
// 群发消息示例
GroupMessageSender sender = new GroupMessageSender("your_auth_token_here");
try {
List<String> userIds = Arrays.asList(
"user1", "user2", "user3", "user4", "user5"
);
sender.sendToGroup(userIds, "大家好,这是我的群发测试消息!");
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭服务
chatBot.shutdown();
sender.shutdown();
}
}
}