This commit is contained in:
lizandrogd
2026-01-21 02:22:29 -05:00
parent 3fffb0d023
commit b1961f0116
35 changed files with 1006 additions and 122 deletions
+36
View File
@@ -0,0 +1,36 @@
<?php
/**
* Migration: añadir columnas reply_to_message_id, reaction_to_message_id, reaction_emoji
*/
require_once __DIR__ . '/../config/config_enhanced.php';
require_once __DIR__ . '/../classes/Database.php';
try {
$db = Database::getInstance();
$columns = $db->fetchAll("SHOW COLUMNS FROM conversations");
$colNames = array_column($columns, 'Field');
if (!in_array('reply_to_message_id', $colNames)) {
$db->query("ALTER TABLE conversations ADD COLUMN reply_to_message_id INT NULL AFTER message_id");
echo "Added column reply_to_message_id\n";
} else {
echo "reply_to_message_id exists\n";
}
if (!in_array('reaction_to_message_id', $colNames)) {
$db->query("ALTER TABLE conversations ADD COLUMN reaction_to_message_id INT NULL AFTER reply_to_message_id");
echo "Added column reaction_to_message_id\n";
} else {
echo "reaction_to_message_id exists\n";
}
if (!in_array('reaction_emoji', $colNames)) {
$db->query("ALTER TABLE conversations ADD COLUMN reaction_emoji VARCHAR(32) NULL AFTER reaction_to_message_id");
echo "Added column reaction_emoji\n";
} else {
echo "reaction_emoji exists\n";
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
+15
View File
@@ -0,0 +1,15 @@
<?php
require_once __DIR__ . '/../config/config.php';
$db = Database::getInstance();
$col = $db->fetch("SHOW COLUMNS FROM conversations LIKE 'is_read'");
if ($col) {
echo "Column is_read already exists\n";
exit(0);
}
try {
$db->query("ALTER TABLE conversations ADD COLUMN is_read TINYINT(1) NULL DEFAULT 0 AFTER status");
echo "Added column is_read\n";
} catch (Exception $e) {
echo "Failed to add is_read: " . $e->getMessage() . "\n";
exit(1);
}
+26
View File
@@ -0,0 +1,26 @@
<?php
require_once __DIR__ . '/../config/config.php';
$db = Database::getInstance();
// Verificar columna message_type
$col = $db->fetch("SHOW COLUMNS FROM conversations LIKE 'message_type'");
if (!$col) {
echo "Column message_type not found\n";
exit(1);
}
$type = $col['Type'];
if (strpos($type, "'reaction'") !== false) {
echo "reaction already present in message_type enum\n";
exit(0);
}
// Construir nuevo enum
$newEnum = "ENUM('text','image','audio','video','document','template','reaction') NOT NULL DEFAULT 'text'";
try {
$db->query("ALTER TABLE conversations MODIFY COLUMN message_type $newEnum");
echo "Added reaction to message_type enum\n";
} catch (Exception $e) {
echo "Failed to alter table: " . $e->getMessage() . "\n";
exit(1);
}
+15
View File
@@ -0,0 +1,15 @@
<?php
require_once __DIR__ . '/../config/config.php';
$db = Database::getInstance();
$col = $db->fetch("SHOW COLUMNS FROM users LIKE 'avatar_url'");
if ($col) {
echo "Column avatar_url already exists\n";
exit(0);
}
try {
$db->query("ALTER TABLE users ADD COLUMN avatar_url VARCHAR(255) NULL AFTER name");
echo "Added column avatar_url\n";
} catch (Exception $e) {
echo "Failed to add avatar_url: " . $e->getMessage() . "\n";
exit(1);
}
+10
View File
@@ -0,0 +1,10 @@
<?php
require_once __DIR__ . '/../config/config.php';
$db = Database::getInstance();
$r = $db->fetch('SELECT * FROM conversations WHERE message_type = :mt ORDER BY id DESC LIMIT 5', ['mt' => 'reaction']);
var_export($r);
echo "\nAll recent rows:\n";
$rows = $db->fetchAll('SELECT id,message_id,message_type,direction,content,reaction_to_message_id,reaction_emoji,reply_to_message_id FROM conversations ORDER BY id DESC LIMIT 10');
foreach ($rows as $row) {
echo json_encode($row) . "\n";
}
+13
View File
@@ -0,0 +1,13 @@
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
require_once __DIR__ . '/../config/config.php';
try {
$db = Database::getInstance();
echo "DB instance ok\n";
} catch (Throwable $t) {
echo "ERROR: " . $t->getMessage() . "\n";
echo $t->getTraceAsString() . "\n";
}
+24
View File
@@ -0,0 +1,24 @@
<?php
require_once __DIR__ . '/../config/config_enhanced.php';
require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/../services/WhatsAppService.php';
require_once __DIR__ . '/../services/BotService.php';
ini_set('display_errors', 1);
error_reporting(E_ALL);
try {
$s = new WhatsAppService();
echo "WhatsAppService OK\n";
} catch (Throwable $t) {
echo "WhatsAppService ERR: " . $t->getMessage() . "\n";
echo $t->getTraceAsString() . "\n";
}
try {
$b = new BotService();
echo "BotService OK\n";
} catch (Throwable $t) {
echo "BotService ERR: " . $t->getMessage() . "\n";
echo $t->getTraceAsString() . "\n";
}
+7
View File
@@ -0,0 +1,7 @@
<?php
require_once __DIR__ . '/../config/config.php';
$db = Database::getInstance();
$cols = $db->fetchAll('DESCRIBE conversations');
foreach ($cols as $c) {
echo $c['Field'] . ' => ' . $c['Type'] . PHP_EOL;
}
+7
View File
@@ -0,0 +1,7 @@
<?php
require_once __DIR__ . '/../config/config.php';
$db = Database::getInstance();
$cols = $db->fetchAll('DESCRIBE users');
foreach ($cols as $c) {
echo $c['Field'] . ' => ' . $c['Type'] . PHP_EOL;
}
+24
View File
@@ -0,0 +1,24 @@
<?php
require_once __DIR__ . '/../config/config_enhanced.php';
require_once __DIR__ . '/../config/config.php';
require_once __DIR__ . '/../api/webhook.php';
// Simular GET verification
action:
$_SERVER['REQUEST_METHOD'] = 'GET';
$_GET['hub_mode'] = 'subscribe';
$_GET['hub_verify_token'] = WEBHOOK_VERIFY_TOKEN;
$_GET['hub_challenge'] = 'testchallenge123';
try {
$w = new WhatsAppWebhook();
ob_start();
$w->handleRequest();
$out = ob_get_clean();
echo "Output:\n" . $out . "\n";
} catch (Throwable $t) {
echo "Throwable: " . $t->getMessage() . "\n";
echo $t->getTraceAsString() . "\n";
}
View File
+121
View File
@@ -0,0 +1,121 @@
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
require_once __DIR__ . '/../api/webhook.php';
$db = Database::getInstance();
$webhook = new WhatsAppWebhook();
// Test reaction
$testPhone = '573007777000';
$userId = $db->insert('users', ['phone_number' => $testPhone, 'name' => 'Reactor', 'status' => 'active']);
$origMsgId = '1001';
$db->insert('conversations', [
'user_id' => $userId,
'message_id' => $origMsgId,
'direction' => 'incoming',
'message_type' => 'text',
'content' => 'Original msg',
'status' => 'received',
'created_at' => date('Y-m-d H:i:s')
]);
$payload = [
'object' => 'whatsapp_business_account',
'entry' => [
[
'changes' => [
[
'field' => 'messages',
'value' => [
'messaging_product' => 'whatsapp',
'metadata' => [
'display_phone_number' => '16505551111',
'phone_number_id' => '123'
],
'contacts' => [
[ 'profile' => ['name' => 'reactor'], 'wa_id' => $testPhone ]
],
'messages' => [
[
'from' => $testPhone,
'id' => '2001',
'timestamp' => time(),
'type' => 'reaction',
'reaction' => ['message_id' => $origMsgId, 'emoji' => '👍']
]
]
]
]
]
]
]
];
$r = $webhook->processPayload($payload);
$conv = $db->fetch('SELECT * FROM conversations WHERE message_id = :mid', ['mid' => '2001']);
if ($conv) {
echo "Reaction saved: reaction_to_message_id={$conv['reaction_to_message_id']}, reaction_emoji={$conv['reaction_emoji']}\n";
} else {
echo "Reaction not saved\n";
}
// Cleanup reaction test
$db->execute('DELETE FROM conversations WHERE message_id = :mid', ['mid' => '2001']);
$db->execute('DELETE FROM conversations WHERE message_id = :mid', ['mid' => $origMsgId]);
$db->execute('DELETE FROM users WHERE id = :id', ['id' => $userId]);
// Test reply/context
$testPhone2 = '573007777111';
$userId2 = $db->insert('users', ['phone_number' => $testPhone2, 'name' => 'Replier', 'status' => 'active']);
$origMsgId2 = '1002';
$db->insert('conversations', [
'user_id' => $userId2,
'message_id' => $origMsgId2,
'direction' => 'incoming',
'message_type' => 'text',
'content' => 'Original for reply',
'status' => 'received',
'created_at' => date('Y-m-d H:i:s')
]);
$payload2 = [
'object' => 'whatsapp_business_account',
'entry' => [
[
'changes' => [
[
'field' => 'messages',
'value' => [
'messages' => [
[
'from' => $testPhone2,
'id' => '2002',
'timestamp' => time(),
'type' => 'text',
'text' => ['body' => 'Reply message'],
'context' => ['id' => $origMsgId2]
]
]
]
]
]
]
]
];
$r2 = $webhook->processPayload($payload2);
$conv2 = $db->fetch('SELECT * FROM conversations WHERE message_id = :mid', ['mid' => '2002']);
if ($conv2) {
echo "Reply saved: reply_to_message_id={$conv2['reply_to_message_id']}\n";
} else {
echo "Reply not saved\n";
}
// Cleanup reply test
$db->execute('DELETE FROM conversations WHERE message_id = :mid', ['mid' => '2002']);
$db->execute('DELETE FROM conversations WHERE message_id = :mid', ['mid' => $origMsgId2]);
$db->execute('DELETE FROM users WHERE id = :id', ['id' => $userId2]);
echo "Done\n";
+12
View File
@@ -0,0 +1,12 @@
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
try {
require_once __DIR__ . '/../config/config_enhanced.php';
echo "config_enhanced OK\n";
require_once __DIR__ . '/../config/config.php';
echo "config.php OK\n";
} catch (Throwable $t) {
echo "Throwable on require: " . $t->getMessage() . "\n";
echo $t->getTraceAsString() . "\n";
}
Binary file not shown.
+16
View File
@@ -0,0 +1,16 @@
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
echo "START wa_service_test\n";
require_once __DIR__ . '/../services/WhatsAppService.php';
echo "AFTER REQUIRE\n";
try {
$s = new WhatsAppService();
echo "WhatsAppService OK\n";
} catch (Throwable $t) {
echo "ERROR: " . $t->getMessage() . "\n";
echo $t->getTraceAsString() . "\n";
}
echo "END wa_service_test\n";
+13
View File
@@ -0,0 +1,13 @@
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
require_once __DIR__ . '/../api/webhook.php';
try {
$w = new WhatsAppWebhook();
echo "Webhook instance OK\n";
} catch (Throwable $t) {
echo "ERROR: " . $t->getMessage() . "\n";
echo $t->getTraceAsString() . "\n";
}