feat: send media as document (#490)

Co-authored-by: Pedro S. Lopez <pslamoros@hotmail.com>
This commit is contained in:
Marcelo Carvalho
2020-12-21 23:48:19 -03:00
committed by Pedro S. Lopez
parent 576768e390
commit e141a5df97
3 changed files with 14 additions and 3 deletions

2
index.d.ts vendored
View File

@@ -553,6 +553,8 @@ declare namespace WAWebJS {
linkPreview?: boolean
/** Send audio as voice message */
sendAudioAsVoice?: boolean
/** Send media as document */
sendMediaAsDocument?: boolean
/** Automatically parse vCards and send them as contacts */
parseVCards?: boolean
/** Image or videos caption */

View File

@@ -420,6 +420,7 @@ class Client extends EventEmitter {
* @typedef {Object} MessageSendOptions
* @property {boolean} [linkPreview=true] - Show links preview
* @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 {string} [caption] - Image or video caption
* @property {string} [quotedMessageId] - Id of the message that is being quoted (or replied to)
@@ -440,6 +441,7 @@ class Client extends EventEmitter {
let internalOptions = {
linkPreview: options.linkPreview === false ? undefined : true,
sendAudioAsVoice: options.sendAudioAsVoice,
sendMediaAsDocument: options.sendMediaAsDocument,
caption: options.caption,
quotedMessageId: options.quotedMessageId,
parseVCards: options.parseVCards === false ? false : true,

View File

@@ -56,7 +56,10 @@ exports.LoadUtils = () => {
window.WWebJS.sendMessage = async (chat, content, options = {}) => {
let attOptions = {};
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;
delete options.attachment;
}
@@ -157,10 +160,10 @@ exports.LoadUtils = () => {
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 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 mediaObject = window.Store.MediaObject.getOrCreateMediaObject(mediaData.filehash);
@@ -173,6 +176,10 @@ exports.LoadUtils = () => {
mediaData.type = 'ptt';
}
if (forceDocument) {
mediaData.type = 'document';
}
if (!(mediaData.mediaBlob instanceof window.Store.OpaqueData)) {
mediaData.mediaBlob = await window.Store.OpaqueData.createFromData(mediaData.mediaBlob, mediaData.mediaBlob.type);
}