feat: WhatsApp Nucleo con Nuxt 4 + Baileys v7
Some checks failed
Build and Deploy / build-and-deploy (push) Failing after 6m46s
Some checks failed
Build and Deploy / build-and-deploy (push) Failing after 6m46s
Reemplazo completo de Evolution API por implementación directa con Baileys. Características: - Dashboard completo con Nuxt UI v4 - Soporte para múltiples instancias de WhatsApp - Conexión via QR code o pairing code - Persistencia de mensajes en PostgreSQL - API REST para integraciones externas - Webhooks con firma HMAC - SSE para actualizaciones en tiempo real - Autenticación con Authentik
This commit is contained in:
54
server/api/instances/[id]/connect.post.ts
Normal file
54
server/api/instances/[id]/connect.post.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* POST /api/instances/:id/connect
|
||||
* Connect an instance (generates QR code)
|
||||
*/
|
||||
import { query } from '../../../utils/database'
|
||||
import { baileysManager } from '../../../services/baileys/manager'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const username = getHeader(event, 'x-authentik-username')
|
||||
if (!username) {
|
||||
throw createError({ statusCode: 401, message: 'Unauthorized' })
|
||||
}
|
||||
|
||||
const id = getRouterParam(event, 'id')
|
||||
|
||||
// Check if instance exists
|
||||
const result = await query<{ id: string; status: string }>(
|
||||
'SELECT id, status FROM instances WHERE id = $1',
|
||||
[id]
|
||||
)
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
throw createError({ statusCode: 404, message: 'Instance not found' })
|
||||
}
|
||||
|
||||
const instance = result.rows[0]
|
||||
|
||||
// Don't connect if already connected
|
||||
if (instance.status === 'connected') {
|
||||
throw createError({ statusCode: 400, message: 'Instance already connected' })
|
||||
}
|
||||
|
||||
// Start connection
|
||||
try {
|
||||
await baileysManager.connect(id!)
|
||||
|
||||
// Wait a bit for QR to generate
|
||||
await new Promise(resolve => setTimeout(resolve, 2000))
|
||||
|
||||
const qrCode = baileysManager.getQRCode(id!)
|
||||
const status = baileysManager.getStatus(id!)
|
||||
|
||||
return {
|
||||
success: true,
|
||||
status: status?.status || 'connecting',
|
||||
qrCode
|
||||
}
|
||||
} catch (error) {
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
message: `Failed to connect: ${(error as Error).message}`
|
||||
})
|
||||
}
|
||||
})
|
||||
35
server/api/instances/[id]/disconnect.post.ts
Normal file
35
server/api/instances/[id]/disconnect.post.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* POST /api/instances/:id/disconnect
|
||||
* Disconnect an instance
|
||||
*/
|
||||
import { query } from '../../../utils/database'
|
||||
import { baileysManager } from '../../../services/baileys/manager'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const username = getHeader(event, 'x-authentik-username')
|
||||
if (!username) {
|
||||
throw createError({ statusCode: 401, message: 'Unauthorized' })
|
||||
}
|
||||
|
||||
const id = getRouterParam(event, 'id')
|
||||
|
||||
// Check if instance exists
|
||||
const result = await query<{ id: string }>(
|
||||
'SELECT id FROM instances WHERE id = $1',
|
||||
[id]
|
||||
)
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
throw createError({ statusCode: 404, message: 'Instance not found' })
|
||||
}
|
||||
|
||||
try {
|
||||
await baileysManager.disconnect(id!)
|
||||
return { success: true, status: 'disconnected' }
|
||||
} catch (error) {
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
message: `Failed to disconnect: ${(error as Error).message}`
|
||||
})
|
||||
}
|
||||
})
|
||||
41
server/api/instances/[id]/index.delete.ts
Normal file
41
server/api/instances/[id]/index.delete.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* DELETE /api/instances/:id
|
||||
* Delete an instance
|
||||
*/
|
||||
import { query } from '../../../utils/database'
|
||||
import { baileysManager } from '../../../services/baileys/manager'
|
||||
import { clearAuthState } from '../../../services/baileys/auth-state'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const username = getHeader(event, 'x-authentik-username')
|
||||
if (!username) {
|
||||
throw createError({ statusCode: 401, message: 'Unauthorized' })
|
||||
}
|
||||
|
||||
const id = getRouterParam(event, 'id')
|
||||
|
||||
// Check if instance exists
|
||||
const result = await query<{ id: string }>(
|
||||
'SELECT id FROM instances WHERE id = $1',
|
||||
[id]
|
||||
)
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
throw createError({ statusCode: 404, message: 'Instance not found' })
|
||||
}
|
||||
|
||||
// Disconnect if connected
|
||||
try {
|
||||
await baileysManager.disconnect(id!)
|
||||
} catch (error) {
|
||||
// Ignore disconnect errors
|
||||
}
|
||||
|
||||
// Clear auth state
|
||||
await clearAuthState(id!)
|
||||
|
||||
// Delete from database (cascades to related tables)
|
||||
await query('DELETE FROM instances WHERE id = $1', [id])
|
||||
|
||||
return { success: true }
|
||||
})
|
||||
53
server/api/instances/[id]/index.get.ts
Normal file
53
server/api/instances/[id]/index.get.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* GET /api/instances/:id
|
||||
* Get instance details
|
||||
*/
|
||||
import { query } from '../../../utils/database'
|
||||
import { baileysManager } from '../../../services/baileys/manager'
|
||||
|
||||
interface InstanceRow {
|
||||
id: string
|
||||
name: string
|
||||
phone_number: string | null
|
||||
status: string
|
||||
qr_code: string | null
|
||||
pairing_code: string | null
|
||||
last_connected_at: Date | null
|
||||
created_by: string
|
||||
created_at: Date
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const username = getHeader(event, 'x-authentik-username')
|
||||
if (!username) {
|
||||
throw createError({ statusCode: 401, message: 'Unauthorized' })
|
||||
}
|
||||
|
||||
const id = getRouterParam(event, 'id')
|
||||
|
||||
const result = await query<InstanceRow>(
|
||||
`SELECT id, name, phone_number, status, qr_code, pairing_code,
|
||||
last_connected_at, created_by, created_at
|
||||
FROM instances WHERE id = $1`,
|
||||
[id]
|
||||
)
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
throw createError({ statusCode: 404, message: 'Instance not found' })
|
||||
}
|
||||
|
||||
const instance = result.rows[0]
|
||||
const liveStatus = baileysManager.getStatus(instance.id)
|
||||
|
||||
return {
|
||||
id: instance.id,
|
||||
name: instance.name,
|
||||
phoneNumber: instance.phone_number,
|
||||
status: liveStatus?.status || instance.status,
|
||||
qrCode: liveStatus?.qrCode || instance.qr_code,
|
||||
pairingCode: liveStatus?.pairingCode || instance.pairing_code,
|
||||
lastConnectedAt: instance.last_connected_at,
|
||||
createdBy: instance.created_by,
|
||||
createdAt: instance.created_at
|
||||
}
|
||||
})
|
||||
69
server/api/instances/[id]/pairing-code.post.ts
Normal file
69
server/api/instances/[id]/pairing-code.post.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* POST /api/instances/:id/pairing-code
|
||||
* Request a pairing code for connection without QR
|
||||
*/
|
||||
import { query } from '../../../utils/database'
|
||||
import { baileysManager } from '../../../services/baileys/manager'
|
||||
|
||||
interface PairingCodeBody {
|
||||
phoneNumber: string
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const username = getHeader(event, 'x-authentik-username')
|
||||
if (!username) {
|
||||
throw createError({ statusCode: 401, message: 'Unauthorized' })
|
||||
}
|
||||
|
||||
const id = getRouterParam(event, 'id')
|
||||
const body = await readBody<PairingCodeBody>(event)
|
||||
|
||||
if (!body.phoneNumber?.trim()) {
|
||||
throw createError({ statusCode: 400, message: 'Phone number is required' })
|
||||
}
|
||||
|
||||
// Clean phone number (remove +, spaces, dashes)
|
||||
const cleanPhone = body.phoneNumber.replace(/[^0-9]/g, '')
|
||||
|
||||
// Check if instance exists
|
||||
const result = await query<{ id: string; status: string }>(
|
||||
'SELECT id, status FROM instances WHERE id = $1',
|
||||
[id]
|
||||
)
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
throw createError({ statusCode: 404, message: 'Instance not found' })
|
||||
}
|
||||
|
||||
const instance = result.rows[0]
|
||||
|
||||
if (instance.status === 'connected') {
|
||||
throw createError({ statusCode: 400, message: 'Instance already connected' })
|
||||
}
|
||||
|
||||
try {
|
||||
// Connect with pairing code mode
|
||||
await baileysManager.connect(id!, true, cleanPhone)
|
||||
|
||||
// Wait for pairing code
|
||||
await new Promise(resolve => setTimeout(resolve, 5000))
|
||||
|
||||
const pairingCode = baileysManager.getPairingCode(id!)
|
||||
const status = baileysManager.getStatus(id!)
|
||||
|
||||
if (!pairingCode) {
|
||||
throw new Error('Failed to generate pairing code')
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
code: pairingCode,
|
||||
status: status?.status || 'pairing'
|
||||
}
|
||||
} catch (error) {
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
message: `Failed to request pairing code: ${(error as Error).message}`
|
||||
})
|
||||
}
|
||||
})
|
||||
34
server/api/instances/[id]/qr.get.ts
Normal file
34
server/api/instances/[id]/qr.get.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* GET /api/instances/:id/qr
|
||||
* Get current QR code for an instance
|
||||
*/
|
||||
import { query } from '../../../utils/database'
|
||||
import { baileysManager } from '../../../services/baileys/manager'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const username = getHeader(event, 'x-authentik-username')
|
||||
if (!username) {
|
||||
throw createError({ statusCode: 401, message: 'Unauthorized' })
|
||||
}
|
||||
|
||||
const id = getRouterParam(event, 'id')
|
||||
|
||||
// Check if instance exists
|
||||
const result = await query<{ id: string; qr_code: string | null; status: string }>(
|
||||
'SELECT id, qr_code, status FROM instances WHERE id = $1',
|
||||
[id]
|
||||
)
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
throw createError({ statusCode: 404, message: 'Instance not found' })
|
||||
}
|
||||
|
||||
// Get live QR from manager
|
||||
const qrCode = baileysManager.getQRCode(id!)
|
||||
const status = baileysManager.getStatus(id!)
|
||||
|
||||
return {
|
||||
qrCode: qrCode || result.rows[0].qr_code,
|
||||
status: status?.status || result.rows[0].status
|
||||
}
|
||||
})
|
||||
36
server/api/instances/[id]/status.get.ts
Normal file
36
server/api/instances/[id]/status.get.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* GET /api/instances/:id/status
|
||||
* Get instance connection status
|
||||
*/
|
||||
import { query } from '../../../utils/database'
|
||||
import { baileysManager } from '../../../services/baileys/manager'
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const username = getHeader(event, 'x-authentik-username')
|
||||
if (!username) {
|
||||
throw createError({ statusCode: 401, message: 'Unauthorized' })
|
||||
}
|
||||
|
||||
const id = getRouterParam(event, 'id')
|
||||
|
||||
// Check if instance exists
|
||||
const result = await query<{ id: string; status: string; phone_number: string | null }>(
|
||||
'SELECT id, status, phone_number FROM instances WHERE id = $1',
|
||||
[id]
|
||||
)
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
throw createError({ statusCode: 404, message: 'Instance not found' })
|
||||
}
|
||||
|
||||
const instance = result.rows[0]
|
||||
const liveStatus = baileysManager.getStatus(id!)
|
||||
|
||||
return {
|
||||
instanceId: id,
|
||||
status: liveStatus?.status || instance.status,
|
||||
phoneNumber: liveStatus?.phoneNumber || instance.phone_number,
|
||||
hasQR: !!liveStatus?.qrCode,
|
||||
hasPairingCode: !!liveStatus?.pairingCode
|
||||
}
|
||||
})
|
||||
42
server/api/instances/index.get.ts
Normal file
42
server/api/instances/index.get.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* GET /api/instances
|
||||
* List all instances
|
||||
*/
|
||||
import { query } from '../../utils/database'
|
||||
import { baileysManager } from '../../services/baileys/manager'
|
||||
|
||||
interface InstanceRow {
|
||||
id: string
|
||||
name: string
|
||||
phone_number: string | null
|
||||
status: string
|
||||
last_connected_at: Date | null
|
||||
created_at: Date
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
// Check auth
|
||||
const username = getHeader(event, 'x-authentik-username')
|
||||
if (!username) {
|
||||
throw createError({ statusCode: 401, message: 'Unauthorized' })
|
||||
}
|
||||
|
||||
const result = await query<InstanceRow>(
|
||||
`SELECT id, name, phone_number, status, last_connected_at, created_at
|
||||
FROM instances
|
||||
ORDER BY created_at DESC`
|
||||
)
|
||||
|
||||
// Enrich with live status from manager
|
||||
return result.rows.map(row => {
|
||||
const liveStatus = baileysManager.getStatus(row.id)
|
||||
return {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
phoneNumber: row.phone_number,
|
||||
status: liveStatus?.status || row.status,
|
||||
lastConnectedAt: row.last_connected_at,
|
||||
createdAt: row.created_at
|
||||
}
|
||||
})
|
||||
})
|
||||
48
server/api/instances/index.post.ts
Normal file
48
server/api/instances/index.post.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* POST /api/instances
|
||||
* Create a new instance
|
||||
*/
|
||||
import { query } from '../../utils/database'
|
||||
|
||||
interface CreateInstanceBody {
|
||||
name: string
|
||||
}
|
||||
|
||||
interface InstanceRow {
|
||||
id: string
|
||||
name: string
|
||||
phone_number: string | null
|
||||
status: string
|
||||
created_at: Date
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
// Check auth
|
||||
const username = getHeader(event, 'x-authentik-username')
|
||||
if (!username) {
|
||||
throw createError({ statusCode: 401, message: 'Unauthorized' })
|
||||
}
|
||||
|
||||
const body = await readBody<CreateInstanceBody>(event)
|
||||
|
||||
if (!body.name?.trim()) {
|
||||
throw createError({ statusCode: 400, message: 'Name is required' })
|
||||
}
|
||||
|
||||
const result = await query<InstanceRow>(
|
||||
`INSERT INTO instances (name, created_by)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id, name, phone_number, status, created_at`,
|
||||
[body.name.trim(), username]
|
||||
)
|
||||
|
||||
const instance = result.rows[0]
|
||||
|
||||
return {
|
||||
id: instance.id,
|
||||
name: instance.name,
|
||||
phoneNumber: instance.phone_number,
|
||||
status: instance.status,
|
||||
createdAt: instance.created_at
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user