Skip to content

Instantly share code, notes, and snippets.

@kuatroestrellas
Last active March 13, 2024 13:39
Show Gist options
  • Save kuatroestrellas/a127ac1d12c6ac83dafee3fe57281949 to your computer and use it in GitHub Desktop.
Save kuatroestrellas/a127ac1d12c6ac83dafee3fe57281949 to your computer and use it in GitHub Desktop.
/**
* Bot para whatsapp
* web: https://kuatroestrellas.github.io/blog/
* responde al hola mundo con un mensaje
* requiere nodejs v12 o superior y las librerias qrcode-terminal y whatsapp-web.js
* npm i qrcode-terminal whatsapp-web.js
**/
const qrcode = require('qrcode-terminal');
//Crea una sesión con whatsapp-web y la guarda localmente para autenticarse solo una vez por QR
const { Client, LocalAuth } = require('whatsapp-web.js');
const client = new Client({
authStrategy: new LocalAuth()
});
//Genera el código qr para conectarse a whatsapp-web
client.on('qr', qr => {
qrcode.generate(qr, {small: true});
});
//Si la conexión es exitosa muestra el mensaje de conexión exitosa
client.on('ready', () => {
console.log('Conexion exitosa nenes');
});
//Aquí sucede la magia, escucha los mensajes y aquí es donde se manipula lo que queremos que haga el bot
client.on('message', message => {
console.log(message.body);
if(message.body === 'hola mundo') {
client.sendMessage(message.from, 'Hola soy un bot, mi creador esta ocupado ayudando a gohan a salvar la tierra');
}
});
client.initialize();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment