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

Not loading session from remote #2

Open
viniciusbig opened this issue Jan 24, 2024 · 6 comments
Open

Not loading session from remote #2

viniciusbig opened this issue Jan 24, 2024 · 6 comments

Comments

@viniciusbig
Copy link

After a succesfull login, the zip file is saved on my S3.

When I restart my app, I can see the session file is downloaded from S3, but the whatsapp client is not logged, it keep asking for the QR Code.

2024-01-24T12:54:55.665Z [STORE_DEBUG] [METHOD: sessionExists] Triggered.
2024-01-24T12:54:56.067Z [STORE_DEBUG] [METHOD: sessionExists] File found. PATH='active/RemoteAuth-my-session.zip'.
2024-01-24T12:54:56.240Z [STORE_DEBUG] [METHOD: extract] Triggered.
2024-01-24T12:54:57.558Z [STORE_DEBUG] [METHOD: extract] File extracted. REMOTE_PATH='active/RemoteAuth-my-session.zip', LOCAL_PATH='RemoteAuth-my-session.zip'.

Is there anything missing to use the session saved in the bucket?

@arbisyarifudin
Copy link
Owner

Hello @viniciusbig , can you give more details about your code?

@viniciusbig
Copy link
Author

viniciusbig commented Jan 24, 2024

Sure!

here is the piece I think its relevant

const { Client, LocalAuth, RemoteAuth } = require('whatsapp-web.js');
const { AwsS3Store } = require('wwebjs-aws-s3');
const { S3Client, PutObjectCommand, HeadObjectCommand, GetObjectCommand, DeleteObjectCommand } = require('@aws-sdk/client-s3');

const s3 = new S3Client({
    region: process.env.AWS_REGION,
    credentials: {
        accessKeyId: process.env.AWS_ACCESS_KEY_ID,
        secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
    }
});
const putObjectCommand = PutObjectCommand;
const headObjectCommand = HeadObjectCommand;
const getObjectCommand = GetObjectCommand;
const deleteObjectCommand = DeleteObjectCommand;

const store = new AwsS3Store({
    bucketName: process.env.S3_BUCKET_NAME,
    remoteDataPath: 'active/',
    s3Client: s3,
    putObjectCommand,
    headObjectCommand,
    getObjectCommand,
    deleteObjectCommand
});

const client = new Client({
    authStrategy: new RemoteAuth({
        clientId: 'my-session',
        dataPath: './.wwebjs_auth',
        store: store,
        backupSyncIntervalMs: 600000
    }),
    restartOnAuthFail: true, // related problem solution
    puppeteer: { 
        headless: true,
        // executablePath: '/usr/bin/google-chrome-stable', // <- uncomment here to work on AWS Beanstalk
        args: [
            '--no-sandbox',
            '--disable-setuid-sandbox',
            '--disable-dev-shm-usage',
            '--disable-accelerated-2d-canvas',
            '--no-first-run',
            '--no-zygote',
            '--single-process', // <- this one doesn't works in Windows
            '--disable-gpu'
        ]
    }
});

client.initialize();

client is initialized and the qr event is triggered.
I'm running locally, in a MacOs M1

The file is correct downloaded and extracted. But it seems its being ignored by the client

@henricker
Copy link

henricker commented Feb 8, 2024

Same problem here
I successfully saved my session to my bucket
But when loading my already saved session, it doesn't find:

image

  const s3 = new S3Client({
      region: config.s3.region,
      credentials: {
          accessKeyId: config.s3.accessKeyId,
          secretAccessKey: config.s3.secretAccessKey
      },
  })
  const putObjectCommand = PutObjectCommand; 
  const headObjectCommand = HeadObjectCommand;
  const getObjectCommand = GetObjectCommand;
  const deleteObjectCommand = DeleteObjectCommand; 
  const store = new AwsS3Store({
      bucketName: config.s3.bucket,
      remoteDataPath: 'whatsapp_sessions/',
      s3Client: s3,
      putObjectCommand,
      headObjectCommand,
      getObjectCommand,
      deleteObjectCommand
    });
    
  const authStrategy = new RemoteAuth({
      backupSyncIntervalMs: 300000,
      store,
      clientId,
      dataPath: "./.wwwebjs_auth"
  })
  const client = new Client({
    authStrategy: authStrategy,
    qrMaxRetries: 5,
    puppeteer: {
      headless: true,
      args: ['--no-sandbox']
    }
  })
  return client

Environment: ubuntu 20-04

@rbgadotti
Copy link

Same here

2024-02-10T21:20:30.977Z [STORE_DEBUG] [METHOD: sessionExists] Triggered.
2024-02-10T21:20:31.740Z [STORE_DEBUG] [METHOD: sessionExists] File found. PATH='sessions/RemoteAuth-yourSessionName3.zip'.
2024-02-10T21:20:31.746Z [STORE_DEBUG] [METHOD: extract] Triggered.
2024-02-10T21:20:37.848Z [STORE_DEBUG] [METHOD: extract] File extracted. REMOTE_PATH='sessions/RemoteAuth-yourSessionName3.zip', LOCAL_PATH='RemoteAuth-yourSessionName3.zip'.

@henricker
Copy link

Updates:
I was created a custom remote auth and works fine:
Basically s3 makes download of session, but the RemoteAuth cannot get the whole path, so in my CustomRemoteAuth i "merge" the dataPath with sessionDirName.

I hope help you!.

import AdmZip from "adm-zip"
import * as fs from "fs-extra"
import * as path from "path"
import WAWebJS, { RemoteAuth } from "whatsapp-web.js"

interface RemoteAuthOptions {
    clientId?: string
    dataPath?: string
    store: WAWebJS.Store
    backupSyncIntervalMs: number
  }

export class CustomRemoteAuth extends RemoteAuth {

    private userDataDir: string

    constructor(options: RemoteAuthOptions) {
        super(options)
        const sessionDirName = options.clientId ? `RemoteAuth-${options.clientId}` : 'RemoteAuth'
        this.userDataDir = path.join(options?.dataPath ?? '', sessionDirName)
    }

    async unCompressSession(compressedSessionPath: string): Promise<void> {
        await new Promise((resolve, reject) => {
            const zip = new AdmZip(compressedSessionPath)
            zip.extractAllToAsync(this.userDataDir, true, false, (err) => {
                console.log("#######", this.userDataDir, compressedSessionPath)
                if(err)
                { reject(err) }
                else
                { resolve(null) }
            })
        })
        await fs.promises.unlink(compressedSessionPath)
    }
}

@rbgadotti
Copy link

rbgadotti commented Feb 12, 2024

It's a whatsapp-web.js issue

Solved using that pr: pedroslopez/whatsapp-web.js#2412

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

No branches or pull requests

4 participants