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
70 lines
1.8 KiB
TypeScript
70 lines
1.8 KiB
TypeScript
/**
|
|
* 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}`
|
|
})
|
|
}
|
|
})
|