mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-20 12:49:32 +00:00
migrated receive message logic to typescript
This commit is contained in:
@@ -7,7 +7,8 @@ import ShowContactService from "../services/ContactServices/ShowContactService";
|
|||||||
import UpdateContactService from "../services/ContactServices/UpdateContactService";
|
import UpdateContactService from "../services/ContactServices/UpdateContactService";
|
||||||
import DeleteContactService from "../services/ContactServices/DeleteContactService";
|
import DeleteContactService from "../services/ContactServices/DeleteContactService";
|
||||||
|
|
||||||
// const { getWbot } = require("../libs/wbot");
|
import CheckIsValidContact from "../services/WbotServices/CheckIsValidContact";
|
||||||
|
import GetProfilePicUrl from "../services/WbotServices/GetProfilePicUrl";
|
||||||
|
|
||||||
type IndexQuery = {
|
type IndexQuery = {
|
||||||
searchParam: string;
|
searchParam: string;
|
||||||
@@ -37,54 +38,17 @@ interface ContactData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const store = async (req: Request, res: Response): Promise<Response> => {
|
export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||||
const { name, number, email, extraInfo }: ContactData = req.body;
|
const newContact: ContactData = req.body;
|
||||||
|
|
||||||
|
await CheckIsValidContact(newContact.number);
|
||||||
|
|
||||||
|
const profilePicUrl = await GetProfilePicUrl(newContact.number);
|
||||||
|
|
||||||
const contact = await CreateContactService({
|
const contact = await CreateContactService({
|
||||||
name,
|
...newContact,
|
||||||
number,
|
profilePicUrl
|
||||||
email,
|
|
||||||
extraInfo
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// const defaultWhatsapp = await Whatsapp.findOne({
|
|
||||||
// where: { default: true }
|
|
||||||
// });
|
|
||||||
|
|
||||||
// if (!defaultWhatsapp) {
|
|
||||||
// return res
|
|
||||||
// .status(404)
|
|
||||||
// .json({ error: "No default WhatsApp found. Check Connection page." });
|
|
||||||
// }
|
|
||||||
|
|
||||||
// const wbot = getWbot(defaultWhatsapp);
|
|
||||||
|
|
||||||
// try {
|
|
||||||
// const isValidNumber = await wbot.isRegisteredUser(
|
|
||||||
// `${newContact.number}@c.us`
|
|
||||||
// );
|
|
||||||
// if (!isValidNumber) {
|
|
||||||
// return res
|
|
||||||
// .status(400)
|
|
||||||
// .json({ error: "The suplied number is not a valid Whatsapp number" });
|
|
||||||
// }
|
|
||||||
// } catch (err) {
|
|
||||||
// console.log(err);
|
|
||||||
// return res.status(500).json({
|
|
||||||
// error: "Could not check whatsapp contact. Check connection page."
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
|
|
||||||
// const profilePicUrl = await wbot.getProfilePicUrl(
|
|
||||||
// `${newContact.number}@c.us`
|
|
||||||
// );
|
|
||||||
|
|
||||||
// const contact = await Contact.create(
|
|
||||||
// { ...newContact, profilePicUrl },
|
|
||||||
// {
|
|
||||||
// include: "extraInfo"
|
|
||||||
// }
|
|
||||||
// );
|
|
||||||
|
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
io.emit("contact", {
|
io.emit("contact", {
|
||||||
action: "create",
|
action: "create",
|
||||||
|
|||||||
@@ -1,8 +1,15 @@
|
|||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
// import { getIO } from "../libs/socket";
|
// import { getIO } from "../libs/socket";
|
||||||
|
|
||||||
|
import { Message as WbotMessage } from "whatsapp-web.js";
|
||||||
|
import SetTicketMessagesAsRead from "../helpers/SetTicketMessagesAsRead";
|
||||||
|
import { getIO } from "../libs/socket";
|
||||||
|
|
||||||
import CreateMessageService from "../services/MessageServices/CreateMessageService";
|
import CreateMessageService from "../services/MessageServices/CreateMessageService";
|
||||||
import ListMessagesService from "../services/MessageServices/ListMessagesService";
|
import ListMessagesService from "../services/MessageServices/ListMessagesService";
|
||||||
|
import ShowTicketService from "../services/TicketServices/ShowTicketService";
|
||||||
|
import SendWhatsAppMedia from "../services/WbotServices/SendWhatsAppMedia";
|
||||||
|
import SendWhatsAppMessage from "../services/WbotServices/SendWhatsAppMessage";
|
||||||
|
|
||||||
// import Sequelize from "sequelize";
|
// import Sequelize from "sequelize";
|
||||||
// import { MessageMedia } from "whatsapp-web.js";
|
// import { MessageMedia } from "whatsapp-web.js";
|
||||||
@@ -14,39 +21,17 @@ import ListMessagesService from "../services/MessageServices/ListMessagesService
|
|||||||
// import Ticket from "../models/Ticket";
|
// import Ticket from "../models/Ticket";
|
||||||
// import { getWbot } from "../libs/wbot";
|
// import { getWbot } from "../libs/wbot";
|
||||||
|
|
||||||
// const setMessagesAsRead = async ticket => {
|
|
||||||
// const io = getIO();
|
|
||||||
// const wbot = getWbot(ticket.whatsappId);
|
|
||||||
|
|
||||||
// await Message.update(
|
|
||||||
// { read: true },
|
|
||||||
// {
|
|
||||||
// where: {
|
|
||||||
// ticketId: ticket.id,
|
|
||||||
// read: false
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// );
|
|
||||||
|
|
||||||
// try {
|
|
||||||
// await wbot.sendSeen(`${ticket.contact.number}@c.us`);
|
|
||||||
// } catch (err) {
|
|
||||||
// console.log(
|
|
||||||
// "Could not mark messages as read. Maybe whatsapp session disconnected?"
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
|
|
||||||
// io.to("notification").emit("ticket", {
|
|
||||||
// action: "updateUnread",
|
|
||||||
// ticketId: ticket.id
|
|
||||||
// });
|
|
||||||
// };
|
|
||||||
|
|
||||||
type IndexQuery = {
|
type IndexQuery = {
|
||||||
searchParam: string;
|
searchParam: string;
|
||||||
pageNumber: string;
|
pageNumber: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type MessageData = {
|
||||||
|
body: string;
|
||||||
|
fromMe: boolean;
|
||||||
|
read: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
export const index = async (req: Request, res: Response): Promise<Response> => {
|
export const index = async (req: Request, res: Response): Promise<Response> => {
|
||||||
const { ticketId } = req.params;
|
const { ticketId } = req.params;
|
||||||
const { searchParam, pageNumber } = req.query as IndexQuery;
|
const { searchParam, pageNumber } = req.query as IndexQuery;
|
||||||
@@ -57,134 +42,49 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
|
|||||||
ticketId
|
ticketId
|
||||||
});
|
});
|
||||||
|
|
||||||
// const ticketMessages = await ticket.getMessages({
|
await SetTicketMessagesAsRead(ticket);
|
||||||
// where: whereCondition,
|
|
||||||
// limit,
|
|
||||||
// offset,
|
|
||||||
// order: [["createdAt", "DESC"]]
|
|
||||||
// });
|
|
||||||
|
|
||||||
// const count = await ticket.countMessages();
|
|
||||||
|
|
||||||
// const serializedMessages = ticketMessages.map(message => {
|
|
||||||
// return {
|
|
||||||
// ...message.dataValues,
|
|
||||||
// mediaUrl: `${
|
|
||||||
// message.mediaUrl
|
|
||||||
// ? `${process.env.BACKEND_URL}:${process.env.PROXY_PORT}/public/${message.mediaUrl}`
|
|
||||||
// : ""
|
|
||||||
// }`
|
|
||||||
// };
|
|
||||||
// });
|
|
||||||
|
|
||||||
// const hasMore = count > offset + ticketMessages.length;
|
|
||||||
|
|
||||||
// return res.json({
|
|
||||||
// messages: serializedMessages.reverse(),
|
|
||||||
// ticket,
|
|
||||||
// count,
|
|
||||||
// hasMore
|
|
||||||
// });
|
|
||||||
return res.json({ count, messages, ticket, hasMore });
|
return res.json({ count, messages, ticket, hasMore });
|
||||||
};
|
};
|
||||||
|
|
||||||
export const store = async (req: Request, res: Response): Promise<Response> => {
|
export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||||
// const io = getIO();
|
|
||||||
|
|
||||||
const { ticketId } = req.params;
|
const { ticketId } = req.params;
|
||||||
const messageData = req.body;
|
const { body, fromMe, read }: MessageData = req.body;
|
||||||
|
const media = req.file;
|
||||||
|
|
||||||
const message = await CreateMessageService({ messageData, ticketId });
|
const ticket = await ShowTicketService(ticketId);
|
||||||
|
|
||||||
// const media = req.file;
|
let sentMessage: WbotMessage;
|
||||||
// let sentMessage;
|
|
||||||
|
|
||||||
// const ticket = await Ticket.findByPk(ticketId, {
|
if (media) {
|
||||||
// include: [
|
sentMessage = await SendWhatsAppMedia({ media, ticket });
|
||||||
// {
|
} else {
|
||||||
// model: Contact,
|
sentMessage = await SendWhatsAppMessage({ body, ticket });
|
||||||
// as: "contact",
|
}
|
||||||
// attributes: ["number", "name", "profilePicUrl"]
|
|
||||||
// }
|
|
||||||
// ]
|
|
||||||
// });
|
|
||||||
|
|
||||||
// if (!ticket) {
|
const newMessage = {
|
||||||
// return res.status(404).json({ error: "No ticket found with this ID" });
|
id: sentMessage.id.id,
|
||||||
// }
|
body,
|
||||||
|
fromMe,
|
||||||
|
read,
|
||||||
|
mediaType: sentMessage.type,
|
||||||
|
mediaUrl: media?.filename
|
||||||
|
};
|
||||||
|
|
||||||
// if (!ticket.whatsappId) {
|
const message = await CreateMessageService({
|
||||||
// const defaultWhatsapp = await Whatsapp.findOne({
|
messageData: newMessage,
|
||||||
// where: { default: true }
|
ticketId: ticket.id
|
||||||
// });
|
});
|
||||||
|
|
||||||
// if (!defaultWhatsapp) {
|
const io = getIO();
|
||||||
// return res
|
io.to(ticketId).to("notification").emit("appMessage", {
|
||||||
// .status(404)
|
action: "create",
|
||||||
// .json({ error: "No default WhatsApp found. Check Connection page." });
|
message,
|
||||||
// }
|
ticket,
|
||||||
|
contact: ticket.contact
|
||||||
|
});
|
||||||
|
|
||||||
// await ticket.setWhatsapp(defaultWhatsapp);
|
await SetTicketMessagesAsRead(ticket);
|
||||||
// }
|
|
||||||
|
|
||||||
// const wbot = getWbot(ticket.whatsappId);
|
|
||||||
|
|
||||||
// try {
|
|
||||||
// if (media) {
|
|
||||||
// const newMedia = MessageMedia.fromFilePath(req.file.path);
|
|
||||||
|
|
||||||
// message.mediaUrl = req.file.filename;
|
|
||||||
// if (newMedia.mimetype) {
|
|
||||||
// message.mediaType = newMedia.mimetype.split("/")[0];
|
|
||||||
// } else {
|
|
||||||
// message.mediaType = "other";
|
|
||||||
// }
|
|
||||||
|
|
||||||
// sentMessage = await wbot.sendMessage(
|
|
||||||
// `${ticket.contact.number}@c.us`,
|
|
||||||
// newMedia
|
|
||||||
// );
|
|
||||||
|
|
||||||
// await ticket.update({ lastMessage: message.mediaUrl });
|
|
||||||
// } else {
|
|
||||||
// sentMessage = await wbot.sendMessage(
|
|
||||||
// `${ticket.contact.number}@c.us`,
|
|
||||||
// message.body
|
|
||||||
// );
|
|
||||||
// await ticket.update({ lastMessage: message.body });
|
|
||||||
// }
|
|
||||||
// } catch (err) {
|
|
||||||
// console.log(
|
|
||||||
// "Could not create whatsapp message. Is session details valid? "
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (sentMessage) {
|
|
||||||
// message.id = sentMessage.id.id;
|
|
||||||
// const newMessage = await ticket.createMessage(message);
|
|
||||||
|
|
||||||
// const serialziedMessage = {
|
|
||||||
// ...newMessage.dataValues,
|
|
||||||
// mediaUrl: `${
|
|
||||||
// message.mediaUrl
|
|
||||||
// ? `${process.env.BACKEND_URL}:${process.env.PROXY_PORT}/public/${message.mediaUrl}`
|
|
||||||
// : ""
|
|
||||||
// }`
|
|
||||||
// };
|
|
||||||
|
|
||||||
// io.to(ticketId).to("notification").emit("appMessage", {
|
|
||||||
// action: "create",
|
|
||||||
// message: serialziedMessage,
|
|
||||||
// ticket,
|
|
||||||
// contact: ticket.contact
|
|
||||||
// });
|
|
||||||
|
|
||||||
// await setMessagesAsRead(ticket);
|
|
||||||
|
|
||||||
return res.status(200).json(message);
|
return res.status(200).json(message);
|
||||||
};
|
};
|
||||||
|
|
||||||
// return res
|
|
||||||
// .status(500)
|
|
||||||
// .json({ error: "Cannot sent whatsapp message. Check connection page." });
|
|
||||||
// };
|
|
||||||
|
|||||||
@@ -1,203 +1,203 @@
|
|||||||
const Message = require("../models/Message");
|
const Message = require("../models/Message");
|
||||||
const Contact = require("../models/Contact");
|
const Contact = require("../models/Contact");
|
||||||
const User = require("../models/User");
|
const User = require("../models/User");
|
||||||
const Whatsapp = require("../models/Whatsapp");
|
const Whatsapp = require("../models/Whatsapp");
|
||||||
|
|
||||||
const Ticket = require("../models/Ticket");
|
const Ticket = require("../models/Ticket");
|
||||||
const { getIO } = require("../libs/socket");
|
const { getIO } = require("../libs/socket");
|
||||||
const { getWbot } = require("../libs/wbot");
|
const { getWbot } = require("../libs/wbot");
|
||||||
const Sequelize = require("sequelize");
|
const Sequelize = require("sequelize");
|
||||||
|
|
||||||
const { MessageMedia } = require("whatsapp-web.js");
|
const { MessageMedia } = require("whatsapp-web.js");
|
||||||
|
|
||||||
const setMessagesAsRead = async ticket => {
|
const setMessagesAsRead = async ticket => {
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
const wbot = getWbot(ticket.whatsappId);
|
const wbot = getWbot(ticket.whatsappId);
|
||||||
|
|
||||||
await Message.update(
|
await Message.update(
|
||||||
{ read: true },
|
{ read: true },
|
||||||
{
|
{
|
||||||
where: {
|
where: {
|
||||||
ticketId: ticket.id,
|
ticketId: ticket.id,
|
||||||
read: false,
|
read: false
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await wbot.sendSeen(`${ticket.contact.number}@c.us`);
|
await wbot.sendSeen(`${ticket.contact.number}@c.us`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(
|
console.log(
|
||||||
"Could not mark messages as read. Maybe whatsapp session disconnected?"
|
"Could not mark messages as read. Maybe whatsapp session disconnected?"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
io.to("notification").emit("ticket", {
|
io.to("notification").emit("ticket", {
|
||||||
action: "updateUnread",
|
action: "updateUnread",
|
||||||
ticketId: ticket.id,
|
ticketId: ticket.id
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.index = async (req, res, next) => {
|
exports.index = async (req, res, next) => {
|
||||||
const { ticketId } = req.params;
|
const { ticketId } = req.params;
|
||||||
const { searchParam = "", pageNumber = 1 } = req.query;
|
const { searchParam = "", pageNumber = 1 } = req.query;
|
||||||
|
|
||||||
const whereCondition = {
|
const whereCondition = {
|
||||||
body: Sequelize.where(
|
body: Sequelize.where(
|
||||||
Sequelize.fn("LOWER", Sequelize.col("body")),
|
Sequelize.fn("LOWER", Sequelize.col("body")),
|
||||||
"LIKE",
|
"LIKE",
|
||||||
"%" + searchParam.toLowerCase() + "%"
|
"%" + searchParam.toLowerCase() + "%"
|
||||||
),
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
const limit = 20;
|
const limit = 20;
|
||||||
const offset = limit * (pageNumber - 1);
|
const offset = limit * (pageNumber - 1);
|
||||||
|
|
||||||
const ticket = await Ticket.findByPk(ticketId, {
|
const ticket = await Ticket.findByPk(ticketId, {
|
||||||
include: [
|
include: [
|
||||||
{
|
{
|
||||||
model: Contact,
|
model: Contact,
|
||||||
as: "contact",
|
as: "contact",
|
||||||
include: "extraInfo",
|
include: "extraInfo",
|
||||||
attributes: ["id", "name", "number", "profilePicUrl"],
|
attributes: ["id", "name", "number", "profilePicUrl"]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
model: User,
|
model: User,
|
||||||
as: "user",
|
as: "user"
|
||||||
},
|
}
|
||||||
],
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!ticket) {
|
if (!ticket) {
|
||||||
return res.status(404).json({ error: "No ticket found with this ID" });
|
return res.status(404).json({ error: "No ticket found with this ID" });
|
||||||
}
|
}
|
||||||
|
|
||||||
await setMessagesAsRead(ticket);
|
await setMessagesAsRead(ticket);
|
||||||
|
|
||||||
const ticketMessages = await ticket.getMessages({
|
const ticketMessages = await ticket.getMessages({
|
||||||
where: whereCondition,
|
where: whereCondition,
|
||||||
limit,
|
limit,
|
||||||
offset,
|
offset,
|
||||||
order: [["createdAt", "DESC"]],
|
order: [["createdAt", "DESC"]]
|
||||||
});
|
});
|
||||||
|
|
||||||
const count = await ticket.countMessages();
|
const count = await ticket.countMessages();
|
||||||
|
|
||||||
const serializedMessages = ticketMessages.map(message => {
|
const serializedMessages = ticketMessages.map(message => {
|
||||||
return {
|
return {
|
||||||
...message.dataValues,
|
...message.dataValues,
|
||||||
mediaUrl: `${
|
mediaUrl: `${
|
||||||
message.mediaUrl
|
message.mediaUrl
|
||||||
? `${process.env.BACKEND_URL}:${process.env.PROXY_PORT}/public/${message.mediaUrl}`
|
? `${process.env.BACKEND_URL}:${process.env.PROXY_PORT}/public/${message.mediaUrl}`
|
||||||
: ""
|
: ""
|
||||||
}`,
|
}`
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
const hasMore = count > offset + ticketMessages.length;
|
const hasMore = count > offset + ticketMessages.length;
|
||||||
|
|
||||||
return res.json({
|
return res.json({
|
||||||
messages: serializedMessages.reverse(),
|
messages: serializedMessages.reverse(),
|
||||||
ticket,
|
ticket,
|
||||||
count,
|
count,
|
||||||
hasMore,
|
hasMore
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.store = async (req, res, next) => {
|
exports.store = async (req, res, next) => {
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
|
|
||||||
const { ticketId } = req.params;
|
const { ticketId } = req.params;
|
||||||
const message = req.body;
|
const message = req.body;
|
||||||
const media = req.file;
|
const media = req.file;
|
||||||
let sentMessage;
|
let sentMessage;
|
||||||
|
|
||||||
const ticket = await Ticket.findByPk(ticketId, {
|
const ticket = await Ticket.findByPk(ticketId, {
|
||||||
include: [
|
include: [
|
||||||
{
|
{
|
||||||
model: Contact,
|
model: Contact,
|
||||||
as: "contact",
|
as: "contact",
|
||||||
attributes: ["number", "name", "profilePicUrl"],
|
attributes: ["number", "name", "profilePicUrl"]
|
||||||
},
|
}
|
||||||
],
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!ticket) {
|
if (!ticket) {
|
||||||
return res.status(404).json({ error: "No ticket found with this ID" });
|
return res.status(404).json({ error: "No ticket found with this ID" });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!ticket.whatsappId) {
|
if (!ticket.whatsappId) {
|
||||||
const defaultWhatsapp = await Whatsapp.findOne({
|
const defaultWhatsapp = await Whatsapp.findOne({
|
||||||
where: { default: true },
|
where: { default: true }
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!defaultWhatsapp) {
|
if (!defaultWhatsapp) {
|
||||||
return res
|
return res
|
||||||
.status(404)
|
.status(404)
|
||||||
.json({ error: "No default WhatsApp found. Check Connection page." });
|
.json({ error: "No default WhatsApp found. Check Connection page." });
|
||||||
}
|
}
|
||||||
|
|
||||||
await ticket.setWhatsapp(defaultWhatsapp);
|
await ticket.setWhatsapp(defaultWhatsapp);
|
||||||
}
|
}
|
||||||
|
|
||||||
const wbot = getWbot(ticket.whatsappId);
|
const wbot = getWbot(ticket.whatsappId);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (media) {
|
if (media) {
|
||||||
const newMedia = MessageMedia.fromFilePath(req.file.path);
|
const newMedia = MessageMedia.fromFilePath(req.file.path);
|
||||||
|
|
||||||
message.mediaUrl = req.file.filename;
|
message.mediaUrl = req.file.filename;
|
||||||
if (newMedia.mimetype) {
|
if (newMedia.mimetype) {
|
||||||
message.mediaType = newMedia.mimetype.split("/")[0];
|
message.mediaType = newMedia.mimetype.split("/")[0];
|
||||||
} else {
|
} else {
|
||||||
message.mediaType = "other";
|
message.mediaType = "other";
|
||||||
}
|
}
|
||||||
|
|
||||||
sentMessage = await wbot.sendMessage(
|
sentMessage = await wbot.sendMessage(
|
||||||
`${ticket.contact.number}@c.us`,
|
`${ticket.contact.number}@c.us`,
|
||||||
newMedia
|
newMedia
|
||||||
);
|
);
|
||||||
|
|
||||||
await ticket.update({ lastMessage: message.mediaUrl });
|
await ticket.update({ lastMessage: message.mediaUrl });
|
||||||
} else {
|
} else {
|
||||||
sentMessage = await wbot.sendMessage(
|
sentMessage = await wbot.sendMessage(
|
||||||
`${ticket.contact.number}@c.us`,
|
`${ticket.contact.number}@c.us`,
|
||||||
message.body
|
message.body
|
||||||
);
|
);
|
||||||
await ticket.update({ lastMessage: message.body });
|
await ticket.update({ lastMessage: message.body });
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(
|
console.log(
|
||||||
"Could not create whatsapp message. Is session details valid? "
|
"Could not create whatsapp message. Is session details valid? "
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sentMessage) {
|
if (sentMessage) {
|
||||||
message.id = sentMessage.id.id;
|
message.id = sentMessage.id.id;
|
||||||
const newMessage = await ticket.createMessage(message);
|
const newMessage = await ticket.createMessage(message);
|
||||||
|
|
||||||
const serialziedMessage = {
|
const serialziedMessage = {
|
||||||
...newMessage.dataValues,
|
...newMessage.dataValues,
|
||||||
mediaUrl: `${
|
mediaUrl: `${
|
||||||
message.mediaUrl
|
message.mediaUrl
|
||||||
? `${process.env.BACKEND_URL}:${process.env.PROXY_PORT}/public/${message.mediaUrl}`
|
? `${process.env.BACKEND_URL}:${process.env.PROXY_PORT}/public/${message.mediaUrl}`
|
||||||
: ""
|
: ""
|
||||||
}`,
|
}`
|
||||||
};
|
};
|
||||||
|
|
||||||
io.to(ticketId).to("notification").emit("appMessage", {
|
io.to(ticketId).to("notification").emit("appMessage", {
|
||||||
action: "create",
|
action: "create",
|
||||||
message: serialziedMessage,
|
message: serialziedMessage,
|
||||||
ticket: ticket,
|
ticket: ticket,
|
||||||
contact: ticket.contact,
|
contact: ticket.contact
|
||||||
});
|
});
|
||||||
|
|
||||||
await setMessagesAsRead(ticket);
|
await setMessagesAsRead(ticket);
|
||||||
|
|
||||||
return res.status(200).json({ newMessage, ticket });
|
return res.status(200).json({ newMessage, ticket });
|
||||||
}
|
}
|
||||||
|
|
||||||
return res
|
return res
|
||||||
.status(500)
|
.status(500)
|
||||||
.json({ error: "Cannot sent whatsapp message. Check connection page." });
|
.json({ error: "Cannot sent whatsapp message. Check connection page." });
|
||||||
};
|
};
|
||||||
|
|||||||
22
backend/src/helpers/GetTicketWbot.ts
Normal file
22
backend/src/helpers/GetTicketWbot.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import { Client as Session } from "whatsapp-web.js";
|
||||||
|
import { getWbot } from "../libs/wbot";
|
||||||
|
import AppError from "../errors/AppError";
|
||||||
|
import GetDefaultWhatsApp from "./GetDefaultWhatsApp";
|
||||||
|
import Ticket from "../models/Ticket";
|
||||||
|
|
||||||
|
const GetTicketWbot = async (ticket: Ticket): Promise<Session> => {
|
||||||
|
if (!ticket.whatsappId) {
|
||||||
|
const defaultWhatsapp = await GetDefaultWhatsApp();
|
||||||
|
|
||||||
|
if (!defaultWhatsapp) {
|
||||||
|
throw new AppError("No default WhatsApp found. Check Connection page.");
|
||||||
|
}
|
||||||
|
await ticket.$set("whatsapp", defaultWhatsapp);
|
||||||
|
}
|
||||||
|
|
||||||
|
const wbot = getWbot(ticket.whatsappId);
|
||||||
|
|
||||||
|
return wbot;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default GetTicketWbot;
|
||||||
34
backend/src/helpers/SetTicketMessagesAsRead.ts
Normal file
34
backend/src/helpers/SetTicketMessagesAsRead.ts
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
import { getIO } from "../libs/socket";
|
||||||
|
import { getWbot } from "../libs/wbot";
|
||||||
|
import Message from "../models/Message";
|
||||||
|
import Ticket from "../models/Ticket";
|
||||||
|
|
||||||
|
const SetTicketMessagesAsRead = async (ticket: Ticket): Promise<void> => {
|
||||||
|
const wbot = getWbot(ticket.whatsappId);
|
||||||
|
|
||||||
|
await Message.update(
|
||||||
|
{ read: true },
|
||||||
|
{
|
||||||
|
where: {
|
||||||
|
ticketId: ticket.id,
|
||||||
|
read: false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await wbot.sendSeen(`${ticket.contact.number}@c.us`);
|
||||||
|
} catch (err) {
|
||||||
|
console.log(
|
||||||
|
"Could not mark messages as read. Maybe whatsapp session disconnected?"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const io = getIO();
|
||||||
|
io.to("notification").emit("ticket", {
|
||||||
|
action: "updateUnread",
|
||||||
|
ticketId: ticket.id
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export default SetTicketMessagesAsRead;
|
||||||
@@ -2,6 +2,7 @@ import qrCode from "qrcode-terminal";
|
|||||||
import { Client } from "whatsapp-web.js";
|
import { Client } from "whatsapp-web.js";
|
||||||
import { getIO } from "./socket";
|
import { getIO } from "./socket";
|
||||||
import Whatsapp from "../models/Whatsapp";
|
import Whatsapp from "../models/Whatsapp";
|
||||||
|
import AppError from "../errors/AppError";
|
||||||
|
|
||||||
interface Session extends Client {
|
interface Session extends Client {
|
||||||
id?: number;
|
id?: number;
|
||||||
@@ -10,7 +11,6 @@ interface Session extends Client {
|
|||||||
const sessions: Session[] = [];
|
const sessions: Session[] = [];
|
||||||
|
|
||||||
export const initWbot = async (whatsapp: Whatsapp): Promise<void> => {
|
export const initWbot = async (whatsapp: Whatsapp): Promise<void> => {
|
||||||
console.log("starting");
|
|
||||||
try {
|
try {
|
||||||
const io = getIO();
|
const io = getIO();
|
||||||
const sessionName = whatsapp.name;
|
const sessionName = whatsapp.name;
|
||||||
@@ -26,7 +26,7 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<void> => {
|
|||||||
sessions.splice(sessionIndex, 1);
|
sessions.splice(sessionIndex, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
const wbot = new Client({
|
const wbot: Session = new Client({
|
||||||
session: sessionCfg,
|
session: sessionCfg,
|
||||||
restartOnAuthFail: true
|
restartOnAuthFail: true
|
||||||
});
|
});
|
||||||
@@ -88,12 +88,11 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<void> => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getWbot = (whatsappId: number): Session | null => {
|
export const getWbot = (whatsappId: number): Session => {
|
||||||
const sessionIndex = sessions.findIndex(s => s.id === whatsappId);
|
const sessionIndex = sessions.findIndex(s => s.id === whatsappId);
|
||||||
|
|
||||||
if (sessionIndex === -1) {
|
if (sessionIndex === -1) {
|
||||||
console.log("This Wbot session is not initialized");
|
throw new AppError("This WhatsApp session is not initialized.");
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
return sessions[sessionIndex];
|
return sessions[sessionIndex];
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ interface Request {
|
|||||||
name: string;
|
name: string;
|
||||||
number: string;
|
number: string;
|
||||||
email?: string;
|
email?: string;
|
||||||
|
profilePicUrl?: string;
|
||||||
extraInfo?: ExtraInfo[];
|
extraInfo?: ExtraInfo[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,18 @@
|
|||||||
|
import AppError from "../../errors/AppError";
|
||||||
import Message from "../../models/Message";
|
import Message from "../../models/Message";
|
||||||
import ShowTicketService from "../TicketServices/ShowTicketService";
|
import ShowTicketService from "../TicketServices/ShowTicketService";
|
||||||
|
|
||||||
|
interface MessageData {
|
||||||
|
id: string;
|
||||||
|
body: string;
|
||||||
|
fromMe: boolean;
|
||||||
|
read: boolean;
|
||||||
|
mediaType: string;
|
||||||
|
mediaUrl?: string;
|
||||||
|
}
|
||||||
interface Request {
|
interface Request {
|
||||||
ticketId: string;
|
ticketId: string | number;
|
||||||
messageData: Message;
|
messageData: MessageData;
|
||||||
}
|
}
|
||||||
|
|
||||||
const CreateMessageService = async ({
|
const CreateMessageService = async ({
|
||||||
@@ -13,10 +22,10 @@ const CreateMessageService = async ({
|
|||||||
const ticket = await ShowTicketService(ticketId);
|
const ticket = await ShowTicketService(ticketId);
|
||||||
|
|
||||||
if (!ticket) {
|
if (!ticket) {
|
||||||
throw new Error("No ticket found with this ID");
|
throw new AppError("No ticket found with this ID");
|
||||||
}
|
}
|
||||||
|
|
||||||
const message = Message.create({ ...messageData, ticketId });
|
const message: Message = await ticket.$create("message", messageData);
|
||||||
|
|
||||||
return message;
|
return message;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ const ShowTicketService = async (id: string | number): Promise<Ticket> => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!ticket) {
|
if (!ticket) {
|
||||||
throw new AppError("No ticket found with this conditions.", 404);
|
throw new AppError("No ticket found with this ID");
|
||||||
}
|
}
|
||||||
|
|
||||||
return ticket;
|
return ticket;
|
||||||
|
|||||||
23
backend/src/services/WbotServices/CheckIsValidContact.ts
Normal file
23
backend/src/services/WbotServices/CheckIsValidContact.ts
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import AppError from "../../errors/AppError";
|
||||||
|
import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp";
|
||||||
|
import { getWbot } from "../../libs/wbot";
|
||||||
|
|
||||||
|
const CheckIsValidContact = async (number: string): Promise<void> => {
|
||||||
|
const defaultWhatsapp = await GetDefaultWhatsApp();
|
||||||
|
|
||||||
|
const wbot = getWbot(defaultWhatsapp.id);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const isValidNumber = await wbot.isRegisteredUser(`${number}@c.us`);
|
||||||
|
if (!isValidNumber) {
|
||||||
|
throw new AppError("The suplied number is not a valid Whatsapp number");
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
throw new AppError(
|
||||||
|
"Could not valid WhatsApp contact. Check connections page"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default CheckIsValidContact;
|
||||||
14
backend/src/services/WbotServices/GetProfilePicUrl.ts
Normal file
14
backend/src/services/WbotServices/GetProfilePicUrl.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp";
|
||||||
|
import { getWbot } from "../../libs/wbot";
|
||||||
|
|
||||||
|
const GetProfilePicUrl = async (number: string): Promise<string> => {
|
||||||
|
const defaultWhatsapp = await GetDefaultWhatsApp();
|
||||||
|
|
||||||
|
const wbot = getWbot(defaultWhatsapp.id);
|
||||||
|
|
||||||
|
const profilePicUrl = await wbot.getProfilePicUrl(`${number}@c.us`);
|
||||||
|
|
||||||
|
return profilePicUrl;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default GetProfilePicUrl;
|
||||||
29
backend/src/services/WbotServices/SendWhatsAppMedia.ts
Normal file
29
backend/src/services/WbotServices/SendWhatsAppMedia.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import { MessageMedia, Message as WbotMessage } from "whatsapp-web.js";
|
||||||
|
import GetTicketWbot from "../../helpers/GetTicketWbot";
|
||||||
|
import Ticket from "../../models/Ticket";
|
||||||
|
|
||||||
|
interface Request {
|
||||||
|
media: Express.Multer.File;
|
||||||
|
ticket: Ticket;
|
||||||
|
}
|
||||||
|
|
||||||
|
const SendWhatsAppMedia = async ({
|
||||||
|
media,
|
||||||
|
ticket
|
||||||
|
}: Request): Promise<WbotMessage> => {
|
||||||
|
const wbot = await GetTicketWbot(ticket);
|
||||||
|
|
||||||
|
const newMedia = MessageMedia.fromFilePath(media.path);
|
||||||
|
|
||||||
|
const mediaUrl = media.filename;
|
||||||
|
|
||||||
|
const sentMessage = await wbot.sendMessage(
|
||||||
|
`${ticket.contact.number}@c.us`,
|
||||||
|
newMedia
|
||||||
|
);
|
||||||
|
|
||||||
|
await ticket.update({ lastMessage: mediaUrl });
|
||||||
|
return sentMessage;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default SendWhatsAppMedia;
|
||||||
25
backend/src/services/WbotServices/SendWhatsAppMessage.ts
Normal file
25
backend/src/services/WbotServices/SendWhatsAppMessage.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import { Message as WbotMessage } from "whatsapp-web.js";
|
||||||
|
import GetTicketWbot from "../../helpers/GetTicketWbot";
|
||||||
|
import Ticket from "../../models/Ticket";
|
||||||
|
|
||||||
|
interface Request {
|
||||||
|
body: string;
|
||||||
|
ticket: Ticket;
|
||||||
|
}
|
||||||
|
|
||||||
|
const SendWhatsAppMessage = async ({
|
||||||
|
body,
|
||||||
|
ticket
|
||||||
|
}: Request): Promise<WbotMessage> => {
|
||||||
|
const wbot = await GetTicketWbot(ticket);
|
||||||
|
|
||||||
|
const sentMessage = await wbot.sendMessage(
|
||||||
|
`${ticket.contact.number}@c.us`,
|
||||||
|
body
|
||||||
|
);
|
||||||
|
|
||||||
|
await ticket.update({ lastMessage: body });
|
||||||
|
return sentMessage;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default SendWhatsAppMessage;
|
||||||
@@ -124,7 +124,6 @@ const handleMessage = async (
|
|||||||
ticket: Ticket,
|
ticket: Ticket,
|
||||||
contact: Contact
|
contact: Contact
|
||||||
) => {
|
) => {
|
||||||
const io = getIO();
|
|
||||||
let newMessage: Message;
|
let newMessage: Message;
|
||||||
|
|
||||||
if (msg.hasMedia) {
|
if (msg.hasMedia) {
|
||||||
@@ -138,6 +137,7 @@ const handleMessage = async (
|
|||||||
await ticket.update({ lastMessage: msg.body });
|
await ticket.update({ lastMessage: msg.body });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const io = getIO();
|
||||||
io.to(ticket.id.toString()).to("notification").emit("appMessage", {
|
io.to(ticket.id.toString()).to("notification").emit("appMessage", {
|
||||||
action: "create",
|
action: "create",
|
||||||
message: newMessage,
|
message: newMessage,
|
||||||
@@ -157,7 +157,7 @@ const wbotMessageListener = (whatsapp: Whatsapp): void => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
wbot.on("message_create", async msg => {
|
wbot.on("message_create", async msg => {
|
||||||
// console.log(msg);
|
console.log(msg.type);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
msg.from === "status@broadcast" ||
|
msg.from === "status@broadcast" ||
|
||||||
|
|||||||
Reference in New Issue
Block a user