question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Session saving problem

See original GitHub issue

Description

The session is created, but when the script is restarted, the session does not rise and asks to rescanQr

Environment

  • Venom version(s): [4.0.5]
  • Browser: [default venom]
  • OS: [linux ubuntu 20.04]
  • Node version: [Node 12.22.5]

Steps to Reproduce

  1. [Session creation]
  2. [Reboot script]

Log Output

Session creation

info:     [79297401290] Initializing browser wss...
info:     [79297401290] Browser successfully opened
info:     [79297401290] checking headless...
info:     [79297401290] headless option is active, browser hidden
info:     [79297401290] Checking page...
info:     [79297401290] Page successfully accessed
- [instance: 79297401290]: Waiting page load
- [instance: 79297401290]: Checking is logged...
- [instance: 79297401290]: Waiting for QRCode Scan...
- [instance: 79297401290]: Waiting for QRCode Scan: Attempt 1
- [instance: 79297401290]: Waiting for QRCode Scan: Attempt 2
- [instance: 79297401290]: Checking QRCode status...
- [instance: 79297401290]: QRCode Success
- [instance: 79297401290]: Checking phone is connected...
- [instance: 79297401290]: Connected

Reboot script

info:     [79297401290] Initializing browser wss...
info:     [79297401290] Browser successfully opened
info:     [79297401290] checking headless...
info:     [79297401290] headless option is active, browser hidden
info:     [79297401290] Checking page...
info:     [79297401290] Page successfully accessed
- [instance: 79297401290]: Waiting page load
- [instance: 79297401290]: Checking is logged...
- [instance: 79297401290]: Waiting for QRCode Scan...
- [instance: 79297401290]: Waiting for QRCode Scan: Attempt 1
- [instance: 79297401290]: Page Closed
- [instance: 79297401290]: Checking QRCode status...
- [instance: 79297401290]: Failed to authenticate
- [instance: 79297401290]: Unknow error
If relevant, paste all of your Log Output

Your Code

const venom = require('venom-bot');

venom
  .create({
    session: 'session-name', //name of session
    multidevice: false // for version not multidevice use false.(default: true)
  })
  .then((client) => start(client))
  .catch((erro) => {
    console.log(erro);
  });

function start(client) {
  client.onMessage((message) => {
    if (message.body === 'Hi' && message.isGroupMsg === false) {
      client
        .sendText(message.from, 'Welcome Venom 🕷')
        .then((result) => {
          console.log('Result: ', result); //return object success
        })
        .catch((erro) => {
          console.error('Error when sending: ', erro); //return object error
        });
    }
  });
}

Additional context / Screenshot

Add any other context about the problem here. If applicable, add screenshots to help explain.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:17 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
r3rcommented, Feb 14, 2022

For multi device beta, the following config created the tokens for me automatically:

const client = await create({
        session: 'wa-session', //name of session
        multidevice: true, // for version not multidevice use false.(default: true)
        folderNameToken: 'tokens', //folder name when saving tokens
        mkdirFolderToken: './data/app', //folder name when saving tokens
        createPathFileToken: true})

Setting folderNameToken, mkdirFolderToken and createPathFileToken were the magic config that created a large session specific token folder that was reused when restarting the server.

For non multi device, you have to save it manually using client.getSessionTokenBrowser() and then read it back and pass it as an option to create as browserSessionToken.

The following works for both multi device and non:

import { create, Whatsapp } from 'venom-bot';
import fs from 'fs';

const TOKEN_DIR = "./data/app/tokens";
const TOKEN_PATH: string = TOKEN_DIR + "/wa-session.data.json";

export default async function createWhatsappClient(): Promise<Whatsapp> {
    var browserSessionToken: any = null;
    if (fs.existsSync(TOKEN_PATH)) {
        const savedTokenString: string = fs.readFileSync(TOKEN_PATH).toString();
        browserSessionToken = JSON.parse(savedTokenString);
    }
    const client = await create({
        session: 'wa-session', //name of session
        multidevice: true, // for version not multidevice use false.(default: true)
        folderNameToken: 'tokens', //folder name when saving tokens
        mkdirFolderToken: './data/app', //folder name when saving tokens
        createPathFileToken: true,
        disableWelcome: true,
        disableSpins: true,
        headless: true,
        browserSessionToken: browserSessionToken,
    });
    const token: any = await client.getSessionTokenBrowser();
    await fs.promises.mkdir(TOKEN_DIR, { recursive: true });
    await fs.promises.writeFile(TOKEN_PATH, JSON.stringify(token));
    return client;
}

3reactions
jrClebercommented, Feb 9, 2022

you can save the session token manually. See here in this script! The comments are in Portuguese, but you can translate the page. OK!?

https://github.com/jrCleber/bot-venom-firebase/blob/main/app/bot.ts

Project developers rarely help us.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PHP Session not Saving - Stack Overflow
session_save_path is defined in php.ini file. Probably in your testing server it is a folder writable by apache but in your server it...
Read more >
Fix: PHP sessions not saving. - This Interests Me
A short guide on how to solve the issue of PHP session data not saving. ... guide on the common problems that could...
Read more >
PHP: My session variables are not being saved
Since the problem is about the session not being saved, the solution is to save the session before redirecting. How to do that?...
Read more >
Problem with saving session - www.neuron.yale.edu
When I first installed Neuron on my laptop with Ubuntu I could save my session, and I could kind of load it, with...
Read more >
Firefox doesn't save my session - Mozilla Support
8 replies; 1 has this problem; 1238 views; Last reply by Alex ... hi dr... save a Firefox session, If you are using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found