wbm icon indicating copy to clipboard operation
wbm copied to clipboard

Can be upgraded for sending messages in Whatsapp groups

Open manankarani opened this issue 3 years ago • 2 comments

Hey, I found this API very interesting, I am a newbie to node.js, but I was wondering if there was a way to send Messages from a saved contact or a Whatsapp Group. Regards,

manankarani avatar Jul 06 '21 16:07 manankarani

Yes there are ways to do it, wbm uses puppeteer(web automation library). So in background wbm access whatsapp web in a browser and do all the steps to send a message.

  • access https://​api.whatsapp.com/send?phone={number}&text={message}
  • check authentication(QR Code appeared?)
  • send message(automatically type the message and press enter)

In these steps you can add a step to search for a user on the whatsapp web search contact field.

  • access https://​api.whatsapp.com/send?phone={number}&text={message}
  • check authentication(QR Code appeared?)
  • search contact(click on search bar and type the group/contact name)
  • select contact(click on contact)
  • send message(automatically type the message and press enter)

I suggest you to have some fun with puppeteer to get the idea. Hope it helped you!

Briuor avatar Jul 14 '21 00:07 Briuor

Hey, here is an example it is just using the open contacts on the left but it should get you the idea you have to replace the function inside the api.js

async function sendTo(phoneOrContact, message) {
    let phone = phoneOrContact;
    if (typeof phoneOrContact === "object") {
        phone = phoneOrContact.phone;
        message = generateCustomMessage(phoneOrContact, message);
    }
    try {
        process.stdout.write("Sending Message...\r");
        // await page.waitForSelector(SELECTORS.LOADING, { hidden: true, timeout: 60000 });

        await page.waitForXPath(`//*[contains(text(), '${phone}')]`, { visible: true }); //Search input
        const elementToClick = await page.$x(`//*[contains(text(), '${phone}')]`);
        await elementToClick[0].click();


        await page.waitForXPath(`//div[@title='Type a message']`, { visible: true }); //Search input
        const elementToClick2 = await page.$x(`//div[@title='Type a message']`);
        await elementToClick2[0].click();

        await page.keyboard.type(message);
        await page.keyboard.press("Enter");
        await page.waitFor(1000);
        process.stdout.clearLine();
        process.stdout.cursorTo(0);
        process.stdout.write(`${phone} Sent\n`);
        counter.success++;
    } catch (err) {
        process.stdout.clearLine();
        process.stdout.cursorTo(0);
        process.stdout.write(`${phone} Failed\n`);
        counter.fails++;
    }
}

asdat3 avatar Aug 17 '22 14:08 asdat3