import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import org.json.JSONObject;
public class TextModeration {
public static void main(String[] args) {
String API_KEY = "your-api-key"; // Replace with your API key
String BASE_URL = "https://api.moderationapi.com/v1";
try {
HttpClient client = HttpClient.newHttpClient();
// Create request body
JSONObject content = new JSONObject()
.put("type", "text")
.put("text", "Hello world!");
JSONObject requestBody = new JSONObject()
.put("content", content)
.put("authorId", "123")
.put("conversationId", "456")
.put("metadata", new JSONObject()
.put("customField", "value")
);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(BASE_URL + "/moderate"))
.header("Authorization", "Bearer " + API_KEY)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(requestBody.toString()))
.build();
HttpResponse<String> response = client.send(request,
HttpResponse.BodyHandlers.ofString());
JSONObject textAnalysis = new JSONObject(response.body());
if (textAnalysis.getBoolean("flagged")) {
System.out.println("Text content flagged");
// Block the content, show an error, etc...
} else {
System.out.println("Text content is safe.");
// Save to database or proceed...
}
} catch (Exception e) {
e.printStackTrace();
}
}
}