Fix: Envío de eventos usando proto directo (bypass sendMessage)
All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 1m12s

Baileys no maneja 'event' en generateWAMessageContent, causando
"Invalid media type" error. Se usa generateWAMessageFromContent
con proto.Message.IEventMessage y relayMessage directamente.
This commit is contained in:
2025-12-04 12:28:17 -06:00
parent b0101c791a
commit a9e786a8bd

View File

@@ -6,6 +6,7 @@
* - Stickers: image converted to WebP (FormData with asSticker flag)
*/
import type { AnyMediaMessageContent } from '@whiskeysockets/baileys'
import { generateWAMessageFromContent, proto } from '@whiskeysockets/baileys'
import { query } from '../../../../utils/database'
import { baileysManager } from '../../../../services/baileys/manager'
import { convertToSticker, canConvertToSticker } from '../../../../services/media/sticker-processor'
@@ -358,6 +359,8 @@ async function handlePollMessage(
/**
* Handle event message sending
* Note: Baileys' sendMessage doesn't handle 'event' type directly,
* so we build the proto message manually and use relayMessage
*/
async function handleEventMessage(
body: EventMessageBody,
@@ -375,33 +378,48 @@ async function handleEventMessage(
throw createError({ statusCode: 400, message: 'Event start date is required' })
}
const eventContent: any = {
// Build the eventMessage proto directly
const eventMessage: proto.Message.IEventMessage = {
name: body.name,
startDate: new Date(body.startDate)
startTime: Math.floor(new Date(body.startDate).getTime() / 1000),
description: body.description || undefined,
extraGuestsAllowed: true
}
if (body.endDate) {
eventContent.endDate = new Date(body.endDate)
eventMessage.endTime = Math.floor(new Date(body.endDate).getTime() / 1000)
}
if (body.description) {
eventContent.description = body.description
}
if (body.location) {
if (body.location.latitude && body.location.longitude) {
eventContent.location = {
if (body.location && body.location.latitude && body.location.longitude) {
eventMessage.location = {
degreesLatitude: body.location.latitude,
degreesLongitude: body.location.longitude,
name: body.location.name,
address: body.location.address
}
name: body.location.name || undefined,
address: body.location.address || undefined
}
}
const content = { event: eventContent }
// Create the message content with eventMessage
const messageContent: proto.IMessage = {
eventMessage
}
const result = await socket.sendMessage(jid, content, options)
// Generate the full WAMessage using generateWAMessageFromContent
const userJid = socket.user?.id
const generatedMessage = generateWAMessageFromContent(
jid,
messageContent,
{
userJid,
quoted: options.quoted,
timestamp: new Date()
}
)
// Use relayMessage to send the pre-built message
await socket.relayMessage(jid, generatedMessage.message!, {
messageId: generatedMessage.key.id
})
// Save to database
await query(
@@ -413,7 +431,7 @@ async function handleEventMessage(
[
instanceId,
chatId,
result.key.id,
generatedMessage.key.id,
'me',
true,
'event',
@@ -425,7 +443,7 @@ async function handleEventMessage(
location: body.location
}),
'sent',
JSON.stringify(result),
JSON.stringify(generatedMessage),
body.quotedMessageId || null
]
)
@@ -437,7 +455,7 @@ async function handleEventMessage(
return {
success: true,
messages: [{ messageId: result.key.id, type: 'event' }]
messages: [{ messageId: generatedMessage.key.id, type: 'event' }]
}
}