-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
104 lines (88 loc) · 3.69 KB
/
index.php
File metadata and controls
104 lines (88 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* Moderyo PHP Backend Example — Slim Framework
*/
require __DIR__ . '/vendor/autoload.php';
use Moderyo\ModeryoClient;
use Moderyo\ModeryoConfig;
use Slim\Factory\AppFactory;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
$app = AppFactory::create();
$app->addBodyParsingMiddleware();
$apiKey = getenv('MODERYO_API_KEY') ?: '';
$client = new ModeryoClient(new ModeryoConfig(['apiKey' => $apiKey]));
$app->post('/api/moderate', function (Request $request, Response $response) use ($client) {
$body = $request->getParsedBody();
$input = $body['input'] ?? '';
if (empty($input)) {
$response->getBody()->write(json_encode(['error' => 'input field is required']));
return $response->withStatus(400)->withHeader('Content-Type', 'application/json');
}
try {
$result = $client->moderate($input, [
'mode' => $body['mode'] ?? 'enforce',
'risk' => $body['risk'] ?? 'balanced',
'playerId' => $body['player_id'] ?? '',
'skipProfanity' => $body['skip_profanity'] ?? false,
'skipThreat' => $body['skip_threat'] ?? false,
'skipMaskedWord' => $body['skip_masked_word'] ?? false,
'longTextMode' => $body['long_text_mode'] ?? false,
]);
$data = [
'blocked' => $result->isBlocked,
'flagged' => $result->isFlagged,
'decision' => $result->policyDecision->decision ?? null,
'reason' => $result->policyDecision->reason ?? null,
'scores' => $result->scores ?? null,
];
$response->getBody()->write(json_encode($data));
return $response->withHeader('Content-Type', 'application/json');
} catch (\Exception $e) {
$response->getBody()->write(json_encode(['error' => $e->getMessage()]));
return $response->withStatus(500)->withHeader('Content-Type', 'application/json');
}
});
$app->post('/api/moderate/batch', function (Request $request, Response $response) use ($client) {
$body = $request->getParsedBody();
$inputs = $body['inputs'] ?? [];
if (empty($inputs) || !is_array($inputs)) {
$response->getBody()->write(json_encode(['error' => 'inputs array is required']));
return $response->withStatus(400)->withHeader('Content-Type', 'application/json');
}
try {
$result = $client->moderateBatch($inputs, [
'mode' => $body['mode'] ?? 'enforce',
'risk' => $body['risk'] ?? 'balanced',
]);
$items = [];
foreach ($result->results as $r) {
$items[] = [
'blocked' => $r->isBlocked,
'flagged' => $r->isFlagged,
'decision' => $r->policyDecision->decision ?? null,
'reason' => $r->policyDecision->reason ?? null,
];
}
$data = [
'total' => count($result->results),
'blockedCount' => count($result->getBlocked()),
'flaggedCount' => count($result->getFlagged()),
'results' => $items,
];
$response->getBody()->write(json_encode($data));
return $response->withHeader('Content-Type', 'application/json');
} catch (\Exception $e) {
$response->getBody()->write(json_encode(['error' => $e->getMessage()]));
return $response->withStatus(500)->withHeader('Content-Type', 'application/json');
}
});
$app->get('/health', function (Request $request, Response $response) {
$response->getBody()->write(json_encode([
'status' => 'ok',
'sdk' => 'moderyo-php',
'version' => '2.0.7',
]));
return $response->withHeader('Content-Type', 'application/json');
});
$app->run();