mimamch / wa-multi-session

Multiple Session Whatsapp Socket Javascript Library
https://www.npmjs.com/package/wa-multi-session
MIT License
114 stars 37 forks source link

Backup session to AWS or similar to prevent deletion #26

Open matan-d opened 7 months ago

matan-d commented 7 months ago

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 commented 3 months ago

or can we save session on mysql or mongodb?