package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func main() {
// Configure the client
apiKey := "your-api-key" // Replace with your API key
baseURL := "https://api.moderationapi.com/v1"
// Prepare request body
requestBody, err := json.Marshal(map[string]interface{}{
"content": map[string]interface{}{
"type": "text",
"text": "Hello world!",
},
"authorId": "user-123",
"conversationId": "room-456",
"metadata": map[string]interface{}{
"customField": "value",
},
})
if err != nil {
panic(err)
}
// Create request
req, err := http.NewRequest("POST", baseURL+"/moderate", bytes.NewBuffer(requestBody))
if err != nil {
panic(err)
}
// Set headers
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
// Send request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// Read response
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
// Parse response
var textAnalysis map[string]interface{}
if err := json.Unmarshal(body, &textAnalysis); err != nil {
panic(err)
}
evaluation := textAnalysis["evaluation"].(map[string]interface{})
if evaluation["flagged"].(bool) {
fmt.Println("Text content flagged")
// Block the content, show an error, etc...
} else {
fmt.Println("Text content is safe.")
// Save to database or proceed...
}
}