diff --git a/example.js b/example.js index 8e1d2d4..63c927f 100644 --- a/example.js +++ b/example.js @@ -54,7 +54,8 @@ client.on('message', async msg => { let messageIndex = msg.body.indexOf(number) + number.length; let message = msg.body.slice(messageIndex, msg.body.length); number = number.includes('@c.us') ? number : `${number}@c.us`; - + let chat = await msg.getChat(); + chat.sendSeen(); client.sendMessage(number, message); } else if (msg.body.startsWith('!subject ')) { @@ -160,7 +161,7 @@ client.on('message', async msg => { }); } else if (msg.body == '!delete' && msg.hasQuotedMsg) { const quotedMsg = await msg.getQuotedMessage(); - if(quotedMsg.fromMe) { + if (quotedMsg.fromMe) { quotedMsg.delete(true); } else { msg.reply('I can only delete my own messages'); diff --git a/src/Client.js b/src/Client.js index 02176cc..c76e23b 100644 --- a/src/Client.js +++ b/src/Client.js @@ -282,7 +282,19 @@ class Client extends EventEmitter { async destroy() { await this.pupBrowser.close(); } + /** + * Mark as seen for the Chat + * @param {string} chatId + * @returns {Promise} result + * + */ + async sendSeen(chatId) { + const result = await this.pupPage.evaluate(async (chatId) => { + return window.WWebJS.sendSeen(chatId); + }, chatId); + return result; + } /** * Send a message to a specific chatId * @param {string} chatId @@ -296,6 +308,8 @@ class Client extends EventEmitter { quotedMessageId: options.quotedMessageId, mentionedJidList: Array.isArray(options.mentions) ? options.mentions.map(contact => contact.id._serialized) : [] }; + + const sendSeen = typeof options.sendSeen === 'undefined' ? true : options.sendSeen; if (content instanceof MessageMedia) { internalOptions.attachment = content; @@ -308,7 +322,7 @@ class Client extends EventEmitter { content = ''; } - const newMessage = await this.pupPage.evaluate(async (chatId, message, options) => { + const newMessage = await this.pupPage.evaluate(async (chatId, message, options, sendSeen) => { let chat = window.Store.Chat.get(chatId); let msg; if (!chat) { // The chat is not available in the previously chatted list @@ -318,16 +332,25 @@ class Client extends EventEmitter { //get the topmost chat object and assign the new chatId to it . //This is just a workaround.May cause problem if there are no chats at all. Need to dig in and emulate how whatsapp web does let chat = window.Store.Chat.models[0]; + if (!chat) + throw 'Chat List empty! Need at least one open conversation with any of your contact'; + let originalChatObjId = chat.id; chat.id = newChatId; + msg = await window.WWebJS.sendMessage(chat, message, options); chat.id = originalChatObjId; //replace the chat with its original id } } - else - msg = await window.WWebJS.sendMessage(chat, message, options); + else { + if(sendSeen) { + window.WWebJS.sendSeen(chatId); + } + + msg = await window.WWebJS.sendMessage(chat, message, options, sendSeen); + } return msg.serialize(); - }, chatId, content, internalOptions); + }, chatId, content, internalOptions, sendSeen); return new Message(this, newMessage); } diff --git a/src/structures/Chat.js b/src/structures/Chat.js index 9811603..8303765 100644 --- a/src/structures/Chat.js +++ b/src/structures/Chat.js @@ -11,7 +11,7 @@ class Chat extends Base { constructor(client, data) { super(client); - if(data) this._patch(data); + if (data) this._patch(data); } _patch(data) { @@ -70,6 +70,14 @@ class Chat extends Base { return this.client.sendMessage(this.id._serialized, content, options); } + /** + * Set the message as seen + * @returns {Promise} result + */ + async sendSeen() { + return this.client.sendSeen(this.id._serialized); + } + /** * Clears all messages from the chat * @returns {Promise} result diff --git a/src/util/Injected.js b/src/util/Injected.js index 1b9f820..dbf523a 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -10,6 +10,7 @@ exports.ExposeStore = (moduleRaidStr) => { window.Store.Conn = window.mR.findModule('Conn')[0].default; window.Store.CryptoLib = window.mR.findModule('decryptE2EMedia')[0]; window.Store.Wap = window.mR.findModule('Wap')[0].default; + window.Store.SendSeen = window.mR.findModule('sendSeen')[0]; window.Store.SendClear = window.mR.findModule('sendClear')[0]; window.Store.SendDelete = window.mR.findModule('sendDelete')[0]; window.Store.genId = window.mR.findModule((module) => module.default && typeof module.default === 'function' && module.default.toString().match(/crypto/))[0].default; @@ -35,6 +36,15 @@ exports.LoadUtils = () => { throw 'The number provided is not a registered whatsapp user'; return result.jid; }; + window.WWebJS.sendSeen = async (chatId) => { + let chat = window.Store.Chat.get(chatId); + if (chat !== undefined) { + await window.Store.SendSeen.sendSeen(chat, false); + return true; + } + return false; + + }; window.WWebJS.sendMessage = async (chat, content, options = {}) => { let attOptions = {}; if (options.attachment) {