Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
whatsapp-connect
Advanced tools
A library that simplifies the initialization and management of WhatsApp Web services using whatsapp-web.js and puppeteer. It generates QR codes as images for authentication, allowing users to scan and then perform additional actions programmatically, such
A JavaScript library that simplifies the initialization and management of WhatsApp Web services using whatsapp-web.js
and puppeteer
. It provides an easy-to-use interface for handling authentication, generating QR codes as images, and enabling users to perform additional actions programmatically, such as sending messages.
For more information, check out the GitHub repository of this project.
Install the library using npm:
npm install whatsapp-connect
To enable Puppeteer to function correctly on Debian-based systems, like Ubuntu, you need to install the following dependencies using the apt-get command (remember to run apt-get update before installing):
sudo apt-get update
sudo apt install -y gconf-service libgbm-dev libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
Note: These instructions are specific to systems using apt-get, such as Debian and Ubuntu. If you are using a different Linux distribution, make sure to search for and install the equivalent dependencies for your distribution.
It is crucial to install chromium-browser specifically for the library to function correctly. Ensure to follow these detailed steps:
# Install Chromium Browser
sudo apt install chromium-browser
Afterward, verify the installation of chromium-browser by running the following command:
chromium-browser --version
This command will display the installed version of Chromium-Browser
. Make sure the installation is successful before proceeding with the library usage. The specific version of chromium-browser
is essential for library compatibility, so confirming its correct installation is crucial.
The necessary modules are imported, including launchBrowser and whatsappClient from the whatsapp-connect library.
const { launchBrowser, whatsappClient } = require('whatsapp-connect');
async function startWhatsAppBot() {
const browserInstance = await launchBrowser();
whatsappClient.on('qr', async qr => {
console.log('Scan the following QR code with your phone:');
});
whatsappClient.initialize();
}
Calls the startWhatsAppBot function to start the WhatsApp bot.
startWhatsAppBot();
SendMsg is an asynchronous function that runs when the WhatsApp client is ready. It sends a test message to a specific phone number using whatsappClient.sendMessage().
const SendMsg = async () => {
await whatsappClient.on('ready', async () => {
const targetNumber = `51961610362@c.us`;
const message = 'Hello, I am Frank Cairampoma! I am testing my WhatsApp-connect.';
await whatsappClient.sendMessage(targetNumber, message);
console.log('Message sent successfully');
});
}
SendMsg();
targetNumber
Variable:In this example, a message is being sent to a Peru number "51971619505@c.us"
. The country code for Peru is +51
, but for the library, we omit the "+"
symbol. Change the country code to the desired one for the number you want to send; for example, "961610362"
is a mobile number in Peru. Replace it with the desired phone number, and end with "@c.us"
which is the required format for processing.
SendMsg
FunctionIn step 4 of the script (index.js
), the function SendMsg
is used to send a test message through the WhatsApp client. It's important to note that the name of the function SendMsg
is arbitrary and can be modified according to user preferences. In fact, this function can be integrated in various ways into your application.
Variation of the Function Name:
The SendMsg
function can be renamed according to preferred naming conventions. You can use any name that better reflects its purpose and code structure.
Integration with a REST API: Instead of encapsulating the functionality in a standalone function, you can integrate it directly into an endpoint of your REST API. This allows the execution of the message-sending service through HTTP requests, providing greater flexibility in integration with other parts of your application.
IMPORTANT:
Important to add to your .gitignore
the folders .wwebjs_auth
and .wwebjs_cache
, as the WhatsApp session is stored there.
Here's a basic example of how the code might be structured within a REST API endpoint:
// Example of integration into a REST API
const express = require('express');
const { launchBrowser, whatsappClient } = require('whatsapp-connect');
const app = express();
app.use(express.json());
async function startWhatsAppBot() {
const browserInstance = await launchBrowser();
whatsappClient.on('qr', async qr => {
console.log('Scan the following QR code with your phone:');
});
await whatsappClient.initialize();
}
startWhatsAppBot();
const SendMsg = async (targetNumber, message) => {
console.log('Sending message...');
try {
await whatsappClient.sendMessage(targetNumber, message);
console.log('Message sent successfully');
return true;
} catch (error) {
console.error('Error sending message:', error);
throw error;
}
};
app.post('/api/whatsapp', async (req, res) => {
const message = req.body.message;
const phone = req.body.phone;
const targetNumber = `51${phone}@c.us`;
try {
const msg = await SendMsg(targetNumber, message);
res.json(msg? { message: 'Message sent successfully' } : { message: 'Error sending message' });
} catch (error) {
res.status(500).json({ error: 'Error sending message' });
}
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`REST API server listening on port ${PORT}`);
});
const { launchBrowser, whatsappClient } = require('whatsapp-connect');
async function startWhatsAppBot() {
const browserInstance = await launchBrowser();
whatsappClient.on('qr', async qr => {
console.log('Scan the following QR code with your phone:');
});
whatsappClient.initialize();
}
startWhatsAppBot();
const SendMsg = async (req, res) => {
await whatsappClient.on('ready', async () => {
const targetNumber = `51961610362@c.us`;
const message = 'Hola, soy un bot que envía dddddd';
await whatsappClient.sendMessage(targetNumber, message);
console.log('Message sent successfully');
})
}
SendMsg();
or
import { launchBrowser, whatsappClient } from 'whatsapp-connect';
async function startWhatsAppBot() {
const browserInstance = await launchBrowser();
whatsappClient.on('qr', async qr => {
console.log('Escanea el siguiente código QR con tu teléfono:');
});
whatsappClient.initialize();
}
startWhatsAppBot();
const SendMsg = async () => {
await whatsappClient.on('ready', async () => {
const targetNumber = `51971619505@c.us`;
const message = 'Hola, soy un bot que envía dddddd';
await whatsappClient.sendMessage(targetNumber, message);
console.log('Mensaje enviado correctamente');
});
}
SendMsg();
Una biblioteca de JavaScript que simplifica la inicialización y gestión de los servicios de WhatsApp Web utilizando whatsapp-web.js
y puppeteer
. Proporciona una interfaz fácil de usar para manejar la autenticación, generar códigos QR como imágenes y permitir a los usuarios realizar acciones adicionales programáticamente, como enviar mensajes.
Para obtener más información, consulta el GitHub repository de este proyecto.
Instala la biblioteca usando npm:
npm install whatsapp-connect
Para que Puppeteer funcione correctamente en sistemas basados en Debian, como Ubuntu, debes instalar las siguientes dependencias usando el comando apt-get (recuerda ejecutar apt-get update antes de instalar):
sudo apt-get update
sudo apt install -y gconf-service libgbm-dev libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
Nota: Estas instrucciones son específicas para sistemas que utilizan apt-get, como Debian y Ubuntu. Si estás utilizando una distribución de Linux diferente, asegúrate de buscar e instalar las dependencias equivalentes para tu distribución.
Es crucial instalar específicamente chromium-browser
para que la biblioteca funcione correctamente. Asegúrate de seguir estos pasos detallados:
# Install Chromium Browser
sudo apt install chromium-browser
Luego, verifica la instalación de chromium-browser
ejecutando el siguiente comando:
chromium-browser --version
Este comando mostrará la versión instalada de Chromium Browser
. Asegúrate de que la instalación sea exitosa antes de proceder con el uso de la biblioteca. La versión específica de chromium-browser
es esencial para la compatibilidad de la biblioteca, por lo que es crucial confirmar su correcta instalación.
Se importan los módulos necesarios, incluidos launchBrowser y whatsappClient de la biblioteca whatsapp-connect
.
const { launchBrowser, whatsappClient } = require('whatsapp-connect');
startWhatsAppBot
inicia el servicio WhatsApp Web JS.whatsappClient.initialize()
. async function startWhatsAppBot() {
const browserInstance = await launchBrowser();
whatsappClient.on('qr', async qr => {
console.log('Scan the following QR code with your phone:');
});
whatsappClient.initialize();
}
Llama a la función startWhatsAppBot
para iniciar el bot de WhatsApp.
startWhatsAppBot();
SendMsg es una función asíncrona que se ejecuta cuando el cliente de WhatsApp está listo.
Envía un mensaje de prueba a un número de teléfono específico usando whatsappClient.sendMessage()
.
const SendMsg = async () => {
await whatsappClient.on('ready', async () => {
const targetNumber = `51961610362@c.us`;
const message = 'Hello, I am Frank Cairampoma! I am testing my WhatsApp-connect.';
await whatsappClient.sendMessage(targetNumber, message);
console.log('Message sent successfully');
});
}
SendMsg();
targetNumber
:En este ejemplo, se está enviando un mensaje a un número de Perú "51971619505@c.us"
. El código de país para Perú es +51
, pero para la biblioteca, omitimos el símbolo "+"
. Cambia al código de país que desees para el número que quieras enviar, por ejemplo, "961610362" es un número de celular en Perú. Reemplaza con el número de teléfono deseado y termina con "@c.us"
, que es el formato necesario para ser procesado.
SendMsg
En el paso 4 del script (index.js
), la función SendMsg
se utiliza para enviar un mensaje de prueba a través del cliente de WhatsApp. Es importante tener en cuenta que el nombre de la función SendMsg
es arbitrario y puede modificarse según las preferencias del usuario. De hecho, esta función se puede integrar de varias maneras en tu aplicación.
Variación del Nombre de la Función:
La función SendMsg
puede ser renombrada según las convenciones de nombres preferidas. Puedes usar cualquier nombre que refleje mejor su propósito y estructura de código.
Integración con una API REST: En lugar de encapsular la funcionalidad en una función independiente, puedes integrarla directamente en un punto final de tu API REST. Esto permite la ejecución del servicio de envío de mensajes a través de solicitudes HTTP, brindando una mayor flexibilidad en la integración con otras partes de tu aplicación.
.gitignore
las carpetas .wwebjs_auth
y .wwebjs_cache
, ya que es allí donde se guarda la sesión de WhatsApp.Aquí tienes un ejemplo básico de cómo podría estructurarse el código dentro de un punto final de una API REST:
// Ejemplo de integración en una API REST
const express = require('express');
const { launchBrowser, whatsappClient } = require('whatsapp-connect');
const app = express();
app.use(express.json());
async function startWhatsAppBot() {
const browserInstance = await launchBrowser();
whatsappClient.on('qr', async qr => {
console.log('Scan the following QR code with your phone:');
});
await whatsappClient.initialize();
}
startWhatsAppBot();
const SendMsg = async (targetNumber, message) => {
console.log('Sending message...');
try {
await whatsappClient.sendMessage(targetNumber, message);
console.log('Message sent successfully');
return true;
} catch (error) {
console.error('Error sending message:', error);
throw error;
}
};
app.post('/api/whatsapp', async (req, res) => {
const message = req.body.message;
const phone = req.body.phone;
const targetNumber = `51${phone}@c.us`;
try {
const msg = await SendMsg(targetNumber, message);
res.json(msg? { message: 'Message sent successfully' } : { message: 'Error sending message' });
} catch (error) {
res.status(500).json({ error: 'Error sending message' });
}
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`REST API server listening on port ${PORT}`);
});
const { launchBrowser, whatsappClient } = require('whatsapp-connect');
async function startWhatsAppBot() {
const browserInstance = await launchBrowser();
whatsappClient.on('qr', async qr => {
console.log('Scan the following QR code with your phone:');
});
whatsappClient.initialize();
}
startWhatsAppBot();
const SendMsg = async (req, res) => {
await whatsappClient.on('ready', async () => {
const targetNumber = `51961610362@c.us`;
const message = 'Hola, soy un bot que envía dddddd';
await whatsappClient.sendMessage(targetNumber, message);
console.log('Message sent successfully');
})
}
SendMsg();
o
import { launchBrowser, whatsappClient } from 'whatsapp-connect';
async function startWhatsAppBot() {
const browserInstance = await launchBrowser();
whatsappClient.on('qr', async qr => {
console.log('Escanea el siguiente código QR con tu teléfono:');
});
whatsappClient.initialize();
}
startWhatsAppBot();
const SendMsg = async () => {
await whatsappClient.on('ready', async () => {
const targetNumber = `51971619505@c.us`;
const message = 'Hola, soy un bot que envía dddddd';
await whatsappClient.sendMessage(targetNumber, message);
console.log('Mensaje enviado correctamente');
});
}
SendMsg();
FAQs
A library that simplifies the initialization and management of WhatsApp Web services using whatsapp-web.js and puppeteer. It generates QR codes as images for authentication, allowing users to scan and then perform additional actions programmatically, such
The npm package whatsapp-connect receives a total of 0 weekly downloads. As such, whatsapp-connect popularity was classified as not popular.
We found that whatsapp-connect demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.