diff --git a/backend/src/controllers/ContactController.ts b/backend/src/controllers/ContactController.ts index 6242fd4..3b50675 100644 --- a/backend/src/controllers/ContactController.ts +++ b/backend/src/controllers/ContactController.ts @@ -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 => { - 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", diff --git a/backend/src/controllers/MessageController.ts b/backend/src/controllers/MessageController.ts index 4d22ec7..a574cac 100644 --- a/backend/src/controllers/MessageController.ts +++ b/backend/src/controllers/MessageController.ts @@ -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 => { const { ticketId } = req.params; const { searchParam, pageNumber } = req.query as IndexQuery; @@ -57,134 +42,49 @@ export const index = async (req: Request, res: Response): Promise => { 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 => { - // 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." }); -// }; diff --git a/backend/src/controllersOld/MessageController.js b/backend/src/controllersOld/MessageController.js index 33a04b7..a34cd3f 100644 --- a/backend/src/controllersOld/MessageController.js +++ b/backend/src/controllersOld/MessageController.js @@ -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." }); +}; diff --git a/backend/src/helpers/GetTicketWbot.ts b/backend/src/helpers/GetTicketWbot.ts new file mode 100644 index 0000000..50f6422 --- /dev/null +++ b/backend/src/helpers/GetTicketWbot.ts @@ -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 => { + 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; diff --git a/backend/src/helpers/SetTicketMessagesAsRead.ts b/backend/src/helpers/SetTicketMessagesAsRead.ts new file mode 100644 index 0000000..b78cdd1 --- /dev/null +++ b/backend/src/helpers/SetTicketMessagesAsRead.ts @@ -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 => { + 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; diff --git a/backend/src/libs/wbot.ts b/backend/src/libs/wbot.ts index 68ef54b..49c0224 100644 --- a/backend/src/libs/wbot.ts +++ b/backend/src/libs/wbot.ts @@ -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 => { - console.log("starting"); try { const io = getIO(); const sessionName = whatsapp.name; @@ -26,7 +26,7 @@ export const initWbot = async (whatsapp: Whatsapp): Promise => { 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 => { } }; -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]; }; diff --git a/backend/src/services/ContactServices/CreateContactService.ts b/backend/src/services/ContactServices/CreateContactService.ts index ecc8955..e1d258d 100644 --- a/backend/src/services/ContactServices/CreateContactService.ts +++ b/backend/src/services/ContactServices/CreateContactService.ts @@ -10,6 +10,7 @@ interface Request { name: string; number: string; email?: string; + profilePicUrl?: string; extraInfo?: ExtraInfo[]; } diff --git a/backend/src/services/MessageServices/CreateMessageService.ts b/backend/src/services/MessageServices/CreateMessageService.ts index 8957672..700e07a 100644 --- a/backend/src/services/MessageServices/CreateMessageService.ts +++ b/backend/src/services/MessageServices/CreateMessageService.ts @@ -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; }; diff --git a/backend/src/services/TicketServices/ShowTicketService.ts b/backend/src/services/TicketServices/ShowTicketService.ts index 4103685..9379aff 100644 --- a/backend/src/services/TicketServices/ShowTicketService.ts +++ b/backend/src/services/TicketServices/ShowTicketService.ts @@ -21,7 +21,7 @@ const ShowTicketService = async (id: string | number): Promise => { }); if (!ticket) { - throw new AppError("No ticket found with this conditions.", 404); + throw new AppError("No ticket found with this ID"); } return ticket; diff --git a/backend/src/services/WbotServices/CheckIsValidContact.ts b/backend/src/services/WbotServices/CheckIsValidContact.ts new file mode 100644 index 0000000..dc9edd6 --- /dev/null +++ b/backend/src/services/WbotServices/CheckIsValidContact.ts @@ -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 => { + 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; diff --git a/backend/src/services/WbotServices/GetProfilePicUrl.ts b/backend/src/services/WbotServices/GetProfilePicUrl.ts new file mode 100644 index 0000000..6e5829a --- /dev/null +++ b/backend/src/services/WbotServices/GetProfilePicUrl.ts @@ -0,0 +1,14 @@ +import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp"; +import { getWbot } from "../../libs/wbot"; + +const GetProfilePicUrl = async (number: string): Promise => { + const defaultWhatsapp = await GetDefaultWhatsApp(); + + const wbot = getWbot(defaultWhatsapp.id); + + const profilePicUrl = await wbot.getProfilePicUrl(`${number}@c.us`); + + return profilePicUrl; +}; + +export default GetProfilePicUrl; diff --git a/backend/src/services/WbotServices/SendWhatsAppMedia.ts b/backend/src/services/WbotServices/SendWhatsAppMedia.ts new file mode 100644 index 0000000..4495e4d --- /dev/null +++ b/backend/src/services/WbotServices/SendWhatsAppMedia.ts @@ -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 => { + 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; diff --git a/backend/src/services/WbotServices/SendWhatsAppMessage.ts b/backend/src/services/WbotServices/SendWhatsAppMessage.ts new file mode 100644 index 0000000..403b915 --- /dev/null +++ b/backend/src/services/WbotServices/SendWhatsAppMessage.ts @@ -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 => { + 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; diff --git a/backend/src/services/WbotServices/wbotMessageListener.ts b/backend/src/services/WbotServices/wbotMessageListener.ts index 2ab9216..d1dc5ed 100644 --- a/backend/src/services/WbotServices/wbotMessageListener.ts +++ b/backend/src/services/WbotServices/wbotMessageListener.ts @@ -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" ||