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

Backup session to AWS or similar to prevent deletion #26

Open
matan-d opened this issue Dec 9, 2023 · 1 comment
Open

Backup session to AWS or similar to prevent deletion #26

matan-d opened this issue Dec 9, 2023 · 1 comment

Comments

@matan-d
Copy link

matan-d commented Dec 9, 2023

Hey,

Great Library and works perfect!

One issue, after I rebuild my Nodejs server all of the sessions get deleted.
Is this something that I can prevent?

I was thinking maybe to backup and restore the sessions to AWS S3.
Even got the saving part working
(currently im saving the entire WASocket object as I couldnt understand which part of it actually holds the token itself)

async function saveWhatsappSessionToAWS(sessionId, session) {
  console.log(`saveWhatsappSessionToAWS sessionId`, sessionId);
  console.log(`saveWhatsappSessionToAWS session`, session);

  // Convert the JSON object to a string
  const wasocketString = JSON.stringify(session);
  console.log(`saveWhatsappSessionToAWS wasocketString`, wasocketString);

  return new Promise(async (resolve, reject) => {
    const awsParams = {
      Body: wasocketString,
      Bucket: 'test',
      Key: `${sessionId}_wa_session`, // Modify Key structure as needed
    };

    // Create an S3 client
    const client = new S3Client({ region: 'us-east-1' });

    try {
      // Upload the session token content to S3
      await client.send(new PutObjectCommand(awsParams));
      console.log(`Session token for ${sessionId} uploaded to AWS S3.`);
      resolve(true)
    } catch (err) {
      console.error('Error uploading session token to S3:', err);
      resolve(false)
    }
  });
}

the problem is to retrieve it back:

async function loadWhatsappSessionsFromAWS(sessionId) {
  console.log(`loadWhatsappSessionsFromAWS sessionId`, sessionId);

  return new Promise(async (resolve, reject) => {
    const awsParams = {
      Bucket: 'test',
      Key: `${sessionId}_wa_session`, // Modify Key structure as needed
    };
    // Create an S3 client
    const client = new S3Client({ region: 'us-east-1' });

    try {
      // Retrieve the session token from S3
      const response = await client.send(new GetObjectCommand(awsParams));

      if (response.Body) {
        const sessionString = response.Body.toString('utf-8');
        console.log(`sessionString for ${sessionId} retrieved from AWS S3:\n`, sessionString);

        if (typeof sessionString === 'string' && sessionString.trim() !== '') {
          const session = JSON.parse(sessionString);
          console.log(`session for ${sessionId} retrieved from AWS S3:\n`, session);

          const waConnection = new WAConnection();
          waConnection.loadAuthInfo(session);
          console.log(`waConnection for ${sessionId} retrieved from AWS S3:\n`, waConnection);

          resolve(waConnection)
        } else {
          resolve(false)
        }
      } else {
        console.log(`Session token for ${sessionId} not found in AWS S3.`);
        resolve(false)
      }
    } catch (err) {
      console.error('Error retrieving session token from S3:', err);
      resolve(false)
    }
  });
}

the restore results in an error: "SyntaxError: Unexpected token o in JSON at position 1" because the WASocket probably gets corrupted while serializing..

any suggestions?

@gillangit
Copy link

or can we save session on mysql or mongodb?

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

2 participants