Saltar al contenido principal

Medios

Buscas un tutorial paso a paso?

Subida y envío de imágenes, video, audio, documentos y stickers. Todos los medios siguen el mismo patrón de dos pasos: subir y luego enviar.

Enviar una Imagen

// Step 1: Upload — returns encrypted media metadata
const media = await client.uploadMedia(absoluteImagePath, "image");

// Step 2: Send using sendRawMessage with the proto message shape
await client.sendRawMessage(jid, {
imageMessage: {
URL: media.URL,
directPath: media.directPath,
mediaKey: media.mediaKey,
fileEncSHA256: media.fileEncSHA256,
fileSHA256: media.fileSHA256,
fileLength: String(media.fileLength),
mimetype: "image/png",
caption: "Sent from whatsmeow-node",
},
});
aviso

Los campos de respuesta de upload usan el casing exacto de protobuf: URL, fileSHA256, fileEncSHA256no url, fileSha256, fileEncSha256. Usar el casing incorrecto fallará silenciosamente.

Código fuente completo: media-send.ts


Enviar Video, Audio y Documentos

Los tres tipos de medios siguen el mismo patrón de subida + envío con campos proto específicos para cada tipo.

Paso de subida compartido

const media = await client.uploadMedia(absolutePath, mediaType);

// These fields are shared across all media types
const sharedFields = {
URL: media.URL,
directPath: media.directPath,
mediaKey: media.mediaKey,
fileEncSHA256: media.fileEncSHA256,
fileSHA256: media.fileSHA256,
fileLength: String(media.fileLength),
mimetype,
};

Video

await client.sendRawMessage(jid, {
videoMessage: {
...sharedFields,
caption: "Sent from whatsmeow-node",
// seconds: 30, // duration
// gifPlayback: true, // play as GIF (loops, no audio)
},
});

Audio (nota de voz)

await client.sendRawMessage(jid, {
audioMessage: {
...sharedFields,
ptt: true, // true = voice note (blue mic icon), false = audio file
// seconds: 15,
},
});

Documento

await client.sendRawMessage(jid, {
documentMessage: {
...sharedFields,
fileName: "report.pdf", // displayed as the document title
caption: "Sent from whatsmeow-node",
},
});

Código fuente completo: media-send-all.ts


Enviar un Sticker

Los stickers usan el mismo flujo de subida pero requieren formato WebP y dimensiones explícitas.

// Upload as "image" — whatsmeow handles encryption the same way
const media = await client.uploadMedia(absoluteStickerPath, "image");

await client.sendRawMessage(jid, {
stickerMessage: {
URL: media.URL,
directPath: media.directPath,
mediaKey: media.mediaKey,
fileEncSHA256: media.fileEncSHA256,
fileSHA256: media.fileSHA256,
fileLength: String(media.fileLength),
mimetype: "image/webp",
width: 512,
height: 512,
// isAnimated: true, // for animated stickers
},
});
aviso

width y height son obligatorios para stickers. Sin ellos, WhatsApp puede mostrar el sticker como un archivo adjunto genérico. El tamaño estándar de sticker es 512x512.

Código fuente completo: sticker-send.ts


Descargar Medios Entrantes

Escucha stickers entrantes (o cualquier medio) y guárdalos en disco.

client.on("message", async ({ info, message }) => {
const sticker = message.stickerMessage;
if (!sticker) return;

// downloadAny() auto-detects the media type from the message
// Downloads to a temp file and returns the file path
const filePath = await client.downloadAny(message);
console.log(`Downloaded to: ${filePath}`);
});
info

downloadAny() funciona con cualquier tipo de medio — imágenes, videos, audio, documentos y stickers. Solo verifica el campo de mensaje apropiado (imageMessage, videoMessage, audioMessage, documentMessage, stickerMessage).

Código fuente completo: sticker-download.ts