Add webhook logging for all user actions
Implemented complete webhook integration following the pattern from AI-ASSISTANT app. All user actions now logged to Make.com. NEW FILE: - webhook_logger.php - Central webhook utility WEBHOOK FUNCTIONS: - sendToWebhook($data) - Core webhook sender - logImageGeneration() - Log image creation/edits - logPromptEnhancement() - Log prompt enhancements - logUserAction() - Log general actions WEBHOOK URL: https://hook.us1.make.celonis.com/sbhcpk0athbdbxxmgijxc5sbwtjsg33h DATA SENT: Image Generation: - client: 'Internal' - deliverableNumber: 'NANO-{timestamp}' - userEmail: from auth or anonymous - generationType: 'Nano Banana Pro - Imagen 3' - actionType: 'generate' or 'edit' - prompt: user's prompt - settings: {aspectRatio, imageSize, model} - imageFile: 'data:image/png;base64,{image}' Prompt Enhancement: - generationType: 'Nano Banana Pro - Prompt Enhancement' - actionType: 'prompt_enhancement' - originalPrompt: scene description - enhancedPrompt: AI-enhanced result - settings: {camera, lens, application, creativeFreedom} INTEGRATION POINTS: - api.php - Logs every image generation/edit - enhance_prompt.php - Logs every prompt enhancement - Auth status included (user email) ERROR HANDLING: - Webhook failures don't break the app - Errors logged to error_log - 10 second timeout on webhook calls - Graceful degradation All user actions now tracked in Make.com! 📊 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
747005733c
commit
0621cf10ad
3 changed files with 196 additions and 1 deletions
29
api.php
29
api.php
|
|
@ -1,13 +1,23 @@
|
|||
<?php
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// Load configuration and session manager
|
||||
// Load configuration, session manager, and webhook logger
|
||||
require_once 'config.php';
|
||||
require_once 'session_manager.php';
|
||||
require_once 'webhook_logger.php';
|
||||
|
||||
// Initialize session manager for multi-user support
|
||||
$sessionManager = new SessionManager();
|
||||
|
||||
// Initialize auth status with default
|
||||
$authStatus = [
|
||||
'authenticated' => true,
|
||||
'user' => [
|
||||
'name' => 'User',
|
||||
'preferred_username' => 'anonymous@nano-banana-pro.com'
|
||||
]
|
||||
];
|
||||
|
||||
// Check authentication (with graceful fallback)
|
||||
try {
|
||||
if (file_exists(__DIR__ . '/AuthMiddleware.php')) {
|
||||
|
|
@ -289,6 +299,23 @@ try {
|
|||
// Add to conversation history
|
||||
$sessionManager->addToHistory($prompt, $inputImage ? 'edit' : 'generate');
|
||||
|
||||
// Log to webhook
|
||||
try {
|
||||
$userEmail = $authStatus['user']['preferred_username'] ?? $authStatus['user']['email'] ?? 'anonymous@nano-banana-pro.com';
|
||||
$webhookSettings = [
|
||||
'prompt' => $prompt,
|
||||
'aspectRatio' => $aspectRatio,
|
||||
'imageSize' => $imageSize,
|
||||
'actionType' => $inputImage ? 'edit' : 'generate',
|
||||
'model' => 'Google Imagen 3'
|
||||
];
|
||||
|
||||
logImageGeneration($prompt, $imageData['base64'], $imageData['mime_type'], $webhookSettings, $userEmail, $inputImage ? 'edit' : 'generate');
|
||||
} catch (Exception $e) {
|
||||
// Don't fail if webhook fails
|
||||
error_log("Webhook logging failed: " . $e->getMessage());
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'message' => 'Image generated successfully'
|
||||
|
|
|
|||
|
|
@ -5,9 +5,19 @@
|
|||
*/
|
||||
|
||||
require_once 'config.php';
|
||||
require_once 'webhook_logger.php';
|
||||
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// Initialize auth status with default
|
||||
$authStatus = [
|
||||
'authenticated' => true,
|
||||
'user' => [
|
||||
'name' => 'User',
|
||||
'preferred_username' => 'anonymous@nano-banana-pro.com'
|
||||
]
|
||||
];
|
||||
|
||||
// Check authentication (with graceful fallback)
|
||||
try {
|
||||
if (file_exists(__DIR__ . '/AuthMiddleware.php')) {
|
||||
|
|
@ -222,6 +232,24 @@ try {
|
|||
|
||||
$enhancedPrompt = trim($result['candidates'][0]['content']['parts'][0]['text']);
|
||||
|
||||
// Log to webhook
|
||||
try {
|
||||
$userEmail = $authStatus['user']['preferred_username'] ?? $authStatus['user']['email'] ?? 'anonymous@nano-banana-pro.com';
|
||||
$webhookSettings = [
|
||||
'sceneDescription' => $sceneDescription,
|
||||
'camera' => $camera,
|
||||
'lens' => $lens,
|
||||
'application' => $application,
|
||||
'aspectRatio' => $aspectRatio,
|
||||
'creativeFreedom' => $creativeFreedom
|
||||
];
|
||||
|
||||
logPromptEnhancement($sceneDescription, $enhancedPrompt, $webhookSettings, $userEmail);
|
||||
} catch (Exception $e) {
|
||||
// Don't fail if webhook fails
|
||||
error_log("Webhook logging failed in enhance_prompt: " . $e->getMessage());
|
||||
}
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'enhancedPrompt' => $enhancedPrompt
|
||||
|
|
|
|||
140
webhook_logger.php
Normal file
140
webhook_logger.php
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
<?php
|
||||
/**
|
||||
* Webhook Logger for Nano Banana Pro
|
||||
* Sends user actions and generated images to Make.com webhook
|
||||
*/
|
||||
|
||||
/**
|
||||
* Send action data to webhook
|
||||
*
|
||||
* @param array $data Data to send to webhook
|
||||
* @return array ['success' => bool, 'http_code' => int, 'response' => string]
|
||||
*/
|
||||
function sendToWebhook($data) {
|
||||
// Webhook URL (Make.com integration)
|
||||
$webhookUrl = 'https://hook.us1.make.celonis.com/sbhcpk0athbdbxxmgijxc5sbwtjsg33h';
|
||||
|
||||
// Add timestamp
|
||||
$data['timestamp'] = date('Y-m-d H:i:s');
|
||||
$data['timezone'] = date_default_timezone_get();
|
||||
|
||||
// Log webhook send attempt
|
||||
error_log("Sending to webhook: " . json_encode(array_keys($data)));
|
||||
|
||||
try {
|
||||
$ch = curl_init($webhookUrl);
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||
'Content-Type: application/json'
|
||||
]);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 10 second timeout
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
|
||||
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$curlError = curl_error($ch);
|
||||
curl_close($ch);
|
||||
|
||||
if ($curlError) {
|
||||
error_log("Webhook cURL Error: " . $curlError);
|
||||
return [
|
||||
'success' => false,
|
||||
'http_code' => 0,
|
||||
'error' => $curlError
|
||||
];
|
||||
}
|
||||
|
||||
if ($httpCode === 200 || $httpCode === 201 || $httpCode === 202) {
|
||||
error_log("Webhook sent successfully (HTTP $httpCode)");
|
||||
return [
|
||||
'success' => true,
|
||||
'http_code' => $httpCode,
|
||||
'response' => $response
|
||||
];
|
||||
} else {
|
||||
error_log("Webhook failed (HTTP $httpCode): " . $response);
|
||||
return [
|
||||
'success' => false,
|
||||
'http_code' => $httpCode,
|
||||
'response' => $response
|
||||
];
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log("Webhook exception: " . $e->getMessage());
|
||||
return [
|
||||
'success' => false,
|
||||
'error' => $e->getMessage()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Log image generation to webhook
|
||||
*
|
||||
* @param string $prompt The prompt used
|
||||
* @param string $imageBase64 Base64 encoded image
|
||||
* @param string $mimeType Image MIME type
|
||||
* @param array $settings Generation settings
|
||||
* @param string $userEmail User email
|
||||
* @param string $actionType Type of action (generate, edit, upload)
|
||||
*/
|
||||
function logImageGeneration($prompt, $imageBase64, $mimeType, $settings, $userEmail, $actionType = 'generate') {
|
||||
$webhookData = [
|
||||
'client' => 'Internal', // Hardcoded for Nano Banana Pro
|
||||
'deliverableNumber' => 'NANO-' . time(), // Auto-generated
|
||||
'userEmail' => $userEmail,
|
||||
'generationType' => 'Nano Banana Pro - Imagen 3',
|
||||
'actionType' => $actionType, // generate, edit, upload
|
||||
'prompt' => $prompt,
|
||||
'settings' => $settings,
|
||||
'imageFile' => 'data:' . $mimeType . ';base64,' . $imageBase64
|
||||
];
|
||||
|
||||
return sendToWebhook($webhookData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log prompt enhancement to webhook
|
||||
*
|
||||
* @param string $originalPrompt Original scene description
|
||||
* @param string $enhancedPrompt AI-enhanced prompt
|
||||
* @param array $settings Studio settings (camera, lens, application, etc.)
|
||||
* @param string $userEmail User email
|
||||
*/
|
||||
function logPromptEnhancement($originalPrompt, $enhancedPrompt, $settings, $userEmail) {
|
||||
$webhookData = [
|
||||
'client' => 'Internal',
|
||||
'deliverableNumber' => 'NANO-PROMPT-' . time(),
|
||||
'userEmail' => $userEmail,
|
||||
'generationType' => 'Nano Banana Pro - Prompt Enhancement',
|
||||
'actionType' => 'prompt_enhancement',
|
||||
'originalPrompt' => $originalPrompt,
|
||||
'enhancedPrompt' => $enhancedPrompt,
|
||||
'settings' => $settings
|
||||
];
|
||||
|
||||
return sendToWebhook($webhookData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log user action to webhook (general purpose)
|
||||
*
|
||||
* @param string $action Action name
|
||||
* @param array $data Action data
|
||||
* @param string $userEmail User email
|
||||
*/
|
||||
function logUserAction($action, $data, $userEmail) {
|
||||
$webhookData = [
|
||||
'client' => 'Internal',
|
||||
'deliverableNumber' => 'NANO-ACTION-' . time(),
|
||||
'userEmail' => $userEmail,
|
||||
'generationType' => 'Nano Banana Pro - User Action',
|
||||
'action' => $action,
|
||||
'data' => $data
|
||||
];
|
||||
|
||||
return sendToWebhook($webhookData);
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue