51 lines
1.5 KiB
PHP
51 lines
1.5 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once __DIR__ . '/../db/config.php';
|
|
require_once __DIR__ . '/../ai/LocalAIApi.php';
|
|
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!$data || empty($data['comment']) || empty($data['username'])) {
|
|
echo json_encode(['error' => 'Missing required data']);
|
|
exit;
|
|
}
|
|
|
|
$username = $data['username'];
|
|
$comment_author = $data['author'] ?? 'Anonymous';
|
|
$comment_text = $data['comment'];
|
|
|
|
// Prompt for AI
|
|
$prompt = "You are a helpful and funny AI assistant for a TikTok live stream.
|
|
The streamer is '$username'.
|
|
The user '$comment_author' just said: '$comment_text'.
|
|
Respond to them in a short, engaging, and friendly way (max 20 words).
|
|
Keep it suitable for a live stream audience.";
|
|
|
|
$resp = LocalAIApi::createResponse([
|
|
'input' => [
|
|
['role' => 'system', 'content' => 'TikTok Live AI Assistant'],
|
|
['role' => 'user', 'content' => $prompt],
|
|
],
|
|
]);
|
|
|
|
$ai_reply = '';
|
|
if (!empty($resp['success'])) {
|
|
$ai_reply = LocalAIApi::extractText($resp);
|
|
} else {
|
|
$ai_reply = "Thanks for your comment, $comment_author!";
|
|
}
|
|
|
|
// Persist to DB
|
|
try {
|
|
$stmt = db()->prepare("INSERT INTO tiktok_history (username, comment_author, comment_text, ai_reply) VALUES (?, ?, ?, ?)");
|
|
$stmt->execute([$username, $comment_author, $comment_text, $ai_reply]);
|
|
} catch (Exception $e) {
|
|
// Log error but continue
|
|
}
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'reply' => $ai_reply,
|
|
'author' => $comment_author,
|
|
'comment' => $comment_text
|
|
]); |