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:
72
server/api/messages/[instanceId]/[chatId]/index.get.ts
Normal file
72
server/api/messages/[instanceId]/[chatId]/index.get.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* GET /api/messages/:instanceId/:chatId
|
||||
* Get messages for a chat
|
||||
*/
|
||||
import { query } from '../../../../utils/database'
|
||||
|
||||
interface MessageRow {
|
||||
id: string
|
||||
message_id: string
|
||||
from_jid: string
|
||||
from_me: boolean
|
||||
message_type: string
|
||||
content: string | null
|
||||
caption: string | null
|
||||
media_url: string | null
|
||||
timestamp: Date
|
||||
status: string
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const username = getHeader(event, 'x-authentik-username')
|
||||
if (!username) {
|
||||
throw createError({ statusCode: 401, message: 'Unauthorized' })
|
||||
}
|
||||
|
||||
const instanceId = getRouterParam(event, 'instanceId')
|
||||
const chatId = getRouterParam(event, 'chatId')
|
||||
|
||||
// Get query params for pagination
|
||||
const queryParams = getQuery(event)
|
||||
const limit = Math.min(parseInt(queryParams.limit as string) || 50, 100)
|
||||
const offset = parseInt(queryParams.offset as string) || 0
|
||||
|
||||
// Verify chat exists and belongs to instance
|
||||
const chatCheck = await query(
|
||||
'SELECT id FROM chats WHERE id = $1 AND instance_id = $2',
|
||||
[chatId, instanceId]
|
||||
)
|
||||
if (chatCheck.rows.length === 0) {
|
||||
throw createError({ statusCode: 404, message: 'Chat not found' })
|
||||
}
|
||||
|
||||
// Get messages
|
||||
const result = await query<MessageRow>(
|
||||
`SELECT id, message_id, from_jid, from_me, message_type,
|
||||
content, caption, media_url, timestamp, status
|
||||
FROM messages
|
||||
WHERE chat_id = $1
|
||||
ORDER BY timestamp DESC
|
||||
LIMIT $2 OFFSET $3`,
|
||||
[chatId, limit, offset]
|
||||
)
|
||||
|
||||
// Mark as read
|
||||
await query(
|
||||
'UPDATE chats SET unread_count = 0 WHERE id = $1',
|
||||
[chatId]
|
||||
)
|
||||
|
||||
return result.rows.map(row => ({
|
||||
id: row.id,
|
||||
messageId: row.message_id,
|
||||
fromJid: row.from_jid,
|
||||
fromMe: row.from_me,
|
||||
type: row.message_type,
|
||||
content: row.content,
|
||||
caption: row.caption,
|
||||
mediaUrl: row.media_url,
|
||||
timestamp: row.timestamp,
|
||||
status: row.status
|
||||
}))
|
||||
})
|
||||
53
server/api/messages/[instanceId]/[chatId]/send.post.ts
Normal file
53
server/api/messages/[instanceId]/[chatId]/send.post.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* POST /api/messages/:instanceId/:chatId/send
|
||||
* Send a message to a chat (from UI)
|
||||
*/
|
||||
import { query } from '../../../../utils/database'
|
||||
import { baileysManager } from '../../../../services/baileys/manager'
|
||||
|
||||
interface SendMessageBody {
|
||||
message: string
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const username = getHeader(event, 'x-authentik-username')
|
||||
if (!username) {
|
||||
throw createError({ statusCode: 401, message: 'Unauthorized' })
|
||||
}
|
||||
|
||||
const instanceId = getRouterParam(event, 'instanceId')
|
||||
const chatId = getRouterParam(event, 'chatId')
|
||||
const body = await readBody<SendMessageBody>(event)
|
||||
|
||||
if (!body.message?.trim()) {
|
||||
throw createError({ statusCode: 400, message: 'Message is required' })
|
||||
}
|
||||
|
||||
// Get chat JID
|
||||
const chatResult = await query<{ jid: string }>(
|
||||
'SELECT jid FROM chats WHERE id = $1 AND instance_id = $2',
|
||||
[chatId, instanceId]
|
||||
)
|
||||
|
||||
if (chatResult.rows.length === 0) {
|
||||
throw createError({ statusCode: 404, message: 'Chat not found' })
|
||||
}
|
||||
|
||||
const jid = chatResult.rows[0].jid
|
||||
|
||||
try {
|
||||
const result = await baileysManager.sendMessage(instanceId!, jid, {
|
||||
text: body.message
|
||||
})
|
||||
|
||||
return {
|
||||
success: true,
|
||||
messageId: result.key.id
|
||||
}
|
||||
} catch (error) {
|
||||
throw createError({
|
||||
statusCode: 500,
|
||||
message: `Failed to send message: ${(error as Error).message}`
|
||||
})
|
||||
}
|
||||
})
|
||||
47
server/api/messages/[instanceId]/chats.get.ts
Normal file
47
server/api/messages/[instanceId]/chats.get.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* GET /api/messages/:instanceId/chats
|
||||
* Get all chats for an instance
|
||||
*/
|
||||
import { query } from '../../../utils/database'
|
||||
|
||||
interface ChatRow {
|
||||
id: string
|
||||
jid: string
|
||||
name: string | null
|
||||
is_group: boolean
|
||||
unread_count: number
|
||||
last_message_at: Date | null
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
const username = getHeader(event, 'x-authentik-username')
|
||||
if (!username) {
|
||||
throw createError({ statusCode: 401, message: 'Unauthorized' })
|
||||
}
|
||||
|
||||
const instanceId = getRouterParam(event, 'instanceId')
|
||||
|
||||
// Verify instance exists
|
||||
const instanceCheck = await query('SELECT id FROM instances WHERE id = $1', [instanceId])
|
||||
if (instanceCheck.rows.length === 0) {
|
||||
throw createError({ statusCode: 404, message: 'Instance not found' })
|
||||
}
|
||||
|
||||
// Get chats with last message
|
||||
const result = await query<ChatRow>(
|
||||
`SELECT c.id, c.jid, c.name, c.is_group, c.unread_count, c.last_message_at
|
||||
FROM chats c
|
||||
WHERE c.instance_id = $1
|
||||
ORDER BY c.last_message_at DESC NULLS LAST`,
|
||||
[instanceId]
|
||||
)
|
||||
|
||||
return result.rows.map(row => ({
|
||||
id: row.id,
|
||||
jid: row.jid,
|
||||
name: row.name || row.jid.split('@')[0],
|
||||
isGroup: row.is_group,
|
||||
unreadCount: row.unread_count,
|
||||
lastMessageAt: row.last_message_at
|
||||
}))
|
||||
})
|
||||
Reference in New Issue
Block a user