nano-pro/webhook_logger.php
DJP 0621cf10ad 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>
2025-12-16 11:55:23 -05:00

140 lines
4.4 KiB
PHP

<?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);
}