diff --git a/backend/src/controllers/MessageController.ts b/backend/src/controllers/MessageController.ts index f9641d6..16fa665 100644 --- a/backend/src/controllers/MessageController.ts +++ b/backend/src/controllers/MessageController.ts @@ -35,7 +35,7 @@ export const index = async (req: Request, res: Response): Promise => { return res.json({ count, messages, ticket, hasMore }); }; -export const store = async (req: Request, res: Response): Promise => { +export const store = async (req: Request, res: Response): Promise => { const { ticketId } = req.params; const { body, quotedMsg }: MessageData = req.body; @@ -43,6 +43,8 @@ export const store = async (req: Request, res: Response): Promise => { const ticket = await ShowTicketService(ticketId); + console.log('TICKET ID: ', ticketId) + SetTicketMessagesAsRead(ticket); if (medias) { diff --git a/backend/src/controllers/TicketController.ts b/backend/src/controllers/TicketController.ts index 8e8db5e..eb3bc3a 100644 --- a/backend/src/controllers/TicketController.ts +++ b/backend/src/controllers/TicketController.ts @@ -92,6 +92,7 @@ export const index = async (req: Request, res: Response): Promise => { export const store = async (req: Request, res: Response): Promise => { const { contactId, status, userId }: TicketData = req.body; + console.log('TICKET CREATE: ', 'contactId: ', contactId, ' | status: ', status, ' | userId: ', userId) // test del let ticket = await Ticket.findOne({ where: { contactId, status: 'queueChoice' } }); diff --git a/backend/src/libs/wbot.ts b/backend/src/libs/wbot.ts index c024161..8e216be 100644 --- a/backend/src/libs/wbot.ts +++ b/backend/src/libs/wbot.ts @@ -238,39 +238,7 @@ export const initWbot = async (whatsapp: Whatsapp, backupSessionRestore: boolean console.log(' PASSOU NO TIMEOUT whatsapp.id: ', whatsapp.id) - } - - - - - // for (let i = 0; i <= 25; i++) { - - // // console.log('Send: ', i, ' | miliseconds[Math.floor(Math.random() * miliseconds.length)]): ', miliseconds[Math.floor(Math.random() * miliseconds.length)]) - - // try { - - // let stat = await wbot.getState(); - - // console.log('GET WHATSAPP STATE: ', stat) - - // if (stat !== 'CONNECTED') break - - // await wbot.sendMessage(`5517988325936@c.us`, `*@!ping*<>${whatsapp.name}\nIndex: ${i}`); - - // await new Promise(r => setTimeout(r, miliseconds[Math.floor(Math.random() * miliseconds.length)])); - - // } catch (error) { - - // console.log(`Error on try send menssage boot from session: ${whatsapp.name}: ${error}`) - - // break - - // } - - - // } - - + } }); } catch (err) { logger.error(`${err}`); diff --git a/backend/src/server.ts b/backend/src/server.ts index 9941d1b..efa24d5 100644 --- a/backend/src/server.ts +++ b/backend/src/server.ts @@ -17,5 +17,5 @@ if (global.gc) { } initIO(server); -// StartAllWhatsAppsSessions(); +StartAllWhatsAppsSessions(); gracefulShutdown(server); diff --git a/backend/src/services/MessageServices/CreateMessageService.ts b/backend/src/services/MessageServices/CreateMessageService.ts index 4ccac9b..9dc9a4c 100644 --- a/backend/src/services/MessageServices/CreateMessageService.ts +++ b/backend/src/services/MessageServices/CreateMessageService.ts @@ -18,6 +18,9 @@ interface Request { } const CreateMessageService = async ({ messageData }: Request): Promise => { + + console.log('UPSERT MESSAGE messageData: ', messageData) + await Message.upsert(messageData); const message = await Message.findByPk(messageData.id, { @@ -55,6 +58,9 @@ const CreateMessageService = async ({ messageData }: Request): Promise // + console.log('Entrou no create message') + + const io = getIO(); io.to(message.ticketId.toString()) .to(message.ticket.status) diff --git a/backend/src/services/WbotServices/SendWhatsAppMessage.ts b/backend/src/services/WbotServices/SendWhatsAppMessage.ts index bac22dd..f615d3b 100644 --- a/backend/src/services/WbotServices/SendWhatsAppMessage.ts +++ b/backend/src/services/WbotServices/SendWhatsAppMessage.ts @@ -50,16 +50,14 @@ const SendWhatsAppMessage = async ({ if (quotedMsg) { await GetWbotMessage(ticket, quotedMsg.id); quotedMsgSerializedId = SerializeWbotMsgId(ticket, quotedMsg); - } + } + + const whatsapp = await Whatsapp.findByPk(ticket.whatsappId); - const whatsapp = await Whatsapp.findByPk(ticket.whatsappId); - if (whatsapp && whatsapp.status != 'CONNECTED') { let whatsapps = await wbotByUserQueue(ticket.userId) - - // console.log('whatsapps whatsapps whatsapps: ', JSON.parse(JSON.stringify(whatsapps))) - + if (whatsapps.length > 0) { if (whatsapps.length > 1) { @@ -93,9 +91,10 @@ const SendWhatsAppMessage = async ({ console.timeEnd(timetaken) return sentMessage; + } catch (err) { - const whatsapp = await ShowWhatsAppService(ticket.whatsappId); + const whatsapp = await ShowWhatsAppService(ticket.whatsappId); throw new AppError("ERR_SENDING_WAPP_MSG"); } diff --git a/backend/src/services/WbotServices/wbotMessageListener.ts b/backend/src/services/WbotServices/wbotMessageListener.ts index 2622097..2f7678e 100644 --- a/backend/src/services/WbotServices/wbotMessageListener.ts +++ b/backend/src/services/WbotServices/wbotMessageListener.ts @@ -159,8 +159,11 @@ const verifyMessage = async ( contact: Contact ) => { + console.log('Entrou no verify message...') + const quotedMsg = await verifyQuotedMessage(msg); + const messageData = { id: msg.id.id, ticketId: ticket.id, @@ -175,10 +178,6 @@ const verifyMessage = async ( await ticket.update({ lastMessage: msg.body }); - // TEST DEL - // await updateTicketCacheByTicketId(ticket.id, { lastMessage: msg.body, updatedAt: new Date(ticket.updatedAt).toISOString() }) - // - await CreateMessageService({ messageData }); }; @@ -373,42 +372,18 @@ const botSendMessage = (ticket: Ticket, contact: Contact, wbot: Session, msg: st // const _clear_lst = () => { // if (lst.length <= 199) return - + // const chunk: any = Math.floor((lst.length / 2)) // lst = lst.slice(chunk, chunk + lst.length); - + // } - + const handleMessage = async ( msg: WbotMessage, wbot: Session ): Promise => { - // TEST DEL MULTI SESSION - - // _clear_lst() - - // let index = lst.findIndex((x: any) => x.id == msg.id.id) - - // console.log('INDEX: ', index) - - // if (index == -1) { - - // lst.push({ id: msg.id.id }) - - // } - // else { - // console.log('IGNORED ID: ', msg.id.id) - - // return - // } - - // // console.log('LIST OF ID MESSAGE lst: ', lst) - - // console.log('PASSOU.................................FROM: ', msg.from.split("@")[0], ' | ID: ', msg.id.id) - - if (!isValidMsg(msg)) { return; } @@ -424,15 +399,21 @@ const handleMessage = async ( // media messages sent from me from cell phone, first comes with "hasMedia = false" and type = "image/ptt/etc" // in this case, return and let this message be handled by "media_uploaded" event, when it will have "hasMedia = true" - + if (!msg.hasMedia && msg.type !== "chat" && msg.type !== "vcard") return; + msgContact = await wbot.getContactById(msg.to); + + console.log('1 --------------> msgContat: ', JSON.parse(JSON.stringify(msgContact))) + console.log(' # msg.type: ', msg.type ) + } else { msgContact = await msg.getContact(); - // + console.log('2 --------------> msgContat: ', JSON.parse(JSON.stringify(msgContact))) + // console.log(`\n <<<<<<<<<< RECEIVING MESSAGE: Parcial msg and msgContact info: msgContact.name: ${msgContact.name} @@ -449,6 +430,7 @@ const handleMessage = async ( const chat = await msg.getChat(); + console.log('----------> chat: ', JSON.parse(JSON.stringify(chat))) if (chat.isGroup) { let msgGroupContact; @@ -467,6 +449,8 @@ const handleMessage = async ( const contact = await verifyContact(msgContact); + console.log('----------> contact: ', JSON.parse(JSON.stringify(contact))) + if (unreadMessages === 0 && whatsapp.farewellMessage && whatsapp.farewellMessage === msg.body) return; @@ -483,9 +467,6 @@ const handleMessage = async ( // Para responder para o cliente pelo mesmo whatsapp que ele enviou a mensagen if (wbot.id != ticket.whatsappId) { - - - await ticket.update({ whatsappId: wbot.id }); } // @@ -819,8 +800,8 @@ const handleMessage = async ( // if (msg.body.trim() == 'broken') { // throw new Error('Throw makes it go boom!') // } -// - + // + } catch (err) { Sentry.captureException(err); logger.error(`Error handling whatsapp message: Err: ${err}`); @@ -848,7 +829,7 @@ const handleMessage = async ( } else if (`${err}`.includes('[object Object]')) { - await _restore(whatsapp, 'auto_object_error') + await _restore(whatsapp, 'auto_object_error') } @@ -876,6 +857,8 @@ const handleMsgAck = async (msg: WbotMessage, ack: MessageAck) => { } await messageToUpdate.update({ ack }); + console.log('ACK messageToUpdate: ', JSON.parse(JSON.stringify(messageToUpdate))) + io.to(messageToUpdate.ticketId.toString()).emit("appMessage", { action: "update", message: messageToUpdate @@ -888,6 +871,7 @@ const handleMsgAck = async (msg: WbotMessage, ack: MessageAck) => { }; const wbotMessageListener = (wbot: Session): void => { + wbot.on("message_create", async msg => { handleMessage(msg, wbot); }); diff --git a/frontend/src/components/MessageInput/index.js b/frontend/src/components/MessageInput/index.js index f9b5fff..59ba149 100644 --- a/frontend/src/components/MessageInput/index.js +++ b/frontend/src/components/MessageInput/index.js @@ -319,6 +319,8 @@ const MessageInput = ({ ticketStatus }) => { quotedMsg: replyingMessage, }; try { + + // console.log('message: ', message) await api.post(`/messages/${ticketId}`, message); } catch (err) { toastError(err); diff --git a/frontend/src/components/MessagesList/index.js b/frontend/src/components/MessagesList/index.js index 94b6fab..75f778a 100644 --- a/frontend/src/components/MessagesList/index.js +++ b/frontend/src/components/MessagesList/index.js @@ -364,6 +364,9 @@ const MessagesList = ({ ticketId, isGroup }) => { socket.on("appMessage", (data) => { if (data.action === "create") { + + console.log('ADD_MESSAGE: ', data.message) + dispatch({ type: "ADD_MESSAGE", payload: data.message }); scrollToBottom();