0

there are lots of advices on QR codes but I'm unable to find answers to my own problem. So I'm posting this question.

I'm trying to use Whatsapp-web.js, a WhatsApp client library for NodeJS that connects through the WhatsApp Web browser app. Below is the code.

const fs = require('fs');
const { Client } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');

// Path where the session data will be stored
const SESSION_FILE_PATH = './session.json';

// Load the session data if it has been previously saved
let sessionData;
if(fs.existsSync(SESSION_FILE_PATH)) {
    sessionData = require(SESSION_FILE_PATH);
}

// Use the saved values
const client = new Client({
    session: sessionData
});

// Save session values to the file upon successful auth
client.on('authenticated', (session) => {
    sessionData = session;
    fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), (err) => {
        if (err) {
            console.error(err);
        }
    });
});

client.on('qr', qr => {
    console.log(qr);
    qrcode.generate(qr, {small: true});
});

client.on('ready', () => {
    console.log('Client is ready!');
   
     // Number where you want to send the message.
    const number = "+911234567890";
   
     // Your message.
    const text = "Hey john";
   
     // Getting chatId from the number.
     // we have to delete "+" from the beginning and add "@c.us" at the end of the number.
    const chatId = number.substring(1) + "@c.us";
   
    // Sending message.
    client.sendMessage(chatId, text);
});

client.on('message', message => {
    console.log(message.body);
});

client.initialize();

The problem is that the QR code generated by the Whatsapp client is not recognized by my phone. I wondered if the QR image generator was wrong, so I logged the QR string and pasted it into a QR image generating tool to see if the images were identical. To me the images looked different. But I don't know if that's the problem at all.

I wish to know if there is an additional step I have overlooked before I can scan the QR image with my phone. Currently, I cannot login because phone doesn't recognize the QR image.

Edit

The QR code string is: 1@vFeiIVji3KTIwmxuzrWThEtLYAx1toBQ+gqPI/tpYwg7Nswvybj6z0XqIcKWAkIoQlaIG6+qAy3yfw==,EMaQav3hXijRWQ0Fo54Vi7xINEhOa8ffU1i5tqAzK1s=,cI/NK6CodxcSeyf9niE3Cg==

The QR code image generated by the code is: https://i.stack.imgur.com/LHU1N.png

PS

Would anybody please address the actual problem? As you can see from the code, I'm trying to use Whatsapp API and that requires authentication. This is achieved by scanning the QR code generated by the Whatsapp client. It's not working for me.

Tech77
  • 41
  • 5
  • Did your phone recognize the alternative image that "looked different" ? – Caius Jard Mar 27 '22 at 06:59
  • It didn't. The tool has various options for QR generation: URL, text, contact... I tried URL and text but none of them were recognized. – Tech77 Mar 27 '22 at 07:04
  • Then it seems that it's rather about your phone not recognizing qr codes, which would be off topic on stackoverflow. See [help/on-topic]. Alternatively, show the qr codes you and that other tool generated to see if they're correct. – Olaf Kock Mar 27 '22 at 07:07
  • I've tried to scan it with a different phone as well, yet it didn't work. And what's more, the same phone can access Whatsapp Web App using QR scanning. – Tech77 Mar 27 '22 at 07:30
  • What exact text are you QR'ing ? Generate a "faulty" QR code too and add it you your post - I'm keen to see what my phone does – Caius Jard Mar 27 '22 at 07:45
  • https://imgur.com/Mz8qb9B This is the QR image generated with the tool I mentioned above – Tech77 Mar 27 '22 at 07:48
  • did you solve? If yes, can you please share what the issue was? – Ermal Jan 08 '23 at 22:44

0 Answers0