mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-20 20:59:16 +00:00
start converting wbot functions to typescript
This commit is contained in:
@@ -16,9 +16,8 @@ import Ticket from "./Ticket";
|
|||||||
@Table
|
@Table
|
||||||
class Message extends Model<Message> {
|
class Message extends Model<Message> {
|
||||||
@PrimaryKey
|
@PrimaryKey
|
||||||
@AutoIncrement
|
|
||||||
@Column
|
@Column
|
||||||
id: number;
|
id: string;
|
||||||
|
|
||||||
@Default(0)
|
@Default(0)
|
||||||
@Column
|
@Column
|
||||||
@@ -35,8 +34,15 @@ class Message extends Model<Message> {
|
|||||||
@Column(DataType.TEXT)
|
@Column(DataType.TEXT)
|
||||||
body: string;
|
body: string;
|
||||||
|
|
||||||
@Column
|
@Column(DataType.STRING)
|
||||||
mediaUrl: string;
|
get mediaUrl(): string | null {
|
||||||
|
if (this.getDataValue("mediaUrl")) {
|
||||||
|
return `${process.env.BACKEND_URL}:${
|
||||||
|
process.env.PROXY_PORT
|
||||||
|
}/public/${this.getDataValue("mediaUrl")}`;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
@Column
|
@Column
|
||||||
mediaType: string;
|
mediaType: string;
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import { initWbot } from "./libs/wbot";
|
|||||||
// const wbotMessageListener = require("./services/wbotMessageListener");
|
// const wbotMessageListener = require("./services/wbotMessageListener");
|
||||||
// const wbotMonitor = require("./services/wbotMonitor");
|
// const wbotMonitor = require("./services/wbotMonitor");
|
||||||
import Whatsapp from "./models/Whatsapp";
|
import Whatsapp from "./models/Whatsapp";
|
||||||
|
import wbotMessageListener from "./services/WbotServices/wbotMessageListener";
|
||||||
|
|
||||||
Sentry.init({ dsn: process.env.SENTRY_DSN });
|
Sentry.init({ dsn: process.env.SENTRY_DSN });
|
||||||
|
|
||||||
@@ -68,8 +69,7 @@ const startWhatsAppSessions = async () => {
|
|||||||
whatsapps.forEach(whatsapp => {
|
whatsapps.forEach(whatsapp => {
|
||||||
initWbot(whatsapp)
|
initWbot(whatsapp)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log("initialized!!");
|
wbotMessageListener(whatsapp);
|
||||||
// wbotMessageListener(whatsapp);
|
|
||||||
// wbotMonitor(whatsapp);
|
// wbotMonitor(whatsapp);
|
||||||
})
|
})
|
||||||
.catch(err => console.log(err));
|
.catch(err => console.log(err));
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ const CreateTicketService = async ({
|
|||||||
throw new AppError("No default WhatsApp found. Check Connection page.");
|
throw new AppError("No default WhatsApp found. Check Connection page.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const { id } = await defaultWhatsapp.$create("ticket", {
|
const { id }: Ticket = await defaultWhatsapp.$create("ticket", {
|
||||||
contactId,
|
contactId,
|
||||||
status
|
status
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -4,7 +4,10 @@ import { Op } from "sequelize";
|
|||||||
import { subHours } from "date-fns";
|
import { subHours } from "date-fns";
|
||||||
import Sentry from "@sentry/node";
|
import Sentry from "@sentry/node";
|
||||||
|
|
||||||
import { Client, Message } from "whatsapp-web.js";
|
import {
|
||||||
|
Contact as WbotContact,
|
||||||
|
Message as WbotMessage
|
||||||
|
} from "whatsapp-web.js";
|
||||||
|
|
||||||
import Contact from "../../models/Contact";
|
import Contact from "../../models/Contact";
|
||||||
import Ticket from "../../models/Ticket";
|
import Ticket from "../../models/Ticket";
|
||||||
@@ -13,42 +16,54 @@ import Whatsapp from "../../models/Whatsapp";
|
|||||||
|
|
||||||
import { getIO } from "../../libs/socket";
|
import { getIO } from "../../libs/socket";
|
||||||
import { getWbot } from "../../libs/wbot";
|
import { getWbot } from "../../libs/wbot";
|
||||||
|
import AppError from "../../errors/AppError";
|
||||||
|
import ShowTicketService from "../TicketServices/ShowTicketService";
|
||||||
|
|
||||||
const verifyContact = async (msgContact, profilePicUrl) => {
|
const verifyContact = async (
|
||||||
|
msgContact: WbotContact,
|
||||||
|
profilePicUrl: string
|
||||||
|
): Promise<Contact> => {
|
||||||
let contact = await Contact.findOne({
|
let contact = await Contact.findOne({
|
||||||
where: { number: msgContact.number }
|
where: { number: msgContact.number }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (contact) {
|
if (contact) {
|
||||||
await contact.update({ profilePicUrl: profilePicUrl });
|
await contact.update({ profilePicUrl });
|
||||||
} else {
|
} else {
|
||||||
contact = await Contact.create({
|
contact = await Contact.create({
|
||||||
name: msgContact.pushname || msgContact.number.toString(),
|
name: msgContact.pushname || msgContact.number.toString(),
|
||||||
number: msgContact.number,
|
number: msgContact.number,
|
||||||
profilePicUrl: profilePicUrl
|
profilePicUrl
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return contact;
|
return contact;
|
||||||
};
|
};
|
||||||
|
|
||||||
const verifyTicket = async (contact, whatsappId) => {
|
const verifyTicket = async (
|
||||||
|
contact: Contact,
|
||||||
|
whatsappId: number
|
||||||
|
): Promise<Ticket> => {
|
||||||
let ticket = await Ticket.findOne({
|
let ticket = await Ticket.findOne({
|
||||||
where: {
|
where: {
|
||||||
status: {
|
status: {
|
||||||
[Op.or]: ["open", "pending"]
|
[Op.or]: ["open", "pending"]
|
||||||
},
|
},
|
||||||
contactId: contact.id
|
contactId: contact.id
|
||||||
}
|
},
|
||||||
|
include: ["contact"]
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!ticket) {
|
if (!ticket) {
|
||||||
ticket = await Ticket.findOne({
|
ticket = await Ticket.findOne({
|
||||||
where: {
|
where: {
|
||||||
createdAt: { [Op.between]: [subHours(new Date(), 2), new Date()] },
|
createdAt: {
|
||||||
|
[Op.between]: [+subHours(new Date(), 2), +new Date()]
|
||||||
|
},
|
||||||
contactId: contact.id
|
contactId: contact.id
|
||||||
},
|
},
|
||||||
order: [["createdAt", "DESC"]]
|
order: [["createdAt", "DESC"]],
|
||||||
|
include: ["contact"]
|
||||||
});
|
});
|
||||||
|
|
||||||
if (ticket) {
|
if (ticket) {
|
||||||
@@ -57,28 +72,35 @@ const verifyTicket = async (contact, whatsappId) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!ticket) {
|
if (!ticket) {
|
||||||
ticket = await Ticket.create({
|
const { id } = await Ticket.create({
|
||||||
contactId: contact.id,
|
contactId: contact.id,
|
||||||
status: "pending",
|
status: "pending",
|
||||||
whatsappId
|
whatsappId
|
||||||
});
|
});
|
||||||
|
|
||||||
|
ticket = await ShowTicketService(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ticket;
|
return ticket;
|
||||||
};
|
};
|
||||||
|
|
||||||
const handlMedia = async (msg, ticket) => {
|
const handlMedia = async (
|
||||||
|
msg: WbotMessage,
|
||||||
|
ticket: Ticket
|
||||||
|
): Promise<Message> => {
|
||||||
const media = await msg.downloadMedia();
|
const media = await msg.downloadMedia();
|
||||||
let newMessage;
|
|
||||||
console.log("criando midia");
|
if (!media) {
|
||||||
if (media) {
|
throw new AppError("Cannot download media from whatsapp.");
|
||||||
|
}
|
||||||
|
|
||||||
if (!media.filename) {
|
if (!media.filename) {
|
||||||
let ext = media.mimetype.split("/")[1].split(";")[0];
|
const ext = media.mimetype.split("/")[1].split(";")[0];
|
||||||
media.filename = `${new Date().getTime()}.${ext}`;
|
media.filename = `${new Date().getTime()}.${ext}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.writeFile(
|
fs.writeFile(
|
||||||
path.join(__dirname, "..", "..", "public", media.filename),
|
path.join(__dirname, "..", "..", "..", "public", media.filename),
|
||||||
media.data,
|
media.data,
|
||||||
"base64",
|
"base64",
|
||||||
err => {
|
err => {
|
||||||
@@ -86,7 +108,7 @@ const handlMedia = async (msg, ticket) => {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
newMessage = await ticket.createMessage({
|
const newMessage: Message = await ticket.$create("message", {
|
||||||
id: msg.id.id,
|
id: msg.id.id,
|
||||||
body: msg.body || media.filename,
|
body: msg.body || media.filename,
|
||||||
fromMe: msg.fromMe,
|
fromMe: msg.fromMe,
|
||||||
@@ -94,19 +116,21 @@ const handlMedia = async (msg, ticket) => {
|
|||||||
mediaType: media.mimetype.split("/")[0]
|
mediaType: media.mimetype.split("/")[0]
|
||||||
});
|
});
|
||||||
await ticket.update({ lastMessage: msg.body || media.filename });
|
await ticket.update({ lastMessage: msg.body || media.filename });
|
||||||
}
|
|
||||||
|
|
||||||
return newMessage;
|
return newMessage;
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleMessage = async (msg, ticket, contact) => {
|
const handleMessage = async (
|
||||||
|
msg: WbotMessage,
|
||||||
|
ticket: Ticket,
|
||||||
|
contact: Contact
|
||||||
|
) => {
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
let newMessage;
|
let newMessage: Message;
|
||||||
|
|
||||||
if (msg.hasMedia) {
|
if (msg.hasMedia) {
|
||||||
newMessage = await handlMedia(msg, ticket);
|
newMessage = await handlMedia(msg, ticket);
|
||||||
} else {
|
} else {
|
||||||
newMessage = await ticket.createMessage({
|
newMessage = await ticket.$create("message", {
|
||||||
id: msg.id.id,
|
id: msg.id.id,
|
||||||
body: msg.body,
|
body: msg.body,
|
||||||
fromMe: msg.fromMe
|
fromMe: msg.fromMe
|
||||||
@@ -114,25 +138,11 @@ const handleMessage = async (msg, ticket, contact) => {
|
|||||||
await ticket.update({ lastMessage: msg.body });
|
await ticket.update({ lastMessage: msg.body });
|
||||||
}
|
}
|
||||||
|
|
||||||
const serializedMessage = {
|
io.to(ticket.id.toString()).to("notification").emit("appMessage", {
|
||||||
...newMessage.dataValues,
|
|
||||||
mediaUrl: `${
|
|
||||||
newMessage.mediaUrl
|
|
||||||
? `${process.env.BACKEND_URL}:${process.env.PROXY_PORT}/public/${newMessage.mediaUrl}`
|
|
||||||
: ""
|
|
||||||
}`
|
|
||||||
};
|
|
||||||
|
|
||||||
const serializaedTicket = {
|
|
||||||
...ticket.dataValues,
|
|
||||||
contact: contact
|
|
||||||
};
|
|
||||||
|
|
||||||
io.to(ticket.id).to("notification").emit("appMessage", {
|
|
||||||
action: "create",
|
action: "create",
|
||||||
message: serializedMessage,
|
message: newMessage,
|
||||||
ticket: serializaedTicket,
|
ticket,
|
||||||
contact: contact
|
contact
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -146,21 +156,20 @@ const wbotMessageListener = (whatsapp: Whatsapp): void => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
wbot.on(
|
wbot.on("message_create", async msg => {
|
||||||
"message_create",
|
|
||||||
async (msg): Promise<void> => {
|
|
||||||
// console.log(msg);
|
// console.log(msg);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
msg.from === "status@broadcast" ||
|
msg.from === "status@broadcast" ||
|
||||||
msg.type === "location" ||
|
msg.type === "location" ||
|
||||||
msg.author !== null // Ignore Group Messages
|
msg.type === "call_log" ||
|
||||||
|
msg.author // Ignore Group Messages
|
||||||
) {
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let msgContact;
|
let msgContact: WbotContact;
|
||||||
|
|
||||||
if (msg.fromMe) {
|
if (msg.fromMe) {
|
||||||
msgContact = await wbot.getContactById(msg.to);
|
msgContact = await wbot.getContactById(msg.to);
|
||||||
@@ -172,7 +181,8 @@ const wbotMessageListener = (whatsapp: Whatsapp): void => {
|
|||||||
const contact = await verifyContact(msgContact, profilePicUrl);
|
const contact = await verifyContact(msgContact, profilePicUrl);
|
||||||
const ticket = await verifyTicket(contact, whatsappId);
|
const ticket = await verifyTicket(contact, whatsappId);
|
||||||
|
|
||||||
//return if message was already created by messageController
|
// return if message was already created by messageController
|
||||||
|
|
||||||
if (msg.fromMe) {
|
if (msg.fromMe) {
|
||||||
const alreadyExists = await Message.findOne({
|
const alreadyExists = await Message.findOne({
|
||||||
where: { id: msg.id.id }
|
where: { id: msg.id.id }
|
||||||
@@ -188,8 +198,7 @@ const wbotMessageListener = (whatsapp: Whatsapp): void => {
|
|||||||
Sentry.captureException(err);
|
Sentry.captureException(err);
|
||||||
console.log(err);
|
console.log(err);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
);
|
|
||||||
|
|
||||||
wbot.on("message_ack", async (msg, ack) => {
|
wbot.on("message_ack", async (msg, ack) => {
|
||||||
try {
|
try {
|
||||||
@@ -199,9 +208,9 @@ const wbotMessageListener = (whatsapp: Whatsapp): void => {
|
|||||||
if (!messageToUpdate) {
|
if (!messageToUpdate) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await messageToUpdate.update({ ack: ack });
|
await messageToUpdate.update({ ack });
|
||||||
|
|
||||||
io.to(messageToUpdate.ticketId).emit("appMessage", {
|
io.to(messageToUpdate.ticketId.toString()).emit("appMessage", {
|
||||||
action: "update",
|
action: "update",
|
||||||
message: messageToUpdate
|
message: messageToUpdate
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user