feat : send conversation seen (#66)

After this commit, chats will be automatically marked as seen when a message is sent to it. If this is not the intended behavior, `sendSeen: false` can be set as part of the options object. 

You can also manually mark a conversation as seen by calling the sendSeen() function on the Chat.

Co-authored-by: Pedro S. Lopez <pedroslopez@me.com>
This commit is contained in:
sahalMoidu
2020-03-02 00:12:35 +05:30
committed by GitHub
parent 48b9ae1e81
commit 98458f760e
4 changed files with 49 additions and 7 deletions

View File

@@ -54,7 +54,8 @@ client.on('message', async msg => {
let messageIndex = msg.body.indexOf(number) + number.length; let messageIndex = msg.body.indexOf(number) + number.length;
let message = msg.body.slice(messageIndex, msg.body.length); let message = msg.body.slice(messageIndex, msg.body.length);
number = number.includes('@c.us') ? number : `${number}@c.us`; number = number.includes('@c.us') ? number : `${number}@c.us`;
let chat = await msg.getChat();
chat.sendSeen();
client.sendMessage(number, message); client.sendMessage(number, message);
} else if (msg.body.startsWith('!subject ')) { } else if (msg.body.startsWith('!subject ')) {
@@ -160,7 +161,7 @@ client.on('message', async msg => {
}); });
} else if (msg.body == '!delete' && msg.hasQuotedMsg) { } else if (msg.body == '!delete' && msg.hasQuotedMsg) {
const quotedMsg = await msg.getQuotedMessage(); const quotedMsg = await msg.getQuotedMessage();
if(quotedMsg.fromMe) { if (quotedMsg.fromMe) {
quotedMsg.delete(true); quotedMsg.delete(true);
} else { } else {
msg.reply('I can only delete my own messages'); msg.reply('I can only delete my own messages');

View File

@@ -282,7 +282,19 @@ class Client extends EventEmitter {
async destroy() { async destroy() {
await this.pupBrowser.close(); await this.pupBrowser.close();
} }
/**
* Mark as seen for the Chat
* @param {string} chatId
* @returns {Promise<boolean>} 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 * Send a message to a specific chatId
* @param {string} chatId * @param {string} chatId
@@ -297,6 +309,8 @@ class Client extends EventEmitter {
mentionedJidList: Array.isArray(options.mentions) ? options.mentions.map(contact => contact.id._serialized) : [] 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) { if (content instanceof MessageMedia) {
internalOptions.attachment = content; internalOptions.attachment = content;
content = ''; content = '';
@@ -308,7 +322,7 @@ class Client extends EventEmitter {
content = ''; 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 chat = window.Store.Chat.get(chatId);
let msg; let msg;
if (!chat) { // The chat is not available in the previously chatted list 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 . //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 //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]; 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; let originalChatObjId = chat.id;
chat.id = newChatId; chat.id = newChatId;
msg = await window.WWebJS.sendMessage(chat, message, options); msg = await window.WWebJS.sendMessage(chat, message, options);
chat.id = originalChatObjId; //replace the chat with its original id chat.id = originalChatObjId; //replace the chat with its original id
} }
} }
else else {
msg = await window.WWebJS.sendMessage(chat, message, options); if(sendSeen) {
window.WWebJS.sendSeen(chatId);
}
msg = await window.WWebJS.sendMessage(chat, message, options, sendSeen);
}
return msg.serialize(); return msg.serialize();
}, chatId, content, internalOptions); }, chatId, content, internalOptions, sendSeen);
return new Message(this, newMessage); return new Message(this, newMessage);
} }

View File

@@ -11,7 +11,7 @@ class Chat extends Base {
constructor(client, data) { constructor(client, data) {
super(client); super(client);
if(data) this._patch(data); if (data) this._patch(data);
} }
_patch(data) { _patch(data) {
@@ -70,6 +70,14 @@ class Chat extends Base {
return this.client.sendMessage(this.id._serialized, content, options); return this.client.sendMessage(this.id._serialized, content, options);
} }
/**
* Set the message as seen
* @returns {Promise<Boolean>} result
*/
async sendSeen() {
return this.client.sendSeen(this.id._serialized);
}
/** /**
* Clears all messages from the chat * Clears all messages from the chat
* @returns {Promise<Boolean>} result * @returns {Promise<Boolean>} result

View File

@@ -10,6 +10,7 @@ exports.ExposeStore = (moduleRaidStr) => {
window.Store.Conn = window.mR.findModule('Conn')[0].default; window.Store.Conn = window.mR.findModule('Conn')[0].default;
window.Store.CryptoLib = window.mR.findModule('decryptE2EMedia')[0]; window.Store.CryptoLib = window.mR.findModule('decryptE2EMedia')[0];
window.Store.Wap = window.mR.findModule('Wap')[0].default; 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.SendClear = window.mR.findModule('sendClear')[0];
window.Store.SendDelete = window.mR.findModule('sendDelete')[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; 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'; throw 'The number provided is not a registered whatsapp user';
return result.jid; 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 = {}) => { window.WWebJS.sendMessage = async (chat, content, options = {}) => {
let attOptions = {}; let attOptions = {};
if (options.attachment) { if (options.attachment) {