mirror of
https://github.com/cheveguerra/botLeiferAurik-Mod_2.0.git
synced 2026-04-19 20:29:23 +00:00
feat: botones
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
const { Client, LocalAuth, Buttons, List } = require('whatsapp-web.js');
|
||||
const { sendMedia, sendMessage, sendMessageButton, sendMessageList, readChat } = require(`../controllers/send`);
|
||||
require('dotenv').config()
|
||||
const express = require('express');
|
||||
const bodyParser = require("body-parser");
|
||||
@@ -24,30 +25,27 @@ app.use(express.json());
|
||||
app.use(bodyParser.urlencoded({ extended: true }));
|
||||
|
||||
let socks
|
||||
let client
|
||||
|
||||
initBot = async () => {
|
||||
console.log("WaWebJS Init")
|
||||
const client = new Client({
|
||||
console.log("Iniciamos WaWebJS")
|
||||
client = new Client({
|
||||
authStrategy: new LocalAuth(),
|
||||
puppeteer: { headless: true, args: ['--no-sandbox','--disable-setuid-sandbox'] }
|
||||
});
|
||||
|
||||
client.on('ready', (a) => {
|
||||
connectionReady()
|
||||
// listenMessage(client)
|
||||
// listenMessageFromBot()
|
||||
// socketEvents.sendStatus(client)
|
||||
});
|
||||
|
||||
client.on('auth_failure', (e) => {
|
||||
// console.log(e)
|
||||
// connectionLost()
|
||||
});
|
||||
|
||||
client.on('authenticated', () => {
|
||||
console.log('AUTHENTICATED');
|
||||
});
|
||||
|
||||
client.initialize();
|
||||
|
||||
/**
|
||||
@@ -58,23 +56,22 @@ initBot = async () => {
|
||||
mysqlConnection.connect()
|
||||
}
|
||||
let waReady = false
|
||||
// Socket IO
|
||||
// Socket IO
|
||||
io.on('connection', async function (socket) {
|
||||
console.log("Connecting ...")
|
||||
console.log("Conectando ...")
|
||||
socket.emit('ioStatus', socketioStatus);
|
||||
socks = socket
|
||||
|
||||
await socket.emit('message', 'Connecting...');
|
||||
|
||||
await socket.emit('message', 'Conectando...');
|
||||
socket.on('checkConn', async function () { // Si recibe mensaje, regresa "connOk"
|
||||
console.log("checking conn")
|
||||
await socket.emit('connOk', 'Connected');
|
||||
})
|
||||
|
||||
try {
|
||||
client.on('message', () => {
|
||||
client.on('message', msg => {
|
||||
// console.log(msg)
|
||||
console.log(waReady)
|
||||
socketioStatus = "wa_msg"; socket.emit('ioStatus', socketioStatus);
|
||||
socketioStatus = "wa_msg"; socket.emit('ioStatus', socketioStatus); socket.emit('wa_msg', msg.body);
|
||||
socket.emit('incomming', 'Message In')
|
||||
waReady = true
|
||||
})
|
||||
@@ -88,8 +85,8 @@ initBot = async () => {
|
||||
|
||||
client.on('ready', async () => {
|
||||
socketioStatus = "wa_ready"
|
||||
await socket.emit('ready', 'Whatsapp is ready!');
|
||||
await socket.emit('message', 'Whatsapp is ready!');
|
||||
await socket.emit('ready', 'Whatsapp esta listo!');
|
||||
await socket.emit('message', 'Whatsapp esta listo!');
|
||||
waReady = true
|
||||
});
|
||||
|
||||
@@ -116,20 +113,17 @@ initBot = async () => {
|
||||
catch (e) {waReady = false }
|
||||
|
||||
});
|
||||
|
||||
server.listen(port, () => {
|
||||
console.log(`El server esta listo en el puerto ${port}`);
|
||||
console.log(`El servidor web esta listo en el puerto ${port} - http://localhost:${port}`);
|
||||
})
|
||||
|
||||
const phoneNumberFormatter = function (number) {
|
||||
// 1. Menghilangkan karakter selain angka
|
||||
// 1. Eliminar caracteres que no sean números
|
||||
let formatted = number.replace(/\D/g, '');
|
||||
|
||||
// 2. Menghilangkan angka 0 di depan (prefix)
|
||||
// Kemudian diganti dengan 62
|
||||
if (formatted.startsWith('0')) {
|
||||
formatted = '62' + formatted.substr(1);
|
||||
}
|
||||
// 2. Eliminar el número 0 del prefijo y reemplazarlo con 62
|
||||
// if (formatted.startsWith('0')) {
|
||||
// formatted = '62' + formatted.substr(1);
|
||||
// }
|
||||
|
||||
if (!formatted.endsWith('@c.us')) {
|
||||
formatted += '@c.us';
|
||||
@@ -137,35 +131,28 @@ initBot = async () => {
|
||||
|
||||
return formatted;
|
||||
}
|
||||
|
||||
const checkRegisteredNumber = async function (number) {
|
||||
const isRegistered = await client.isRegisteredUser(number);
|
||||
return isRegistered;
|
||||
}
|
||||
|
||||
// Send message
|
||||
app.post('/send-message', [
|
||||
body('number').notEmpty(),
|
||||
body('message').notEmpty(),
|
||||
], async (req, res) => {
|
||||
|
||||
console.log("xxxxxxxx", req.body)
|
||||
|
||||
console.log("REQUEST=", req.body)
|
||||
socks.emit('incomming', 'Message In')
|
||||
|
||||
const errors = validationResult(req).formatWith(({
|
||||
msg
|
||||
}) => {
|
||||
return msg;
|
||||
});
|
||||
|
||||
if (!errors.isEmpty()) {
|
||||
return res.status(422).json({
|
||||
status: false,
|
||||
message: errors.mapped()
|
||||
});
|
||||
}
|
||||
|
||||
const number = phoneNumberFormatter(req.body.number);
|
||||
const message = req.body.message;
|
||||
const isRegisteredNumber = await checkRegisteredNumber(number);
|
||||
@@ -175,7 +162,6 @@ initBot = async () => {
|
||||
message: 'The number is not registered'
|
||||
});
|
||||
}
|
||||
|
||||
client.sendMessage(number, message).then(response => {
|
||||
res.status(200).json({
|
||||
status: true,
|
||||
@@ -188,7 +174,49 @@ initBot = async () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
// Send buttons
|
||||
app.post('/send-buttons', async (req, res) => {
|
||||
console.log("REQUEST=", req.body)
|
||||
socks.emit('incomming', 'Button In')
|
||||
|
||||
if (client.theMsg === undefined) {
|
||||
client.theMsg = {}
|
||||
client.theMsg.from = "WEB"
|
||||
client.theMsg.body = "WEB"
|
||||
client.theMsg.name = "WEB"
|
||||
}
|
||||
|
||||
const number = phoneNumberFormatter(req.body.number);
|
||||
const title = req.body.title || null;
|
||||
const btnmsg = req.body.btnmsg;
|
||||
const btn1 = req.body.btn1;
|
||||
const btn2 = req.body.btn2 || null;
|
||||
const btn3 = req.body.btn3 || null;
|
||||
const footer = req.body.footer || null;
|
||||
const isRegisteredNumber = await checkRegisteredNumber(number);
|
||||
if (!isRegisteredNumber) {
|
||||
return res.status(422).json({
|
||||
status: false,
|
||||
message: 'The number is not registered'
|
||||
});
|
||||
}
|
||||
let botones = []
|
||||
botones.push({ body: btn1 })
|
||||
if (btn2 != null) { botones.push({ body: btn2 }) }
|
||||
if (btn3 != null) { botones.push({body: btn3}) }
|
||||
let losBotones = new Buttons(btnmsg, botones, title, footer);
|
||||
client.sendMessage(number, losBotones).then(response => {
|
||||
res.status(200).json({
|
||||
status: true,
|
||||
response: response
|
||||
});
|
||||
}).catch(err => {
|
||||
res.status(500).json({
|
||||
status: false,
|
||||
response: err
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
checkEnvFile();
|
||||
return client
|
||||
|
||||
Reference in New Issue
Block a user