From 01478d91d111323a40a2b96ac27f94627681135c Mon Sep 17 00:00:00 2001 From: adriano Date: Wed, 25 Jan 2023 17:44:02 -0300 Subject: [PATCH] Desenvolvimento parcial do recebimento de midia --- backend/src/libs/socket.ts | 53 ++++++++++- backend/src/server.ts | 2 +- .../WbotServices/StartAllWhatsAppsSessions.ts | 9 -- .../WbotServices/wbotMessageListener.ts | 90 +++++++++++-------- .../WhatsappService/ShowWhatsAppService.ts | 2 + 5 files changed, 107 insertions(+), 49 deletions(-) diff --git a/backend/src/libs/socket.ts b/backend/src/libs/socket.ts index ea923b4..53cf1f2 100644 --- a/backend/src/libs/socket.ts +++ b/backend/src/libs/socket.ts @@ -15,6 +15,8 @@ import { splitDateTime } from "../helpers/SplitDateTime"; import format from 'date-fns/format'; import ptBR from 'date-fns/locale/pt-BR'; import ListUserOnlineOffline from "../services/UserServices/ListUsersOnlineOfflineService"; +import { handleMessage } from "../services/WbotServices/wbotMessageListener"; +import { join } from "path"; let count: number = 0 let listOnline: any[] = [] @@ -46,8 +48,51 @@ export const initIO = (httpServer: Server): SocketIO => { logger.info("Client Connected"); + + + + + + socket.on("message_from_client", () => { + console.log('message_from_client!') + + socket.emit('message_from_server', 'Sent an event from the server!'); + }) + + socket.on("message_create", async (data: any) => { + + console.log('DATA: ', data) + + handleMessage(data.msg, data); + + }); + + socket.on("media_uploaded", async (file: any, callback: any) => { + + console.log('_______file: ', file); + + // handleMessage(data.msg, data); + + try { + + writeFileAsync(join(__dirname, "..", "..", "..", "..", "..", "public", file.filename), file.data, "base64"); + + } catch (err) { + + logger.error(`There was an error on try get data: ${err}`); + + } + + }); + + + + + + + socket.on("online", (userId: any) => { - + // console.log('userId: ', userId) obj.uuid = uuidv4() @@ -81,7 +126,7 @@ export const initIO = (httpServer: Server): SocketIO => { if (index == -1) { listOnlineAux.push({ 'id': userId }) } - else { + else { return } @@ -204,6 +249,10 @@ export const getIO = (): SocketIO => { }; + +function writeFileAsync(arg0: any, data: any, arg2: string) { + throw new Error("Function not implemented."); +} // exports.listOnlineUsers = listUserId // exports.listUserId diff --git a/backend/src/server.ts b/backend/src/server.ts index efa24d5..9941d1b 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/WbotServices/StartAllWhatsAppsSessions.ts b/backend/src/services/WbotServices/StartAllWhatsAppsSessions.ts index a3bccef..037c55a 100644 --- a/backend/src/services/WbotServices/StartAllWhatsAppsSessions.ts +++ b/backend/src/services/WbotServices/StartAllWhatsAppsSessions.ts @@ -23,16 +23,7 @@ import openSocket from "socket.io-client"; let counter = 0 export const StartAllWhatsAppsSessions = async (): Promise => { - - - console.log('KKKKKKKKKKKKKKKKKKKKKKKK') - - const io = require("socket.io-client"); - const socket = io("http://localhost:8020/", { - withCredentials: true - }); - const cacheLength = await cacheSize() diff --git a/backend/src/services/WbotServices/wbotMessageListener.ts b/backend/src/services/WbotServices/wbotMessageListener.ts index 2f7678e..ff47cf8 100644 --- a/backend/src/services/WbotServices/wbotMessageListener.ts +++ b/backend/src/services/WbotServices/wbotMessageListener.ts @@ -69,8 +69,9 @@ interface Session extends Client { const writeFileAsync = promisify(writeFile); -const verifyContact = async (msgContact: WbotContact): Promise => { - const profilePicUrl = await msgContact.getProfilePicUrl(); +const verifyContact = async (msgContact: any): Promise => { + // const profilePicUrl = await msgContact.getProfilePicUrl(); + const profilePicUrl = msgContact.getProfilePicUrl; const contactData = { name: msgContact.name || msgContact.pushname || msgContact.id.user, @@ -85,11 +86,14 @@ const verifyContact = async (msgContact: WbotContact): Promise => { }; const verifyQuotedMessage = async ( - msg: WbotMessage + msg: any, + _quotedMsg?: any ): Promise => { if (!msg.hasQuotedMsg) return null; - const wbotQuotedMsg = await msg.getQuotedMessage(); + // const wbotQuotedMsg = await msg.getQuotedMessage(); + + const wbotQuotedMsg = _quotedMsg; const quotedMsg = await Message.findOne({ where: { id: wbotQuotedMsg.id.id } @@ -101,11 +105,12 @@ const verifyQuotedMessage = async ( }; const verifyMediaMessage = async ( - msg: WbotMessage, + msg: any, ticket: Ticket, - contact: Contact + contact: Contact, + quotedMsg?: any, ): Promise => { - const quotedMsg = await verifyQuotedMessage(msg); + // const quotedMsg = await verifyQuotedMessage(msg); const media = await msg.downloadMedia(); @@ -144,7 +149,8 @@ const verifyMediaMessage = async ( read: msg.fromMe, mediaUrl: media.filename, mediaType: media.mimetype.split("/")[0], - quotedMsgId: quotedMsg?.id + quotedMsgId: quotedMsg + // quotedMsgId: quotedMsg?.id }; await ticket.update({ lastMessage: msg.body || media.filename }); @@ -154,15 +160,18 @@ const verifyMediaMessage = async ( }; const verifyMessage = async ( - msg: WbotMessage, + msg: WbotMessage, ticket: Ticket, - contact: Contact + contact: Contact, + quotedMsg?: any, ) => { console.log('Entrou no verify message...') - const quotedMsg = await verifyQuotedMessage(msg); + // const quotedMsg = await verifyQuotedMessage(msg); + + // const quotedMsg = await verifyQuotedMessage(msg); const messageData = { id: msg.id.id, @@ -172,7 +181,8 @@ const verifyMessage = async ( fromMe: msg.fromMe, mediaType: msg.type, read: msg.fromMe, - quotedMsgId: quotedMsg?.id + quotedMsgId: quotedMsg + // quotedMsgId: quotedMsg?.id }; @@ -380,8 +390,8 @@ const botSendMessage = (ticket: Ticket, contact: Contact, wbot: Session, msg: st // } const handleMessage = async ( - msg: WbotMessage, - wbot: Session + msg: any, + wbot: any ): Promise => { if (!isValidMsg(msg)) { @@ -389,8 +399,8 @@ const handleMessage = async ( } try { - let msgContact: WbotContact; - let groupContact: Contact | undefined; + let msgContact: any = wbot.msgContact + // let groupContact: Contact | undefined; if (msg.fromMe) { // messages sent automatically by wbot have a special character in front of it @@ -402,16 +412,16 @@ const handleMessage = async ( if (!msg.hasMedia && msg.type !== "chat" && msg.type !== "vcard") return; - msgContact = await wbot.getContactById(msg.to); + // msgContact = await wbot.getContactById(msg.to); - console.log('1 --------------> msgContat: ', JSON.parse(JSON.stringify(msgContact))) - console.log(' # msg.type: ', msg.type ) + // console.log('1 --------------> msgContat: ', JSON.parse(JSON.stringify(msgContact))) + // console.log(' # msg.type: ', msg.type ) } else { - msgContact = await msg.getContact(); + // msgContact = await msg.getContact(); - console.log('2 --------------> msgContat: ', JSON.parse(JSON.stringify(msgContact))) + // console.log('2 --------------> msgContat: ', JSON.parse(JSON.stringify(msgContact))) // console.log(`\n <<<<<<<<<< RECEIVING MESSAGE: @@ -428,28 +438,34 @@ const handleMessage = async ( } - const chat = await msg.getChat(); + // const chat = await msg.getChat(); + const chat = wbot.chat - console.log('----------> chat: ', JSON.parse(JSON.stringify(chat))) + // console.log('----------> chat: ', JSON.parse(JSON.stringify(chat))) - if (chat.isGroup) { - let msgGroupContact; + // if (chat.isGroup) { + // let msgGroupContact; + + // if (msg.fromMe) { + // msgGroupContact = await wbot.getContactById(msg.to); + // } else { + // msgGroupContact = await wbot.getContactById(msg.from); + // } + + // groupContact = await verifyContact(msgGroupContact); + // } + - if (msg.fromMe) { - msgGroupContact = await wbot.getContactById(msg.to); - } else { - msgGroupContact = await wbot.getContactById(msg.from); - } - groupContact = await verifyContact(msgGroupContact); - } const whatsapp = await ShowWhatsAppService(wbot.id!); + + // const whatsapp = await ShowWhatsAppService(46); const unreadMessages = msg.fromMe ? 0 : chat.unreadCount; const contact = await verifyContact(msgContact); - console.log('----------> contact: ', JSON.parse(JSON.stringify(contact))) + // console.log('----------> contact: ', JSON.parse(JSON.stringify(contact))) @@ -459,7 +475,7 @@ const handleMessage = async ( contact, wbot.id!, unreadMessages, - groupContact + // groupContact ); // @@ -471,11 +487,11 @@ const handleMessage = async ( } // - + if (msg.hasMedia) { - await verifyMediaMessage(msg, ticket, contact); + await verifyMediaMessage(msg, ticket, contact, wbot.quotedMsg); } else { - await verifyMessage(msg, ticket, contact); + await verifyMessage(msg, ticket, contact, wbot.quotedMsg); } //setTimeout(()=>verifyQueue(wbot, msg, ticket, contact), 3000); diff --git a/backend/src/services/WhatsappService/ShowWhatsAppService.ts b/backend/src/services/WhatsappService/ShowWhatsAppService.ts index c652be9..db386a1 100644 --- a/backend/src/services/WhatsappService/ShowWhatsAppService.ts +++ b/backend/src/services/WhatsappService/ShowWhatsAppService.ts @@ -14,6 +14,8 @@ const ShowWhatsAppService = async (id: string | number): Promise => { order: [["queues", "id", "ASC"]] }); + console.log('kkkkkkkkkkkkkkkkkkkk: ', whatsapp) + if (!whatsapp) { throw new AppError("ERR_NO_WAPP_FOUND", 404); }