open-wa / wa-automate-nodejs

💬 🤖 The most reliable tool for chatbots with advanced features. Be sure to 🌟 this repository for updates!
https://docs.openwa.dev/
Other
3.12k stars 590 forks source link

Bug: client.refresh() not working. #2291

Closed siddiquiaffan closed 2 years ago

siddiquiaffan commented 2 years ago

Are you using the latest version of the library?

Current Behavior

I'm trying to refresh my client using client.refresh() but it's not working and throwing some error.

The error I got is:

- session_REFRESH
- Refreshing session
- Opening session in new tab
- Waiting for conflict to close stale tab...
- Checking if fresh session is authenticated...
- Injected new session...
- Downloading patches from https://cdn.openwa.dev/patches.json
- Generating patch hash
- Downloaded patches in 0.563s
- Installing patches
(node:19) UnhandledPromiseRejectionWarning: Error: Evaluation failed: TypeError: Cannot read properties of undefined (reading 'sendMessage')
    at __puppeteer_evaluation_script__:1:117221
    at __puppeteer_evaluation_script__:1:450370
    at ExecutionContext._evaluateInternal (/app/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:175:23)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async ExecutionContext.evaluate (/app/node_modules/puppeteer/lib/cjs/puppeteer/common/ExecutionContext.js:110:16)
    at async Promise.all (index 0)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:19) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:19) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Expected Behavior

It should refresh the client.

Steps To Reproduce

In my project there are two bots working simultaneously. i) WhatsApp Bot (wa-automate-nodejs) ii) Telegram Bot( gramjs)

I've added a command to to telegram bot named /restart to refresh the client. When user hits the /retsart command a function named restart is called.

Restart Function:

async function restart() {
        log('Restarting WhatsApp Client');
        await client.refresh();
        return {status: 'ok'}
}

Mode

My own code

create() code

const client = await wa.create({useChrome: true, headless: true, authTimeout: 0, chromiumArgs: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-gpu']});

DEBUG INFO

{
    WA_VERSION: '2.2144.10',
    PAGE_UA: 'WhatsApp/2.2108.8 Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36',
    WA_AUTOMATE_VERSION: '4.23.7',
    BROWSER_VERSION: 'HeadlessChrome/93.0.4577.63',
    OS: 'Linux 4.9,
    START_TS: 1636782632653
}

Environment

- OS: Deployed on railway.app
- Node: 14
- npm:

Screenshots

No response

Anything else?

No response

smashah commented 2 years ago

I have a feeling this is related to wapi being injected way too early

Might need to reimplement ripe session detection

https://docs.openwa.dev/interfaces/api_model_config.ConfigObject.html#waitForRipeSession

siddiquiaffan commented 2 years ago

Using waitForRipeSession can reduce this issue?

And yeah it's WAPI issue cuz once I got error something like WAPI undefined

siddiquiaffan commented 2 years ago

So what I need to do basically to solve this issue?

I have a feeling this is related to wapi being injected way too early Might need to reimplement ripe session detection https://docs.openwa.dev/interfaces/api_model_config.ConfigObject.html#waitForRipeSession

siddiquiaffan commented 2 years ago

Any updates?

smashah commented 2 years ago

@github-actions run

⚡ Release! ⚡ ```js (async () => { function exec(cmd) { console.log(execSync(cmd).toString()); } //set the version type process.env.VERS = "patch" // Config const gitUserEmail = "github-actions[bot]@users.noreply.github.com"; const gitUserName = "github-actions[bot]"; exec(`echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc`); exec(`git config --global user.email "${gitUserEmail}"`); exec(`git config --global user.name "${gitUserName}"`); exec(`npm i -D`); exec(`npm run release-ci $VERS`); // types only package exec('npm config set //registry.npmjs.org/:_authToken $NPM_TOKEN') exec(`cd ./types-only && npm version $VERS && npm run build && npm publish && cd ..`); exec(`git commit -a -m 'updated types-only package'`); exec(`git push --force`); //comment on the issue var result = execSync(`npx auto-changelog -o ./tempchangelog.txt --commit-limit false --template ./compact-keepachangelog.hbs --stdout`).toString(); await postComment(result); //create changelog image exec(`npm run release-image`); exec(`git commit -a -m 'updated release-image'`); exec(`git push --force`); })(); ```
smashah commented 2 years ago

Changelog

🚀 Release 4.23.11 (2021-11-16)

smashah commented 2 years ago

@AffanTheBest update to the latest version and try again thanks

smashah commented 2 years ago

@AffanTheBest to be honest I am unable to replicate this locally. Please add more context to this issue + try to replicate it locally with the easy api.

siddiquiaffan commented 2 years ago

It's still throwing same error. Even on my local machine.

See below SS:

Logs of restart function starts here

smashah commented 2 years ago

@AffanTheBest

  1. Run npx @open-wa/wa-automate --headful
  2. Open the api explorer and execute a post request to the /refresh endpoint.
  3. If the issue continues then make a screen recording of the process
siddiquiaffan commented 2 years ago

Nope I didn't get any error when used api

smashah commented 2 years ago

@AffanTheBest ok then it seems to be related to your implementation.

Please add more context (e.g the config) to this issue.

When you add the necessary context to replicate, I will reopen the issue

smashah commented 2 years ago

2312

This is fixed in 4.23.17

thanks

siddiquiaffan commented 2 years ago

Woah thanks!