-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest2.js
78 lines (64 loc) · 2.27 KB
/
test2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const { Client, LocalAuth } = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
const Funciones = require('./funciones_2.js');
const dotenv = require('dotenv');
const readlineSync = require('readline-sync');
dotenv.config();
// Pregunta al usuario qué cliente desea utilizar
const selectedClient = readlineSync.question('¿Deseas iniciar con el cliente 1 o cliente 2? (1/2): ');
// Inicializa el cliente correspondiente
let client;
if (selectedClient === '1') {
client = createClient('cliente-1');
} else if (selectedClient === '2') {
client = createClient('cliente-2');
} else {
console.log('Selección no válida. Debes elegir 1 o 2.');
process.exit(1);
}
// Función para crear un cliente con la configuración dada
function createClient(clientId) {
return new Client({
authStrategy: new LocalAuth({ clientId }),
});
}
// Manejar eventos del cliente
client.on('qr', onQRCode);
client.on('ready', onClientReady);
client.on('message', onMessageReceived);
client.initialize();
// Manejar cierre del programa
process.on('SIGINT', async () => {
console.log('(SIGINT) Shutting down...');
// Espera 1 segundo antes de continuar con la destrucción del cliente
setTimeout(async () => {
await client.destroy();
console.log('Client destroyed');
process.exit(0);
}, 1000);
});
// Funciones para manejar eventos
function onQRCode(qr) {
qrcode.generate(qr, { small: true });
console.log('Escanea el código QR con la aplicación de WhatsApp en tu teléfono.');
}
function onClientReady() {
console.log('¡El cliente está listo!');
const numeroDestino = process.env.NUMERO_DESTINO;
const chatId = numeroDestino.substring(1) + "@c.us";
client.sendMessage(chatId, `Inicio de sesión exitoso`);
}
function onMessageReceived(msg) {
chat = msg.from.split('@')[0];
console.log(chat + ': ' + msg.body);
if (chat.length <= 11) {
Funciones.Saludo(msg, client);
Funciones.Contacto(msg, client);
const randomDelay = Math.floor(Math.random() * (14000 - 5000 + 1)) + 5000;
// Llama a las funciones con el retraso aleatorio
setTimeout(() => {
Funciones.Proyectos(msg, client);
Funciones.Pagina(msg, client);
}, randomDelay);
}
}