using System;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
public class ModerationClient
{
private readonly HttpClient _httpClient;
private const string BaseUrl = "https://api.moderationapi.com/v1";
public ModerationClient(string apiKey)
{
_httpClient = new HttpClient();
_httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
}
public async Task<TextModerationResult> ModerateTextAsync(string text, string authorId = "123", string conversationId = "456")
{
var payload = new
{
content = new
{
type = "text",
text = text
},
authorId = authorId,
conversationId = conversationId,
metadata = new { customField = "value" }
};
var json = JsonSerializer.Serialize(payload);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync($"{BaseUrl}/moderate", content);
response.EnsureSuccessStatusCode();
var responseContent = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<TextModerationResult>(responseContent);
}
}
public class TextModerationResult
{
public bool flagged { get; set; }
public object[] categories { get; set; }
}
// Usage example
class Program
{
static async Task Main(string[] args)
{
var client = new ModerationClient("your-api-key"); // Replace with your API key
var result = await client.ModerateTextAsync("Hello world!");
if (result.flagged)
{
Console.WriteLine("Text content flagged");
// Block the content, show an error, etc...
}
else
{
Console.WriteLine("Text content is safe.");
// Save to database or proceed...
}
}
}