mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-18 11:39:14 +00:00
@@ -26,11 +26,12 @@ class Chat extends Base {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to this chat.
|
||||
* @param {string} message
|
||||
* Send a message to this chat
|
||||
* @param {string|MessageMedia} content
|
||||
* @param {object} options
|
||||
*/
|
||||
async sendMessage(message) {
|
||||
return this.client.sendMessage(this.id._serialized, message);
|
||||
async sendMessage(content, options) {
|
||||
return this.client.sendMessage(this.id._serialized, content, options);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const Base = require('./Base');
|
||||
const MessageMedia = require('./MessageMedia');
|
||||
|
||||
/**
|
||||
* Represents a Message on WhatsApp
|
||||
@@ -36,6 +37,7 @@ class Message extends Base {
|
||||
|
||||
/**
|
||||
* Returns the Chat this message was sent in
|
||||
* @returns {Chat}
|
||||
*/
|
||||
getChat() {
|
||||
return this.client.getChatById(this._getChatId());
|
||||
@@ -43,6 +45,7 @@ class Message extends Base {
|
||||
|
||||
/**
|
||||
* Returns the Contact this message was sent from
|
||||
* @returns {Contact}
|
||||
*/
|
||||
getContact() {
|
||||
return this.client.getContactById(this._getChatId());
|
||||
@@ -50,6 +53,7 @@ class Message extends Base {
|
||||
|
||||
/**
|
||||
* Returns the quoted message, if any
|
||||
* @returns {Message}
|
||||
*/
|
||||
async getQuotedMessage() {
|
||||
if (!this.hasQuotedMsg) return undefined;
|
||||
@@ -66,46 +70,49 @@ class Message extends Base {
|
||||
* Sends a message as a reply. If chatId is specified, it will be sent
|
||||
* through the specified Chat. If not, it will send the message
|
||||
* in the same Chat as the original message was sent.
|
||||
* @param {string} message
|
||||
*
|
||||
* @param {string|MessageMedia} content
|
||||
* @param {?string} chatId
|
||||
* @param {object} options
|
||||
* @returns {Message}
|
||||
*/
|
||||
async reply(message, chatId) {
|
||||
async reply(content, chatId, options={}) {
|
||||
if (!chatId) {
|
||||
chatId = this._getChatId();
|
||||
}
|
||||
|
||||
const newMessage = await this.client.pupPage.evaluate(async (chatId, quotedMessageId, message) => {
|
||||
let quotedMessage = window.Store.Msg.get(quotedMessageId);
|
||||
if(quotedMessage.canReply()) {
|
||||
const chat = window.Store.Chat.get(chatId);
|
||||
const newMessage = await window.WWebJS.sendMessage(chat, message, quotedMessage.msgContextInfo(chat));
|
||||
return newMessage.serialize();
|
||||
} else {
|
||||
throw new Error('This message cannot be replied to.');
|
||||
}
|
||||
}, chatId, this.id._serialized, message);
|
||||
|
||||
return new Message(this.client, newMessage);
|
||||
options = {
|
||||
...options,
|
||||
quotedMessageId: this.id._serialized
|
||||
};
|
||||
|
||||
return this.client.sendMessage(chatId, content, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads and returns the attatched message media
|
||||
* @returns {MessageMedia}
|
||||
*/
|
||||
async downloadMedia() {
|
||||
if (!this.hasMedia) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return await this.client.pupPage.evaluate(async (msgId) => {
|
||||
const {data, mimetype, filename} = await this.client.pupPage.evaluate(async (msgId) => {
|
||||
const msg = window.Store.Msg.get(msgId);
|
||||
const buffer = await window.WWebJS.downloadBuffer(msg.clientUrl);
|
||||
const decrypted = await window.Store.CryptoLib.decryptE2EMedia(msg.type, buffer, msg.mediaKey, msg.mimetype);
|
||||
const data = await window.WWebJS.readBlobAsync(decrypted._blob);
|
||||
|
||||
return {
|
||||
data,
|
||||
data: data.split(',')[1],
|
||||
mimetype: msg.mimetype,
|
||||
filename: msg.filename
|
||||
};
|
||||
|
||||
}, this.id._serialized);
|
||||
|
||||
return new MessageMedia(mimetype, data, filename);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
31
src/structures/MessageMedia.js
Normal file
31
src/structures/MessageMedia.js
Normal file
@@ -0,0 +1,31 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Media attached to a message
|
||||
* @param {string} mimetype MIME type of the attachment
|
||||
* @param {string} data Base64-encoded data of the file
|
||||
* @param {?string} filename Document file name
|
||||
*/
|
||||
class MessageMedia {
|
||||
constructor(mimetype, data, filename) {
|
||||
/**
|
||||
* MIME type of the attachment
|
||||
* @type {string}
|
||||
*/
|
||||
this.mimetype = mimetype;
|
||||
|
||||
/**
|
||||
* Base64 encoded data that represents the file
|
||||
* @type {string}
|
||||
*/
|
||||
this.data = data;
|
||||
|
||||
/**
|
||||
* Name of the file (for documents)
|
||||
* @type {?string}
|
||||
*/
|
||||
this.filename = filename;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MessageMedia;
|
||||
Reference in New Issue
Block a user