mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 12:19:16 +00:00
Backend folders organize and finshed send audio
This commit is contained in:
@@ -2,7 +2,7 @@ const Contact = require("../models/Contact");
|
||||
const Message = require("../models/Message");
|
||||
const Sequelize = require("sequelize");
|
||||
const { getIO } = require("../libs/socket");
|
||||
const { getWbot } = require("./wbot");
|
||||
const { getWbot } = require("../libs/wbot");
|
||||
|
||||
exports.getContacts = async (req, res) => {
|
||||
const { searchParam = "" } = req.query;
|
||||
|
||||
@@ -2,7 +2,7 @@ const fs = require("fs");
|
||||
const Message = require("../models/Message");
|
||||
const Contact = require("../models/Contact");
|
||||
const { getIO } = require("../libs/socket");
|
||||
const { getWbot } = require("./wbot");
|
||||
const { getWbot } = require("../libs/wbot");
|
||||
const Sequelize = require("sequelize");
|
||||
|
||||
const { MessageMedia } = require("whatsapp-web.js");
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
const qrCode = require("qrcode-terminal");
|
||||
const { Client } = require("whatsapp-web.js");
|
||||
const Whatsapp = require("../models/Whatsapp");
|
||||
const { getIO } = require("../libs/socket");
|
||||
|
||||
let wbot;
|
||||
|
||||
module.exports = {
|
||||
init: async () => {
|
||||
let sessionCfg;
|
||||
|
||||
const dbSession = await Whatsapp.findOne({ where: { id: 1 } });
|
||||
if (dbSession && dbSession.session) {
|
||||
sessionCfg = JSON.parse(dbSession.session);
|
||||
}
|
||||
|
||||
wbot = new Client({
|
||||
session: sessionCfg,
|
||||
restartOnAuthFail: true,
|
||||
});
|
||||
wbot.initialize();
|
||||
wbot.on("qr", async qr => {
|
||||
qrCode.generate(qr, { small: true });
|
||||
await Whatsapp.upsert({ id: 1, qrcode: qr, status: "pending" });
|
||||
getIO().emit("qrcode", {
|
||||
action: "update",
|
||||
qr: qr,
|
||||
});
|
||||
});
|
||||
wbot.on("authenticated", async session => {
|
||||
console.log("AUTHENTICATED");
|
||||
await Whatsapp.upsert({
|
||||
id: 1,
|
||||
session: JSON.stringify(session),
|
||||
status: "authenticated",
|
||||
});
|
||||
getIO().emit("whats_auth", {
|
||||
action: "authentication",
|
||||
session: dbSession,
|
||||
});
|
||||
});
|
||||
wbot.on("auth_failure", async msg => {
|
||||
console.error("AUTHENTICATION FAILURE", msg);
|
||||
await Whatsapp.update({ session: "" }, { where: { id: 1 } });
|
||||
});
|
||||
wbot.on("ready", async () => {
|
||||
console.log("READY");
|
||||
await Whatsapp.update(
|
||||
{ status: "online", qrcode: "" },
|
||||
{ where: { id: 1 } }
|
||||
);
|
||||
// const chats = await wbot.getChats(); // pega as mensagens nao lidas (recebidas quando o bot estava offline)
|
||||
// let unreadMessages; // todo > salvar isso no DB pra mostrar no frontend
|
||||
// for (let chat of chats) {
|
||||
// if (chat.unreadCount > 0) {
|
||||
// unreadMessages = await chat.fetchMessages({
|
||||
// limit: chat.unreadCount,
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
|
||||
// console.log(unreadMessages);
|
||||
wbot.sendPresenceAvailable();
|
||||
});
|
||||
return wbot;
|
||||
},
|
||||
|
||||
getWbot: () => {
|
||||
if (!wbot) {
|
||||
throw new Error("Wbot not initialized");
|
||||
}
|
||||
return wbot;
|
||||
},
|
||||
};
|
||||
@@ -1,138 +0,0 @@
|
||||
const Contact = require("../models/Contact");
|
||||
const Message = require("../models/Message");
|
||||
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
|
||||
const { getIO } = require("../libs/socket");
|
||||
const { getWbot, init } = require("./wbot");
|
||||
|
||||
const wbotMessageListener = () => {
|
||||
const io = getIO();
|
||||
const wbot = getWbot();
|
||||
|
||||
wbot.on("message", async msg => {
|
||||
console.log(msg);
|
||||
let newMessage;
|
||||
// console.log(msg);
|
||||
if (msg.from === "status@broadcast") {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const msgContact = await msg.getContact();
|
||||
const profilePicUrl = await msgContact.getProfilePicUrl();
|
||||
try {
|
||||
let contact = await Contact.findOne({
|
||||
where: { number: msgContact.number },
|
||||
});
|
||||
|
||||
if (contact) {
|
||||
await contact.update({ profilePicUrl: profilePicUrl });
|
||||
}
|
||||
|
||||
if (!contact) {
|
||||
try {
|
||||
contact = await Contact.create({
|
||||
name: msgContact.pushname || msgContact.number.toString(),
|
||||
number: msgContact.number,
|
||||
profilePicUrl: profilePicUrl,
|
||||
});
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
}
|
||||
|
||||
if (msg.hasQuotedMsg) {
|
||||
const quotedMessage = await msg.getQuotedMessage();
|
||||
console.log("quoted", quotedMessage);
|
||||
}
|
||||
|
||||
if (msg.hasMedia) {
|
||||
const media = await msg.downloadMedia();
|
||||
|
||||
if (media) {
|
||||
if (!media.filename) {
|
||||
let ext = media.mimetype.split("/")[1].split(";")[0];
|
||||
media.filename = `${new Date().getTime()}.${ext}`;
|
||||
}
|
||||
|
||||
fs.writeFile(
|
||||
path.join(__dirname, "..", "public", media.filename),
|
||||
media.data,
|
||||
"base64",
|
||||
err => {
|
||||
console.log(err);
|
||||
}
|
||||
);
|
||||
|
||||
newMessage = await contact.createMessage({
|
||||
id: msg.id.id,
|
||||
messageBody: msg.body || media.filename,
|
||||
mediaUrl: media.filename,
|
||||
mediaType: media.mimetype.split("/")[0],
|
||||
});
|
||||
await contact.update({ lastMessage: msg.body || media.filename });
|
||||
}
|
||||
} else {
|
||||
newMessage = await contact.createMessage({
|
||||
id: msg.id.id,
|
||||
messageBody: msg.body,
|
||||
});
|
||||
await contact.update({ lastMessage: msg.body });
|
||||
}
|
||||
|
||||
io.to(contact.id)
|
||||
.to("notification")
|
||||
.emit("appMessage", {
|
||||
action: "create",
|
||||
message: {
|
||||
...newMessage.dataValues,
|
||||
mediaUrl: `${
|
||||
newMessage.mediaUrl
|
||||
? `http://${process.env.HOST}:${process.env.PORT}/public/${newMessage.mediaUrl}`
|
||||
: ""
|
||||
}`,
|
||||
},
|
||||
contact: {
|
||||
...contact.dataValues,
|
||||
unreadMessages: 1,
|
||||
lastMessage: newMessage.messageBody,
|
||||
},
|
||||
});
|
||||
|
||||
let chat = await msg.getChat();
|
||||
chat.sendSeen();
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
});
|
||||
|
||||
wbot.on("message_ack", async (msg, ack) => {
|
||||
try {
|
||||
const messageToUpdate = await Message.findOne({
|
||||
where: { id: msg.id.id },
|
||||
});
|
||||
if (!messageToUpdate) {
|
||||
// will throw an error is msg wasn't sent from app
|
||||
const error = new Error(
|
||||
"Erro ao alterar o ack da mensagem no banco de dados"
|
||||
);
|
||||
error.statusCode = 501;
|
||||
throw error;
|
||||
}
|
||||
await messageToUpdate.update({ ack: ack });
|
||||
|
||||
io.to(messageToUpdate.contactId).emit("appMessage", {
|
||||
action: "update",
|
||||
message: messageToUpdate,
|
||||
});
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = wbotMessageListener;
|
||||
@@ -1,50 +0,0 @@
|
||||
const Whatsapp = require("../models/Whatsapp");
|
||||
const wbotMessageListener = require("./wbotMessageListener");
|
||||
|
||||
const { getIO } = require("../libs/socket");
|
||||
const { getWbot, init } = require("./wbot");
|
||||
|
||||
const wbotMonitor = () => {
|
||||
const io = getIO();
|
||||
const wbot = getWbot();
|
||||
|
||||
try {
|
||||
wbot.on("change_state", newState => {
|
||||
console.log("monitor", newState);
|
||||
});
|
||||
|
||||
wbot.on("change_battery", async batteryInfo => {
|
||||
// Battery percentage for attached device has changed
|
||||
const { battery, plugged } = batteryInfo;
|
||||
try {
|
||||
await Whatsapp.update({ battery, plugged }, { where: { id: 1 } });
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
|
||||
console.log(`Battery: ${battery}% - Charging? ${plugged}`); //todo> save batery state to db
|
||||
});
|
||||
|
||||
wbot.on("disconnected", reason => {
|
||||
console.log("disconnected", reason); //todo> save connection status to DB
|
||||
setTimeout(
|
||||
() =>
|
||||
init()
|
||||
.then(res => {
|
||||
wbotMessageListener();
|
||||
wbotMonitor();
|
||||
})
|
||||
.catch(err => console.log(err)),
|
||||
2000
|
||||
);
|
||||
});
|
||||
|
||||
// setInterval(() => {
|
||||
// wbot.resetState();
|
||||
// }, 20000);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = wbotMonitor;
|
||||
@@ -1,6 +1,6 @@
|
||||
const Whatsapp = require("../models/Whatsapp");
|
||||
const { getIO } = require("../libs/socket");
|
||||
const { getWbot, init } = require("./wbot");
|
||||
const { getWbot, init } = require("../libs/wbot");
|
||||
|
||||
exports.getSession = async (req, res, next) => {
|
||||
const dbSession = await Whatsapp.findOne({ where: { id: 1 } });
|
||||
|
||||
Reference in New Issue
Block a user