DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

pawan deore
pawan deore

Posted on

Whatsapp BOT with nodeJs

`
const puppeteer = require('puppeteer');

(async function(){

try{

    const browser = await puppeteer.launch({ headless:false })
    const page = await browser.newPage()

    await page.setUserAgent(
        "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
      );

    await page.goto("https://web.whatsapp.com/")
    await page.waitForSelector("._13NKt");
    await delay(5000)


    const contactName = "Corona Bot";
    await page.click(`span[title='${contactName}']`);
    await page.waitForSelector('.fd365im1')


    const amountOfMessages = 10;

    for(var i =0; i<amountOfMessages; i++){

        await page.type(".p3_M1", "Hi, how are you, I hope you are good!");
        delay(2000)
        await page.keyboard.press("Enter");
        await delay(500)
    }


}catch(err){
    console.log(err)
}
Enter fullscreen mode Exit fullscreen mode

})();

function delay(time){
return new Promise(function(resolve){
setTimeout(resolve, time)
})
}
`

Top comments (0)

11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!