Fix: Agregar soporte para recepción de Polls y Events
All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 1m8s
All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 1m8s
- manager.ts: Detectar pollCreationMessage y eventMessage en getMessageType - manager.ts: Extraer y guardar datos de poll/event como JSON en content - index.get.ts: Parsear poll/event desde raw_message y content - index.get.ts: Incluir poll y event en respuesta de la API
This commit is contained in:
@@ -116,6 +116,38 @@ function parseRawMessage(raw: any, messageType: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Poll message
|
||||||
|
if (messageType === 'poll') {
|
||||||
|
const pollMsg = msg.pollCreationMessage || msg.pollCreationMessageV3
|
||||||
|
if (pollMsg) {
|
||||||
|
result.poll = {
|
||||||
|
name: pollMsg.name,
|
||||||
|
options: pollMsg.options?.map((o: any) => o.optionName) || [],
|
||||||
|
selectableCount: pollMsg.selectableOptionsCount || 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Event message
|
||||||
|
if (messageType === 'event' && msg.eventMessage) {
|
||||||
|
result.event = {
|
||||||
|
name: msg.eventMessage.name,
|
||||||
|
startDate: msg.eventMessage.startTime
|
||||||
|
? new Date(msg.eventMessage.startTime * 1000).toISOString()
|
||||||
|
: null,
|
||||||
|
endDate: msg.eventMessage.endTime
|
||||||
|
? new Date(msg.eventMessage.endTime * 1000).toISOString()
|
||||||
|
: null,
|
||||||
|
description: msg.eventMessage.description,
|
||||||
|
location: msg.eventMessage.location ? {
|
||||||
|
name: msg.eventMessage.location.name,
|
||||||
|
address: msg.eventMessage.location.address,
|
||||||
|
latitude: msg.eventMessage.location.degreesLatitude,
|
||||||
|
longitude: msg.eventMessage.location.degreesLongitude
|
||||||
|
} : undefined
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Extract quoted message if exists
|
// Extract quoted message if exists
|
||||||
const contextInfo = getContextInfo(msg)
|
const contextInfo = getContextInfo(msg)
|
||||||
if (contextInfo?.quotedMessage) {
|
if (contextInfo?.quotedMessage) {
|
||||||
@@ -183,8 +215,10 @@ function getMessageType(msg: any): string {
|
|||||||
if (msg.audioMessage) return 'audio'
|
if (msg.audioMessage) return 'audio'
|
||||||
if (msg.documentMessage) return 'document'
|
if (msg.documentMessage) return 'document'
|
||||||
if (msg.stickerMessage) return 'sticker'
|
if (msg.stickerMessage) return 'sticker'
|
||||||
if (msg.contactMessage) return 'contact'
|
if (msg.contactMessage || msg.contactsArrayMessage) return 'contact'
|
||||||
if (msg.locationMessage) return 'location'
|
if (msg.locationMessage) return 'location'
|
||||||
|
if (msg.pollCreationMessage || msg.pollCreationMessageV3) return 'poll'
|
||||||
|
if (msg.eventMessage) return 'event'
|
||||||
return 'unknown'
|
return 'unknown'
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -268,6 +302,20 @@ export default defineEventHandler(async (event) => {
|
|||||||
return result.rows.map(row => {
|
return result.rows.map(row => {
|
||||||
const parsedData = parseRawMessage(row.raw_message, row.message_type)
|
const parsedData = parseRawMessage(row.raw_message, row.message_type)
|
||||||
|
|
||||||
|
// For poll/event, try to parse content as JSON if it's stored that way
|
||||||
|
let poll = parsedData.poll
|
||||||
|
let event = parsedData.event
|
||||||
|
if (row.message_type === 'poll' && row.content && !poll) {
|
||||||
|
try {
|
||||||
|
poll = JSON.parse(row.content)
|
||||||
|
} catch {}
|
||||||
|
}
|
||||||
|
if (row.message_type === 'event' && row.content && !event) {
|
||||||
|
try {
|
||||||
|
event = JSON.parse(row.content)
|
||||||
|
} catch {}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: row.id,
|
id: row.id,
|
||||||
messageId: row.message_id,
|
messageId: row.message_id,
|
||||||
@@ -280,6 +328,8 @@ export default defineEventHandler(async (event) => {
|
|||||||
media: parsedData.media || (row.media_url ? { url: row.media_url } : undefined),
|
media: parsedData.media || (row.media_url ? { url: row.media_url } : undefined),
|
||||||
location: parsedData.location,
|
location: parsedData.location,
|
||||||
contact: parsedData.contact,
|
contact: parsedData.contact,
|
||||||
|
poll: poll,
|
||||||
|
event: event,
|
||||||
quoted: parsedData.quoted,
|
quoted: parsedData.quoted,
|
||||||
timestamp: row.timestamp,
|
timestamp: row.timestamp,
|
||||||
status: row.status || 'sent',
|
status: row.status || 'sent',
|
||||||
|
|||||||
@@ -639,14 +639,46 @@ class BaileysManager extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get message content
|
// Get message content
|
||||||
const content = msg.message?.conversation ||
|
const messageType = this.getMessageType(msg.message)
|
||||||
|
|
||||||
|
// Extract content based on message type
|
||||||
|
let content = ''
|
||||||
|
if (messageType === 'poll') {
|
||||||
|
// For polls, store the poll data as JSON
|
||||||
|
const pollMsg = msg.message?.pollCreationMessage || msg.message?.pollCreationMessageV3
|
||||||
|
if (pollMsg) {
|
||||||
|
content = JSON.stringify({
|
||||||
|
name: pollMsg.name,
|
||||||
|
options: pollMsg.options?.map((o: any) => o.optionName) || [],
|
||||||
|
selectableCount: pollMsg.selectableOptionsCount || 1
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else if (messageType === 'event') {
|
||||||
|
// For events, store the event data as JSON
|
||||||
|
const eventMsg = msg.message?.eventMessage
|
||||||
|
if (eventMsg) {
|
||||||
|
content = JSON.stringify({
|
||||||
|
name: eventMsg.name,
|
||||||
|
startDate: eventMsg.startTime ? new Date(eventMsg.startTime * 1000).toISOString() : null,
|
||||||
|
endDate: eventMsg.endTime ? new Date(eventMsg.endTime * 1000).toISOString() : null,
|
||||||
|
description: eventMsg.description,
|
||||||
|
location: eventMsg.location ? {
|
||||||
|
name: eventMsg.location.name,
|
||||||
|
address: eventMsg.location.address,
|
||||||
|
latitude: eventMsg.location.degreesLatitude,
|
||||||
|
longitude: eventMsg.location.degreesLongitude
|
||||||
|
} : null
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Default content extraction
|
||||||
|
content = msg.message?.conversation ||
|
||||||
msg.message?.extendedTextMessage?.text ||
|
msg.message?.extendedTextMessage?.text ||
|
||||||
msg.message?.imageMessage?.caption ||
|
msg.message?.imageMessage?.caption ||
|
||||||
msg.message?.videoMessage?.caption ||
|
msg.message?.videoMessage?.caption ||
|
||||||
msg.message?.documentMessage?.caption ||
|
msg.message?.documentMessage?.caption ||
|
||||||
''
|
''
|
||||||
|
}
|
||||||
const messageType = this.getMessageType(msg.message)
|
|
||||||
|
|
||||||
// Skip unknown message types with no content (likely internal messages)
|
// Skip unknown message types with no content (likely internal messages)
|
||||||
if (messageType === 'unknown' && !content) {
|
if (messageType === 'unknown' && !content) {
|
||||||
@@ -709,8 +741,10 @@ class BaileysManager extends EventEmitter {
|
|||||||
if (message.audioMessage) return 'audio'
|
if (message.audioMessage) return 'audio'
|
||||||
if (message.documentMessage) return 'document'
|
if (message.documentMessage) return 'document'
|
||||||
if (message.stickerMessage) return 'sticker'
|
if (message.stickerMessage) return 'sticker'
|
||||||
if (message.contactMessage) return 'contact'
|
if (message.contactMessage || message.contactsArrayMessage) return 'contact'
|
||||||
if (message.locationMessage) return 'location'
|
if (message.locationMessage) return 'location'
|
||||||
|
if (message.pollCreationMessage || message.pollCreationMessageV3) return 'poll'
|
||||||
|
if (message.eventMessage) return 'event'
|
||||||
return 'unknown'
|
return 'unknown'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user