mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-17 19:26:20 +00:00
Support replying directly to messages
This commit is contained in:
@@ -19,7 +19,12 @@ client.on('ready', () => {
|
||||
client.on('message', (msg) => {
|
||||
console.log('MESSAGE RECEIVED', msg);
|
||||
|
||||
if (msg.body == 'ping') {
|
||||
if (msg.body == 'ping reply') {
|
||||
// Send a new message as a reply to the current one
|
||||
msg.reply('pong');
|
||||
|
||||
} else if (msg.body == 'ping') {
|
||||
// Send a new message to the same chat
|
||||
client.sendMessage(msg.from, 'pong');
|
||||
}
|
||||
})
|
||||
|
||||
@@ -25,10 +25,39 @@ class Message extends Base {
|
||||
this.broadcast = data.broadcast;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Chat this message was sent in
|
||||
*/
|
||||
getChat() {
|
||||
return this.client.getChatById(this.from);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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} chatId
|
||||
*/
|
||||
async reply(message, chatId) {
|
||||
if (!chatId) {
|
||||
chatId = this.from;
|
||||
}
|
||||
|
||||
return await this.client.pupPage.evaluate((chatId, quotedMessageId, message) => {
|
||||
let quotedMessage = Store.Msg.get(quotedMessageId);
|
||||
if(quotedMessage.canReply()) {
|
||||
const chat = Store.Chat.get(chatId);
|
||||
chat.composeQuotedMsg = quotedMessage;
|
||||
chat.sendMessage(message, {quotedMsg: quotedMessage});
|
||||
chat.composeQuotedMsg = null;
|
||||
} else {
|
||||
throw new Error('This message cannot be replied to.');
|
||||
}
|
||||
|
||||
}, chatId, this.id._serialized, message);
|
||||
}
|
||||
|
||||
static get WAppModel() {
|
||||
return 'Msg';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user