102 lines
4.0 KiB
PHP
102 lines
4.0 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../config/config.php';
|
|
require_once __DIR__ . '/../services/WhatsAppService.php';
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
if (function_exists('requireAuthentication')) requireAuthentication();
|
|
|
|
$input = json_decode(file_get_contents('php://input'), true) ?: $_POST;
|
|
$message_id = $input['message_id'] ?? null;
|
|
$emoji = $input['emoji'] ?? null;
|
|
|
|
// Normalize emoji aliases and decode any HTML entities
|
|
function normalize_emoji($s) {
|
|
if (!$s) return $s;
|
|
$orig = $s;
|
|
$s = html_entity_decode($s, ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
|
$s_trim = trim(mb_strtolower($s, 'UTF-8'));
|
|
$map = [
|
|
'corazon' => '❤️', 'heart' => '❤️', '<3' => '❤️',
|
|
'like' => '👍', 'thumbsup' => '👍', 'thumbs_up' => '👍',
|
|
'laugh' => '😂', 'risa' => '😂', 'joy' => '😂',
|
|
'wow' => '😮', 'surprised' => '😮',
|
|
'sad' => '😢', 'triste' => '😢',
|
|
'clap' => '👏', 'applause' => '👏',
|
|
'party' => '🎉', 'celebrate' => '🎉',
|
|
'fire' => '🔥', 'pray' => '🙏'
|
|
];
|
|
|
|
$key = preg_replace('/[^a-z0-9_\-]/u', '', $s_trim);
|
|
if (isset($map[$key])) return $map[$key];
|
|
|
|
// If it already contains an emoji, extract the first emoji char
|
|
if (preg_match('/[\x{1F300}-\x{1F6FF}\x{1F900}-\x{1F9FF}\x{2600}-\x{26FF}\x{2700}-\x{27BF}]/u', $s, $m)) {
|
|
return $m[0];
|
|
}
|
|
|
|
return trim($orig);
|
|
}
|
|
|
|
$rawEmoji = $input['emoji'] ?? null;
|
|
$emoji = normalize_emoji($emoji);
|
|
error_log('react_message: received raw=' . ($rawEmoji ?? 'NULL') . ' normalized=' . ($emoji ?? 'NULL') . ' message_id=' . ($message_id ?? 'NULL'));
|
|
|
|
if (!$message_id || !$emoji) {
|
|
http_response_code(400);
|
|
echo json_encode(['success' => false, 'error' => 'Parámetros faltantes: message_id, emoji (después de normalizar)']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$db = Database::getInstance();
|
|
|
|
// Buscar mensaje objetivo para obtener user_id
|
|
$stmt = $db->query("SELECT id, user_id, message_id, direction FROM conversations WHERE message_id = ? OR id = ? LIMIT 1", [$message_id, $message_id]);
|
|
$target = $stmt->fetch();
|
|
if (!$target) {
|
|
http_response_code(404);
|
|
echo json_encode(['success' => false, 'error' => 'Mensaje objetivo no encontrado']);
|
|
exit;
|
|
}
|
|
|
|
// Obtener teléfono del usuario
|
|
$user = $db->query("SELECT phone_number FROM users WHERE id = ? LIMIT 1", [$target['user_id']])->fetch();
|
|
if (!$user || empty($user['phone_number'])) {
|
|
http_response_code(500);
|
|
echo json_encode(['success' => false, 'error' => 'No se pudo determinar el teléfono del usuario']);
|
|
exit;
|
|
}
|
|
|
|
// Enviar reacción a través de WhatsAppService
|
|
$wa = new WhatsAppService();
|
|
$to = $user['phone_number'];
|
|
$resp = null;
|
|
try {
|
|
$resp = $wa->sendReaction($to, $message_id, $emoji, false);
|
|
} catch (Exception $e) {
|
|
// fallar la operación de envío pero seguir guardando registro con status "failed"
|
|
error_log('sendReaction failed: ' . $e->getMessage());
|
|
}
|
|
|
|
// Registrar la reacción en el mensaje objetivo (no crear un nuevo "reaction" como mensaje saliente)
|
|
try {
|
|
$update = ['reaction_emoji' => $emoji, 'reaction_to_message_id' => $message_id, 'updated_at' => date('Y-m-d H:i:s')];
|
|
// If send failed, optionally record a status on target (not mandatory)
|
|
if (!$resp) $update['reaction_status'] = 'failed';
|
|
|
|
$db->update('conversations', $update, 'id = :id', ['id' => $target['id']]);
|
|
|
|
// Return updated row for convenience
|
|
$updated = $db->query('SELECT * FROM conversations WHERE id = ? LIMIT 1', [$target['id']])->fetch();
|
|
|
|
echo json_encode(['success' => true, 'message' => 'Reacción aplicada', 'updated' => $updated]);
|
|
} catch (Exception $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['success' => false, 'error' => 'No se pudo guardar la reacción: ' . $e->getMessage()]);
|
|
}
|
|
} catch (Exception $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['success' => false, 'error' => $e->getMessage()]);
|
|
}
|