mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-19 03:59:16 +00:00
feat: send media as document (#490)
Co-authored-by: Pedro S. Lopez <pslamoros@hotmail.com>
This commit is contained in:
committed by
Pedro S. Lopez
parent
576768e390
commit
e141a5df97
2
index.d.ts
vendored
2
index.d.ts
vendored
@@ -553,6 +553,8 @@ declare namespace WAWebJS {
|
|||||||
linkPreview?: boolean
|
linkPreview?: boolean
|
||||||
/** Send audio as voice message */
|
/** Send audio as voice message */
|
||||||
sendAudioAsVoice?: boolean
|
sendAudioAsVoice?: boolean
|
||||||
|
/** Send media as document */
|
||||||
|
sendMediaAsDocument?: boolean
|
||||||
/** Automatically parse vCards and send them as contacts */
|
/** Automatically parse vCards and send them as contacts */
|
||||||
parseVCards?: boolean
|
parseVCards?: boolean
|
||||||
/** Image or videos caption */
|
/** Image or videos caption */
|
||||||
|
|||||||
@@ -420,6 +420,7 @@ class Client extends EventEmitter {
|
|||||||
* @typedef {Object} MessageSendOptions
|
* @typedef {Object} MessageSendOptions
|
||||||
* @property {boolean} [linkPreview=true] - Show links preview
|
* @property {boolean} [linkPreview=true] - Show links preview
|
||||||
* @property {boolean} [sendAudioAsVoice=false] - Send audio as voice message
|
* @property {boolean} [sendAudioAsVoice=false] - Send audio as voice message
|
||||||
|
* @property {boolean} [sendMediaAsDocument=false] = Send media as a document
|
||||||
* @property {boolean} [parseVCards=true] - Automatically parse vCards and send them as contacts
|
* @property {boolean} [parseVCards=true] - Automatically parse vCards and send them as contacts
|
||||||
* @property {string} [caption] - Image or video caption
|
* @property {string} [caption] - Image or video caption
|
||||||
* @property {string} [quotedMessageId] - Id of the message that is being quoted (or replied to)
|
* @property {string} [quotedMessageId] - Id of the message that is being quoted (or replied to)
|
||||||
@@ -440,6 +441,7 @@ class Client extends EventEmitter {
|
|||||||
let internalOptions = {
|
let internalOptions = {
|
||||||
linkPreview: options.linkPreview === false ? undefined : true,
|
linkPreview: options.linkPreview === false ? undefined : true,
|
||||||
sendAudioAsVoice: options.sendAudioAsVoice,
|
sendAudioAsVoice: options.sendAudioAsVoice,
|
||||||
|
sendMediaAsDocument: options.sendMediaAsDocument,
|
||||||
caption: options.caption,
|
caption: options.caption,
|
||||||
quotedMessageId: options.quotedMessageId,
|
quotedMessageId: options.quotedMessageId,
|
||||||
parseVCards: options.parseVCards === false ? false : true,
|
parseVCards: options.parseVCards === false ? false : true,
|
||||||
|
|||||||
@@ -56,7 +56,10 @@ exports.LoadUtils = () => {
|
|||||||
window.WWebJS.sendMessage = async (chat, content, options = {}) => {
|
window.WWebJS.sendMessage = async (chat, content, options = {}) => {
|
||||||
let attOptions = {};
|
let attOptions = {};
|
||||||
if (options.attachment) {
|
if (options.attachment) {
|
||||||
attOptions = await window.WWebJS.processMediaData(options.attachment, options.sendAudioAsVoice);
|
attOptions = await window.WWebJS.processMediaData(options.attachment, {
|
||||||
|
forceVoice: options.sendAudioAsVoice,
|
||||||
|
forceDocument: options.sendMediaAsDocument
|
||||||
|
});
|
||||||
content = attOptions.preview;
|
content = attOptions.preview;
|
||||||
delete options.attachment;
|
delete options.attachment;
|
||||||
}
|
}
|
||||||
@@ -157,10 +160,10 @@ exports.LoadUtils = () => {
|
|||||||
return window.Store.Msg.get(newMsgId._serialized);
|
return window.Store.Msg.get(newMsgId._serialized);
|
||||||
};
|
};
|
||||||
|
|
||||||
window.WWebJS.processMediaData = async (mediaInfo, forceVoice) => {
|
window.WWebJS.processMediaData = async (mediaInfo, { forceVoice, forceDocument }) => {
|
||||||
const file = window.WWebJS.mediaInfoToFile(mediaInfo);
|
const file = window.WWebJS.mediaInfoToFile(mediaInfo);
|
||||||
const mData = await window.Store.OpaqueData.createFromData(file, file.type);
|
const mData = await window.Store.OpaqueData.createFromData(file, file.type);
|
||||||
const mediaPrep = window.Store.MediaPrep.prepRawMedia(mData, {});
|
const mediaPrep = window.Store.MediaPrep.prepRawMedia(mData, { asDocument: forceDocument });
|
||||||
const mediaData = await mediaPrep.waitForPrep();
|
const mediaData = await mediaPrep.waitForPrep();
|
||||||
const mediaObject = window.Store.MediaObject.getOrCreateMediaObject(mediaData.filehash);
|
const mediaObject = window.Store.MediaObject.getOrCreateMediaObject(mediaData.filehash);
|
||||||
|
|
||||||
@@ -173,6 +176,10 @@ exports.LoadUtils = () => {
|
|||||||
mediaData.type = 'ptt';
|
mediaData.type = 'ptt';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (forceDocument) {
|
||||||
|
mediaData.type = 'document';
|
||||||
|
}
|
||||||
|
|
||||||
if (!(mediaData.mediaBlob instanceof window.Store.OpaqueData)) {
|
if (!(mediaData.mediaBlob instanceof window.Store.OpaqueData)) {
|
||||||
mediaData.mediaBlob = await window.Store.OpaqueData.createFromData(mediaData.mediaBlob, mediaData.mediaBlob.type);
|
mediaData.mediaBlob = await window.Store.OpaqueData.createFromData(mediaData.mediaBlob, mediaData.mediaBlob.type);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user