mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-18 03:39:29 +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 DeleteContactService from "../services/ContactServices/DeleteContactService";
|
||||
|
||||
// const { getWbot } = require("../libs/wbot");
|
||||
import CheckIsValidContact from "../services/WbotServices/CheckIsValidContact";
|
||||
import GetProfilePicUrl from "../services/WbotServices/GetProfilePicUrl";
|
||||
|
||||
type IndexQuery = {
|
||||
searchParam: string;
|
||||
@@ -37,54 +38,17 @@ interface ContactData {
|
||||
}
|
||||
|
||||
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({
|
||||
name,
|
||||
number,
|
||||
email,
|
||||
extraInfo
|
||||
...newContact,
|
||||
profilePicUrl
|
||||
});
|
||||
|
||||
// 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();
|
||||
io.emit("contact", {
|
||||
action: "create",
|
||||
|
||||
@@ -1,8 +1,15 @@
|
||||
import { Request, Response } from "express";
|
||||
// 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 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 { MessageMedia } from "whatsapp-web.js";
|
||||
@@ -14,39 +21,17 @@ import ListMessagesService from "../services/MessageServices/ListMessagesService
|
||||
// import Ticket from "../models/Ticket";
|
||||
// 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 = {
|
||||
searchParam: string;
|
||||
pageNumber: string;
|
||||
};
|
||||
|
||||
type MessageData = {
|
||||
body: string;
|
||||
fromMe: boolean;
|
||||
read: boolean;
|
||||
};
|
||||
|
||||
export const index = async (req: Request, res: Response): Promise<Response> => {
|
||||
const { ticketId } = req.params;
|
||||
const { searchParam, pageNumber } = req.query as IndexQuery;
|
||||
@@ -57,134 +42,49 @@ export const index = async (req: Request, res: Response): Promise<Response> => {
|
||||
ticketId
|
||||
});
|
||||
|
||||
// const ticketMessages = await ticket.getMessages({
|
||||
// where: whereCondition,
|
||||
// limit,
|
||||
// offset,
|
||||
// order: [["createdAt", "DESC"]]
|
||||
// });
|
||||
await SetTicketMessagesAsRead(ticket);
|
||||
|
||||
// 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 });
|
||||
};
|
||||
|
||||
export const store = async (req: Request, res: Response): Promise<Response> => {
|
||||
// const io = getIO();
|
||||
|
||||
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;
|
||||
let sentMessage: WbotMessage;
|
||||
|
||||
// const ticket = await Ticket.findByPk(ticketId, {
|
||||
// include: [
|
||||
// {
|
||||
// model: Contact,
|
||||
// as: "contact",
|
||||
// attributes: ["number", "name", "profilePicUrl"]
|
||||
// }
|
||||
// ]
|
||||
// });
|
||||
if (media) {
|
||||
sentMessage = await SendWhatsAppMedia({ media, ticket });
|
||||
} else {
|
||||
sentMessage = await SendWhatsAppMessage({ body, ticket });
|
||||
}
|
||||
|
||||
// if (!ticket) {
|
||||
// return res.status(404).json({ error: "No ticket found with this ID" });
|
||||
// }
|
||||
const newMessage = {
|
||||
id: sentMessage.id.id,
|
||||
body,
|
||||
fromMe,
|
||||
read,
|
||||
mediaType: sentMessage.type,
|
||||
mediaUrl: media?.filename
|
||||
};
|
||||
|
||||
// if (!ticket.whatsappId) {
|
||||
// const defaultWhatsapp = await Whatsapp.findOne({
|
||||
// where: { default: true }
|
||||
// });
|
||||
const message = await CreateMessageService({
|
||||
messageData: newMessage,
|
||||
ticketId: ticket.id
|
||||
});
|
||||
|
||||
// if (!defaultWhatsapp) {
|
||||
// return res
|
||||
// .status(404)
|
||||
// .json({ error: "No default WhatsApp found. Check Connection page." });
|
||||
// }
|
||||
const io = getIO();
|
||||
io.to(ticketId).to("notification").emit("appMessage", {
|
||||
action: "create",
|
||||
message,
|
||||
ticket,
|
||||
contact: ticket.contact
|
||||
});
|
||||
|
||||
// await ticket.setWhatsapp(defaultWhatsapp);
|
||||
// }
|
||||
|
||||
// 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);
|
||||
await SetTicketMessagesAsRead(ticket);
|
||||
|
||||
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 Contact = require("../models/Contact");
|
||||
const User = require("../models/User");
|
||||
const Whatsapp = require("../models/Whatsapp");
|
||||
|
||||
const Ticket = require("../models/Ticket");
|
||||
const { getIO } = require("../libs/socket");
|
||||
const { getWbot } = require("../libs/wbot");
|
||||
const Sequelize = require("sequelize");
|
||||
|
||||
const { MessageMedia } = require("whatsapp-web.js");
|
||||
|
||||
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,
|
||||
});
|
||||
};
|
||||
|
||||
exports.index = async (req, res, next) => {
|
||||
const { ticketId } = req.params;
|
||||
const { searchParam = "", pageNumber = 1 } = req.query;
|
||||
|
||||
const whereCondition = {
|
||||
body: Sequelize.where(
|
||||
Sequelize.fn("LOWER", Sequelize.col("body")),
|
||||
"LIKE",
|
||||
"%" + searchParam.toLowerCase() + "%"
|
||||
),
|
||||
};
|
||||
|
||||
const limit = 20;
|
||||
const offset = limit * (pageNumber - 1);
|
||||
|
||||
const ticket = await Ticket.findByPk(ticketId, {
|
||||
include: [
|
||||
{
|
||||
model: Contact,
|
||||
as: "contact",
|
||||
include: "extraInfo",
|
||||
attributes: ["id", "name", "number", "profilePicUrl"],
|
||||
},
|
||||
{
|
||||
model: User,
|
||||
as: "user",
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
if (!ticket) {
|
||||
return res.status(404).json({ error: "No ticket found with this ID" });
|
||||
}
|
||||
|
||||
await setMessagesAsRead(ticket);
|
||||
|
||||
const ticketMessages = await ticket.getMessages({
|
||||
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,
|
||||
});
|
||||
};
|
||||
|
||||
exports.store = async (req, res, next) => {
|
||||
const io = getIO();
|
||||
|
||||
const { ticketId } = req.params;
|
||||
const message = req.body;
|
||||
const media = req.file;
|
||||
let sentMessage;
|
||||
|
||||
const ticket = await Ticket.findByPk(ticketId, {
|
||||
include: [
|
||||
{
|
||||
model: Contact,
|
||||
as: "contact",
|
||||
attributes: ["number", "name", "profilePicUrl"],
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
if (!ticket) {
|
||||
return res.status(404).json({ error: "No ticket found with this ID" });
|
||||
}
|
||||
|
||||
if (!ticket.whatsappId) {
|
||||
const defaultWhatsapp = await Whatsapp.findOne({
|
||||
where: { default: true },
|
||||
});
|
||||
|
||||
if (!defaultWhatsapp) {
|
||||
return res
|
||||
.status(404)
|
||||
.json({ error: "No default WhatsApp found. Check Connection page." });
|
||||
}
|
||||
|
||||
await ticket.setWhatsapp(defaultWhatsapp);
|
||||
}
|
||||
|
||||
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: ticket,
|
||||
contact: ticket.contact,
|
||||
});
|
||||
|
||||
await setMessagesAsRead(ticket);
|
||||
|
||||
return res.status(200).json({ newMessage, ticket });
|
||||
}
|
||||
|
||||
return res
|
||||
.status(500)
|
||||
.json({ error: "Cannot sent whatsapp message. Check connection page." });
|
||||
};
|
||||
const Message = require("../models/Message");
|
||||
const Contact = require("../models/Contact");
|
||||
const User = require("../models/User");
|
||||
const Whatsapp = require("../models/Whatsapp");
|
||||
|
||||
const Ticket = require("../models/Ticket");
|
||||
const { getIO } = require("../libs/socket");
|
||||
const { getWbot } = require("../libs/wbot");
|
||||
const Sequelize = require("sequelize");
|
||||
|
||||
const { MessageMedia } = require("whatsapp-web.js");
|
||||
|
||||
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
|
||||
});
|
||||
};
|
||||
|
||||
exports.index = async (req, res, next) => {
|
||||
const { ticketId } = req.params;
|
||||
const { searchParam = "", pageNumber = 1 } = req.query;
|
||||
|
||||
const whereCondition = {
|
||||
body: Sequelize.where(
|
||||
Sequelize.fn("LOWER", Sequelize.col("body")),
|
||||
"LIKE",
|
||||
"%" + searchParam.toLowerCase() + "%"
|
||||
)
|
||||
};
|
||||
|
||||
const limit = 20;
|
||||
const offset = limit * (pageNumber - 1);
|
||||
|
||||
const ticket = await Ticket.findByPk(ticketId, {
|
||||
include: [
|
||||
{
|
||||
model: Contact,
|
||||
as: "contact",
|
||||
include: "extraInfo",
|
||||
attributes: ["id", "name", "number", "profilePicUrl"]
|
||||
},
|
||||
{
|
||||
model: User,
|
||||
as: "user"
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
if (!ticket) {
|
||||
return res.status(404).json({ error: "No ticket found with this ID" });
|
||||
}
|
||||
|
||||
await setMessagesAsRead(ticket);
|
||||
|
||||
const ticketMessages = await ticket.getMessages({
|
||||
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
|
||||
});
|
||||
};
|
||||
|
||||
exports.store = async (req, res, next) => {
|
||||
const io = getIO();
|
||||
|
||||
const { ticketId } = req.params;
|
||||
const message = req.body;
|
||||
const media = req.file;
|
||||
let sentMessage;
|
||||
|
||||
const ticket = await Ticket.findByPk(ticketId, {
|
||||
include: [
|
||||
{
|
||||
model: Contact,
|
||||
as: "contact",
|
||||
attributes: ["number", "name", "profilePicUrl"]
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
if (!ticket) {
|
||||
return res.status(404).json({ error: "No ticket found with this ID" });
|
||||
}
|
||||
|
||||
if (!ticket.whatsappId) {
|
||||
const defaultWhatsapp = await Whatsapp.findOne({
|
||||
where: { default: true }
|
||||
});
|
||||
|
||||
if (!defaultWhatsapp) {
|
||||
return res
|
||||
.status(404)
|
||||
.json({ error: "No default WhatsApp found. Check Connection page." });
|
||||
}
|
||||
|
||||
await ticket.setWhatsapp(defaultWhatsapp);
|
||||
}
|
||||
|
||||
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: ticket,
|
||||
contact: ticket.contact
|
||||
});
|
||||
|
||||
await setMessagesAsRead(ticket);
|
||||
|
||||
return res.status(200).json({ newMessage, ticket });
|
||||
}
|
||||
|
||||
return res
|
||||
.status(500)
|
||||
.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 { getIO } from "./socket";
|
||||
import Whatsapp from "../models/Whatsapp";
|
||||
import AppError from "../errors/AppError";
|
||||
|
||||
interface Session extends Client {
|
||||
id?: number;
|
||||
@@ -10,7 +11,6 @@ interface Session extends Client {
|
||||
const sessions: Session[] = [];
|
||||
|
||||
export const initWbot = async (whatsapp: Whatsapp): Promise<void> => {
|
||||
console.log("starting");
|
||||
try {
|
||||
const io = getIO();
|
||||
const sessionName = whatsapp.name;
|
||||
@@ -26,7 +26,7 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<void> => {
|
||||
sessions.splice(sessionIndex, 1);
|
||||
}
|
||||
|
||||
const wbot = new Client({
|
||||
const wbot: Session = new Client({
|
||||
session: sessionCfg,
|
||||
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);
|
||||
|
||||
if (sessionIndex === -1) {
|
||||
console.log("This Wbot session is not initialized");
|
||||
return null;
|
||||
throw new AppError("This WhatsApp session is not initialized.");
|
||||
}
|
||||
return sessions[sessionIndex];
|
||||
};
|
||||
|
||||
@@ -10,6 +10,7 @@ interface Request {
|
||||
name: string;
|
||||
number: string;
|
||||
email?: string;
|
||||
profilePicUrl?: string;
|
||||
extraInfo?: ExtraInfo[];
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,18 @@
|
||||
import AppError from "../../errors/AppError";
|
||||
import Message from "../../models/Message";
|
||||
import ShowTicketService from "../TicketServices/ShowTicketService";
|
||||
|
||||
interface MessageData {
|
||||
id: string;
|
||||
body: string;
|
||||
fromMe: boolean;
|
||||
read: boolean;
|
||||
mediaType: string;
|
||||
mediaUrl?: string;
|
||||
}
|
||||
interface Request {
|
||||
ticketId: string;
|
||||
messageData: Message;
|
||||
ticketId: string | number;
|
||||
messageData: MessageData;
|
||||
}
|
||||
|
||||
const CreateMessageService = async ({
|
||||
@@ -13,10 +22,10 @@ const CreateMessageService = async ({
|
||||
const ticket = await ShowTicketService(ticketId);
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
@@ -21,7 +21,7 @@ const ShowTicketService = async (id: string | number): Promise<Ticket> => {
|
||||
});
|
||||
|
||||
if (!ticket) {
|
||||
throw new AppError("No ticket found with this conditions.", 404);
|
||||
throw new AppError("No ticket found with this ID");
|
||||
}
|
||||
|
||||
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,
|
||||
contact: Contact
|
||||
) => {
|
||||
const io = getIO();
|
||||
let newMessage: Message;
|
||||
|
||||
if (msg.hasMedia) {
|
||||
@@ -138,6 +137,7 @@ const handleMessage = async (
|
||||
await ticket.update({ lastMessage: msg.body });
|
||||
}
|
||||
|
||||
const io = getIO();
|
||||
io.to(ticket.id.toString()).to("notification").emit("appMessage", {
|
||||
action: "create",
|
||||
message: newMessage,
|
||||
@@ -157,7 +157,7 @@ const wbotMessageListener = (whatsapp: Whatsapp): void => {
|
||||
}
|
||||
|
||||
wbot.on("message_create", async msg => {
|
||||
// console.log(msg);
|
||||
console.log(msg.type);
|
||||
|
||||
if (
|
||||
msg.from === "status@broadcast" ||
|
||||
|
||||
Reference in New Issue
Block a user