feat(chat-turnero): backend APIs for media upload, reactions, and template messages
- chat_upload_media.php: multipart upload to WhatsApp Media API, sends image/video/audio/document - chat_react.php: sends emoji reaction to a message via WhatsApp, updates reaction_emoji in DB - chat_send_message.php: extended to support type=template with lang+params Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
a7ab91615f
commit
ec614c2a89
@@ -1,61 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* API - Enviar mensaje desde el número turnero
|
||||
* Soporta: text, template (básico)
|
||||
* Soporta: text, template
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/../../../config/config.php';
|
||||
require_once __DIR__ . '/../../../services/WhatsAppService.php';
|
||||
requireAuthentication();
|
||||
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
http_response_code(405);
|
||||
echo json_encode(['success' => false, 'error' => 'Método no permitido']);
|
||||
exit;
|
||||
}
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') { http_response_code(405); echo json_encode(['success'=>false,'error'=>'Método no permitido']); exit; }
|
||||
|
||||
try {
|
||||
$input = json_decode(file_get_contents('php://input'), true);
|
||||
if (!$input) {
|
||||
$input = $_POST;
|
||||
}
|
||||
$input = json_decode(file_get_contents('php://input'), true) ?: $_POST;
|
||||
|
||||
$userId = intval($input['user_id'] ?? 0);
|
||||
$message = trim($input['message'] ?? '');
|
||||
$userId = intval($input['user_id'] ?? 0);
|
||||
$type = $input['type'] ?? 'text';
|
||||
$message = trim($input['message'] ?? '');
|
||||
$template = trim($input['template'] ?? '');
|
||||
$lang = trim($input['lang'] ?? 'es_CO');
|
||||
$params = $input['params'] ?? [];
|
||||
|
||||
if (!$userId || $message === '') {
|
||||
http_response_code(400);
|
||||
echo json_encode(['success' => false, 'error' => 'user_id y message son requeridos']);
|
||||
exit;
|
||||
if (!$userId || ($type === 'text' && $message === '') || ($type === 'template' && $template === '')) {
|
||||
http_response_code(400); echo json_encode(['success'=>false,'error'=>'Parámetros insuficientes']); exit;
|
||||
}
|
||||
|
||||
$db = Database::getInstance();
|
||||
$user = $db->fetch("SELECT phone_number, name FROM users WHERE id = :id", ['id' => $userId]);
|
||||
if (!$user) { http_response_code(404); echo json_encode(['success'=>false,'error'=>'Usuario no encontrado']); exit; }
|
||||
|
||||
if (!$user) {
|
||||
http_response_code(404);
|
||||
echo json_encode(['success' => false, 'error' => 'Usuario no encontrado']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Verificar que el número turnero esté configurado
|
||||
$turneroPhoneId = getConfigFromDB('whatsapp_phone_number_id_turnero', '');
|
||||
if (empty($turneroPhoneId)) {
|
||||
http_response_code(503);
|
||||
echo json_encode(['success' => false, 'error' => 'El número de WhatsApp del turnero no está configurado']);
|
||||
exit;
|
||||
}
|
||||
if (empty($turneroPhoneId)) { http_response_code(503); echo json_encode(['success'=>false,'error'=>'Número turnero no configurado']); exit; }
|
||||
|
||||
$operatorId = $_SESSION['admin_id'] ?? $_SESSION['user_id'] ?? null;
|
||||
$wa = new WhatsAppService('turnero');
|
||||
|
||||
$wa = new WhatsAppService('turnero');
|
||||
$response = $wa->sendTextMessage(
|
||||
$user['phone_number'],
|
||||
$message,
|
||||
$operatorId ? ['operator_id' => $operatorId, 'canal' => 'turnero'] : ['canal' => 'turnero']
|
||||
);
|
||||
if ($type === 'template') {
|
||||
$response = $wa->sendTemplateMessage($user['phone_number'], $template, $lang, $params);
|
||||
} else {
|
||||
$extra = ['canal' => 'turnero'];
|
||||
if ($operatorId) $extra['operator_id'] = $operatorId;
|
||||
$response = $wa->sendTextMessage($user['phone_number'], $message, $extra);
|
||||
}
|
||||
|
||||
echo json_encode(['success' => true, 'whatsapp_response' => $response]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user