Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: group mentions + improved user mentions #2602

Merged
merged 20 commits into from
Jan 11, 2024

Conversation

alechkos
Copy link
Collaborator

@alechkos alechkos commented Oct 24, 2023

Table of Contents

- Description

- Usage Example

- I Want to Test this PR

- I Got an Error While Testing This PR ❌

- How Has the PR Been Tested (latest test on 04.12.2023)

- Types of Changes


Description

This PR introduces logic for mentioning groups in messages and improves the logic for mentioning users:

  1. Group mentions:
    • Added option to send a message with clickable group mentions, and when the group mention is clicked, a chat with the mentioned group will be opened.

      NOTE:
      The user that does not participate in the mentioned group,
      will not be able to open that mentioned group,
      the same if the group does not exist

    • Added Message.getGroupMentions method to get an array of GroupChat objects from a message with group mentions
  2. User mentions:
    • Added logic for mentioning those users with whom you have never interacted
    • Added a check to verify the existence of the user, preventing accidental mentions of unregistered phone numbers on WhatsApp

Usage Example

client.on('message', async (msg) => {
    const chat = await msg.getChat();
    const userNumber = '26022602XX';

    if (msg.body === '!mentionUsers') {
        /**
         * To mention one user you can pass user's ID to 'mentions' property as is,
         * without wrapping it in Array, and a user's phone number to the message body:
         */
        await chat.sendMessage(`Hi @${userNumber}`, {
            mentions: userNumber + '@c.us'
        });

        // To mention a list of users:
        await chat.sendMessage(`Hi @${userNumber}, @${userNumber}`, {
            mentions: [userNumber + '@c.us', userNumber + '@c.us']
        });
    }
    
    if (msg.body === '!mentionGroups') {
        const chat = await msg.getChat();
        const groupId = 'YYYYYYYYYY@g.us';
        /**
         * Sends clickable group mentions, the same as user mentions.
         * When the mentions are clicked, it opens a chat with the mentioned group.
         * The 'groupMentions.subject' can be custom
         * 
         * @note The user that does not participate in the mentioned group,
         * will not be able to click on that mentioned group, the same if the group does not exist
         *
         * To mention one group:
         */
        await chat.sendMessage(`Check the last message here: @${groupId}`, {
            groupMentions: { subject: 'GroupSubject', id: groupId }
        });

        // To mention a list of groups:
        await chat.sendMessage(`Check the last message in these groups: @${groupId}, @${groupId}`, {
            groupMentions: [
                { subject: 'FirstGroup', id: groupId },
                { subject: 'SecondGroup', id: groupId }
            ]
        });
    }

    if (msg.body === '!getGroupMentions') {
        // To get group mentions from a message:
        const groupId = 'ZZZZZZZZZZ@g.us';
        const msg = await client.sendMessage('chatId', `Check the last message here: @${groupId}`, {
            groupMentions: { subject: 'GroupSubject', id: groupId }
        });

        /** {@link groupMentions} is an array of `GroupChat` */
        const groupMentions = await msg.getGroupMentions();
    }
});

To test this PR by yourself you can run one of the following commands:

  • NPM
npm install github:alechkos/whatsapp-web.js#group-mentions
  • YARN
yarn add github:alechkos/whatsapp-web.js#group-mentions

If you encounter any errors while testing this PR, please provide in a comment:

  1. The code you've used without any sensitive information (use syntax highlighting for more readability)
  2. The error you got
  3. The library version
  4. The WWeb version: console.log(await client.getWWebVersion());
  5. The browser (Chrome/Chromium)

Important

You have to reapply the PR each time it is changed (new commits were pushed since your last application)


How Has The PR Been Tested (latest test on 04.12.2023)

Tested with a code provided in usage example.

Tested On:

Types of accounts:

  • Personal
  • Buisness

Environment:

  • Android 10:
    • WhatsApp: latest
    • WA Business: latest
  • Windows 10:
    • WWebJS: v1.23.1-alpha.0
    • WWeb: v2.2353.0
    • Puppeteer: v18.2.1
    • Node: v18.17.1
    • Chrome: latest

Types of Changes

  • Dependency change
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix/feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project
  • I have updated the usage example accordingly (example.js)
  • I have updated the documentation accordingly (index.d.ts)

Sorry, something went wrong.

alechkos and others added 4 commits October 25, 2023 01:57

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.

Verified

This commit was signed with the committer’s verified signature.
adamjstewart Adam J. Stewart

Verified

This commit was signed with the committer’s verified signature.
adamjstewart Adam J. Stewart
@alechkos alechkos requested a review from shirser121 November 12, 2023 17:46
@alechkos alechkos mentioned this pull request Dec 3, 2023
7 tasks
@crash-overdrive
Copy link

crash-overdrive commented Dec 3, 2023

Using TypeScript, I can't use groupMentions as a MessageSendOption while calling the function client.SendMessage. Line 802-817 in Client.js should contain property groupMentions to fix this (I think). @alechkos

@alechkos
Copy link
Collaborator Author

alechkos commented Dec 3, 2023

@crash-overdrive

Line 802-817 in Client.js should contain property groupMentions to fix this

You are right, I forgot to add this
Will be fixed

@crash-overdrive
Copy link

@crash-overdrive

Line 802-817 in Client.js should contain property groupMentions to fix this

You are right, I forgot to add this Will be fixed

Lemme know when you plan on fixing this! Using it for a project of mine :D

@alechkos
Copy link
Collaborator Author

alechkos commented Dec 3, 2023

@crash-overdrive
Fixed, let me know if there are another issues

@alechkos alechkos enabled auto-merge (squash) December 13, 2023 15:35
@alechkos alechkos requested review from aliyss and tuyuribr December 13, 2023 15:35
@shavy4452
Copy link

is this ready for merge? I am waiting for this to include in my project.

@alechkos
Copy link
Collaborator Author

alechkos commented Jan 9, 2024

@shavy4452

is this ready for merge?

Yes it is

Copy link
Collaborator

@shirser121 shirser121 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@alechkos alechkos merged commit e0d955f into pedroslopez:main Jan 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants