From 56166138079053f9179a57ed47981234d802d190 Mon Sep 17 00:00:00 2001 From: Pedro Lopez Date: Sat, 8 Feb 2020 03:19:51 -0400 Subject: [PATCH] feat: Mention users, get user number from Contact model --- README.md | 2 +- example.js | 6 ++++++ src/Client.js | 3 ++- src/structures/Contact.js | 6 ++++++ src/util/Injected.js | 3 +++ 5 files changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 545da52..ad85d0f 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ Take a look at [example.js](https://github.com/pedroslopez/whatsapp-web.js/blob/ | Add group participants | ✅ | | Kick group participants | ✅ | | Promote/demote group participants | ✅ | -| Mention users | _pending_ | +| Mention users | ✅ | | Get contact info | ✅ | | Get profile pictures | ✅ | | Set user status message | ✅ | diff --git a/example.js b/example.js index 7251785..8677ac7 100644 --- a/example.js +++ b/example.js @@ -131,6 +131,12 @@ client.on('message', async msg => { const newStatus = msg.body.split(' ')[1]; await client.setStatus(newStatus); msg.reply(`Status was updated to *${newStatus}*`); + } else if (msg.body == '!mention') { + const contact = await msg.getContact(); + const chat = await msg.getChat(); + chat.sendMessage(`Hi @${contact.number}!`, { + mentions: [contact] + }); } }); diff --git a/src/Client.js b/src/Client.js index 732f94e..7f0aa82 100644 --- a/src/Client.js +++ b/src/Client.js @@ -247,7 +247,8 @@ class Client extends EventEmitter { async sendMessage(chatId, content, options={}) { let internalOptions = { caption: options.caption, - quotedMessageId: options.quotedMessageId + quotedMessageId: options.quotedMessageId, + mentionedJidList: Array.isArray(options.mentions) ? options.mentions.map(contact => contact.id._serialized) : [] }; if(content instanceof MessageMedia) { diff --git a/src/structures/Contact.js b/src/structures/Contact.js index 7cd7dee..60fd3b7 100644 --- a/src/structures/Contact.js +++ b/src/structures/Contact.js @@ -20,6 +20,12 @@ class Contact extends Base { */ this.id = data.id; + /** + * Contact's phone number + * @type {string} + */ + this.number = data.userid; + /** * Indicates if the contact is a business contact * @type {boolean} diff --git a/src/util/Injected.js b/src/util/Injected.js index 99d0be1..a4e97ac 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -40,6 +40,8 @@ exports.LoadUtils = () => { delete options.quotedMessageId; } + options.mentionedJidList = options.mentionedJidList.map(cId=> window.Store.Contact.get(cId).id); + let locationOptions = {}; if (options.location) { locationOptions = { @@ -145,6 +147,7 @@ exports.LoadUtils = () => { res.isGroup = contact.isGroup; res.isWAContact = contact.isWAContact; res.isMyContact = contact.isMyContact; + res.userid = contact.userid; return res; };