|
| 1 | +--- |
| 2 | +lang: en-US |
| 3 | +title: Mentions |
| 4 | +description: How to get and send messages with mentions |
| 5 | +--- |
| 6 | + |
| 7 | +# {{ $frontmatter.title }} |
| 8 | + |
| 9 | +With the help of the library, you can mention WhatsApp users and groups you are participating in. |
| 10 | + |
| 11 | +## Mentioning Users |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +:::tip |
| 16 | +It is possible to mention also those users who are not in your contact list. |
| 17 | +::: |
| 18 | + |
| 19 | +### Getting Mentioned Users |
| 20 | + |
| 21 | +You can get all users that have been mentioned in a message by using `getMentions` method of a `Message` object. This will conveniently return a list of [`Contact`](https://docs.wwebjs.dev/Contact.html) objects: |
| 22 | + |
| 23 | +```javascript |
| 24 | +// client initialization... |
| 25 | + |
| 26 | +client.on('message', async (msg) => { |
| 27 | + const mentions = await msg.getMentions(); |
| 28 | + |
| 29 | + for (let user of mentions) { |
| 30 | + console.log(`${user.pushname} was mentioned`); |
| 31 | + } |
| 32 | +}); |
| 33 | +``` |
| 34 | + |
| 35 | +This is just a helper function for parsing the `mentionedIds` array available on messages. This just contains a list of user IDs, so you can use this instead if you don't intend to do anything like getting their name or accessing any properties on their `Contact`. |
| 36 | + |
| 37 | +### Sending Messages with User Mentions |
| 38 | + |
| 39 | +You can mention other user by using the `mentions` option when sending a message. Note that the message text needs to also reference mentioned users by using the format `@[phone number]` **without a '+' at the beginning of a phone number**: |
| 40 | + |
| 41 | +```javascript |
| 42 | +// client initialization... |
| 43 | + |
| 44 | +client.on('message', async (msg) => { |
| 45 | + const chat = await msg.getChat(); |
| 46 | + let user = await msg.getContact(); |
| 47 | + await chat.sendMessage(`Hello @${user.id.user}`, { |
| 48 | + mentions: [user] |
| 49 | + }); |
| 50 | + |
| 51 | + // OR |
| 52 | + |
| 53 | + let userPhone = '123456789'; |
| 54 | + await chat.sendMessage(`Hello @${userPhone}`, { |
| 55 | + mentions: [userPhone + 'c.us'] |
| 56 | + }); |
| 57 | +}); |
| 58 | +``` |
| 59 | + |
| 60 | +:::tip |
| 61 | +You can mention users in a message without explicitly referencing them by using the format `@[phone number]` in a message body. Those users will still be mentioned but silently; they won't see their mentioned nicknames in a message body but will still be pinged. |
| 62 | +::: |
| 63 | + |
| 64 | +#### Example of Mentioning All Group Members |
| 65 | + |
| 66 | +The following is a simple command that mentions all users in a group if someone sends a `!everyone` message: |
| 67 | + |
| 68 | +```javascript |
| 69 | +// client initialization... |
| 70 | + |
| 71 | +client.on('message', async (msg) => { |
| 72 | + if (msg.body === '!everyone') { |
| 73 | + const chat = await msg.getChat(); |
| 74 | + |
| 75 | + let text = ''; |
| 76 | + let mentions = []; |
| 77 | + |
| 78 | + for (let participant of chat.participants) { |
| 79 | + mentions.push(`${participant.id.user}@c.us`); |
| 80 | + text += `@${participant.id.user} `; |
| 81 | + } |
| 82 | + |
| 83 | + await chat.sendMessage(text, { mentions }); |
| 84 | + } |
| 85 | +}); |
| 86 | +``` |
| 87 | + |
| 88 | +## Mentioning Groups |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +:::tip |
| 93 | +As a name of a group to mention, you can provide **your custom group name** (like in the image above). It aslo can be an original group name, this is for your choice. |
| 94 | +::: |
| 95 | + |
| 96 | +### Getting Mentioned Groups |
| 97 | + |
| 98 | +You can get all groups that have been mentioned in a message by using `getGroupMentions` method of a `Message` object. This will conveniently return a list of [groups](https://docs.wwebjs.dev/GroupChat.html): |
| 99 | + |
| 100 | +```javascript |
| 101 | +// client initialization... |
| 102 | + |
| 103 | +client.on('message', async (msg) => { |
| 104 | + const group_mentions = await msg.getGroupMentions(); |
| 105 | + |
| 106 | + for (const group of group_mentions) { |
| 107 | + console.log(`Group ${group.name} with an ID ${group.id._serialized} was mentioned`); |
| 108 | + } |
| 109 | +}); |
| 110 | +``` |
| 111 | + |
| 112 | +This is just a helper function that simply prints group names mentioned in the message along with their IDs for demonstration. |
| 113 | + |
| 114 | +### Sending Messages with Group Mentions |
| 115 | + |
| 116 | +You can send a message with clickable group mentions, and similar to [user mentions](./mentions.md#mentioning-users), when the group mention is tapped, a chat with that mentioned group will be opened. |
| 117 | + |
| 118 | +:::warning IMPORTANT |
| 119 | +Users who do not participate in the mentioned group, will not be able to get that group opened by tapping on its mention, the same when the group does not exist. |
| 120 | +::: |
| 121 | + |
| 122 | +```javascript |
| 123 | +// client initialization... |
| 124 | + |
| 125 | +client.on('message', async (msg) => { |
| 126 | + if (msg.body === '!mentionGroups') { |
| 127 | + const chat = await msg.getChat(); // defining chat to send the group mention to |
| 128 | + const groupId = 'YYYYYYYYYY@g.us'; // defining an ID of a group to mention |
| 129 | + |
| 130 | + // To mention one group: |
| 131 | + await chat.sendMessage( |
| 132 | + `@${groupId}`, |
| 133 | + { groupMentions: { subject: 'Your Group Name Here', id: groupId } } |
| 134 | + ); |
| 135 | + |
| 136 | + // To mention a list of groups: |
| 137 | + const anotherGrpId = 'XXXXXXXXXX@g.us'; // defining another ID of a group to mention |
| 138 | + await chat.sendMessage( |
| 139 | + `Here can be your custom text... @${groupId}, @${anotherGrpId}`, { |
| 140 | + groupMentions: [ |
| 141 | + { subject: 'Some Group Name Of Your Choice', id: groupId }, |
| 142 | + { subject: 'Some Another Group Name', id: anotherGrpId } |
| 143 | + ] |
| 144 | + }); |
| 145 | + } |
| 146 | +}); |
| 147 | +``` |
0 commit comments