Whatsapp-web.js On Ubuntu Server

If you are encountering issues with launching the browser while trying to load the QR code using whatsapp-web.js on an Ubuntu server, you can try the following approach:

First, make sure you have installed the necessary dependencies, including whatsapp-web.js, qrcode-terminal, and a headless browser like puppeteer:

npm install whatsapp-web.js qrcode-terminal puppeteer

Next, create a file called index.js and paste the following code:

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

// Path to store session data
const SESSION_FILE_PATH = './session.json';

// Initialize the WhatsApp client
const client = new Client({
    session: require(SESSION_FILE_PATH),
    puppeteer: {
        executablePath: '/usr/bin/chromium-browser', // Change this path to your Chromium executable
        headless: true,
        args: ['--no-sandbox', '--disable-setuid-sandbox'],
    },
});

// Generate and display the QR code
client.on('qr', (qr) => {
    console.log('Scan the QR code below to log in:');
    qrcode.generate(qr, { small: true });
});

// Save session data upon successful authentication
client.on('authenticated', (session) => {
    console.log('Authenticated');
    // Save session data to file
    fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), (err) => {
        if (err) {
            console.error(err);
        }
    });
});

// Start the client
client.initialize();

// Handle any errors
client.on('auth_failure', (error) => {
    console.error('Authentication failed:', error);
});
client.on('disconnected', (reason) => {
    console.error('Client disconnected:', reason);
});

// Set up an HTTP server to keep the script running
const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
    res.send('WhatsApp Web script is running.');
});

app.listen(port, () => {
    console.log(`Server running at http://localhost:${port}`);
});

Make sure to adjust the executablePath in the puppeteer configuration to the correct path of your Chromium executable on your Ubuntu server.

To run the code, execute the following command in the terminal:

node index.js

This will start the WhatsApp client, generate the QR code, and display it in the terminal. You can then use a QR code scanning app on your mobile device to scan the code and authenticate the session.

Please note that running a headless browser on a server might require additional configurations and dependencies. Make sure you have the necessary dependencies installed and the correct paths configured for Chromium or any other headless browser you choose to use.

Remember to handle any errors and implement the necessary error-handling and exception-handling mechanisms in your code.

Additionally, ensure that you comply with the terms of service and usage policies of WhatsApp and the whatsapp-web.js library.

note kill port
sudo lsof -i :$your_port
kill -9 $your_pid

Komentar

Postingan populer dari blog ini

Node.js Telegram Bot API send an image with text

Node.js Telegram BOT retrieve data from API

WhatsApp Web login QR code in an HTML page using whatsapp-web.js