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

fix: link retrieval from message #2716

Merged
merged 7 commits into from
Jan 14, 2024
Merged

Conversation

alechkos
Copy link
Collaborator

@alechkos alechkos commented Jan 11, 2024

Table of Contents

- Description

- Related Issue

- 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 14.01.2024)

- Types of Changes


Description

The PR fixes link retrieval from a message if it contains any.
Fixed both for outcoming and incoming messages.


Related Issue

The PR closes #1766


Usage Example

// client initialization...

client.on('ready', (message) => {
    const chatId = 'XXXXXXXXXX@c.us';

    const noLinks = await client.sendMessage(chatId, 'message without links');
    /**
     * Message {
     *     _data: {
     *       ...
     *       links: []
     *     },
     *     ...
     *     links: []
     *   }
     */
    console.log(noLinks);

    const withLinks = await client.sendMessage(chatId, 'bit.ly');
    /**
     * Message {
     *     _data: {
     *       ...
     *       links: [ { link: 'http://bit.ly', isSuspicious: false } ]
     *     },
     *     ...
     *     links: [ { link: 'http://bit.ly', isSuspicious: false } ]
     *   }
     */
    console.log(withLinks);
});

client.on('message', (message) => {
    /**
     * Let's say the 'message.body' is as follows:
     * "Hey there! These are some links: https://fb.com, fb.com, bit.ly, http://instagram.com", then
     * the output will be as follows:
     * [
     *   { link: 'https://fb.com', isSuspicious: false },
     *   { link: 'http://fb.com', isSuspicious: false },
     *   { link: 'http://bit.ly', isSuspicious: false },
     *   { link: 'http://instagram.com', isSuspicious: false }
     * ]
     */
    console.log(message.links);
});

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

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

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 14.01.2024)

Tested with the code provided in the usage example, both with text and media messages that have links and without.

Tested On:

Types of accounts:

  • Personal
  • Buisness

Environment:

  • Android 10:
    • WhatsApp: latest
    • WA Business: latest
  • Windows 10:
    • WWebJS: v1.23.1-alpha.3
    • WWeb: v2.2402.5
    • 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 (feature/fix 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 alechkos added the breaking change Fix or feature that would cause existing functionality to change label Jan 11, 2024
@shirser121
Copy link
Collaborator

I think we should keep the object as it was to not break the code

@alechkos alechkos removed the breaking change Fix or feature that would cause existing functionality to change label Jan 11, 2024
@alechkos
Copy link
Collaborator Author

@alechkos alechkos enabled auto-merge (squash) January 11, 2024 11:00
@alechkos alechkos requested a review from tuyuribr January 12, 2024 02:46
@shirser121
Copy link
Collaborator

Is that support old node version?
Also, i think that if its a media, its just wasted CPU to looking for links over the base64 preview

@alechkos
Copy link
Collaborator Author

alechkos commented Jan 14, 2024

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.

Thanks

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.

msg.links is empty when from .on("message") events
3 participants