exec("CREATE TABLE IF NOT EXISTS admin.whatsapp_messages ( id SERIAL PRIMARY KEY, direction VARCHAR(4), phone VARCHAR(20), message TEXT, msg_type VARCHAR(20), wa_id VARCHAR(50), status VARCHAR(20) DEFAULT 'received', created_at TIMESTAMP DEFAULT NOW() )"); $stmt = $db->prepare("INSERT INTO admin.whatsapp_messages (direction,phone,message,msg_type,wa_id) VALUES('in',?,?,?,?)"); $stmt->execute([$from, $text, $type, $msg['id'] ?? '']); } catch (Exception $e) {} // Auto-reply (optional) // Uncomment to enable: autoReply($from, $text); } } // Process status updates if (isset($data['entry'][0]['changes'][0]['value']['statuses'])) { $statuses = $data['entry'][0]['changes'][0]['value']['statuses']; foreach ($statuses as $st) { try { $db = new PDO('pgsql:host=127.0.0.1;dbname=adx_system', 'admin', weval_secret('DB_PASS','admin123')); $db->prepare("UPDATE admin.whatsapp_messages SET status=? WHERE wa_id=?")->execute([$st['status'], $st['id']]); } catch (Exception $e) {} } } http_response_code(200); echo json_encode(['ok' => true]);