Desenvolvimento parcial do recebimento de midia
							parent
							
								
									5c44656328
								
							
						
					
					
						commit
						01478d91d1
					
				| 
						 | 
					@ -15,6 +15,8 @@ import { splitDateTime } from "../helpers/SplitDateTime";
 | 
				
			||||||
import format from 'date-fns/format';
 | 
					import format from 'date-fns/format';
 | 
				
			||||||
import ptBR from 'date-fns/locale/pt-BR';
 | 
					import ptBR from 'date-fns/locale/pt-BR';
 | 
				
			||||||
import ListUserOnlineOffline from "../services/UserServices/ListUsersOnlineOfflineService";
 | 
					import ListUserOnlineOffline from "../services/UserServices/ListUsersOnlineOfflineService";
 | 
				
			||||||
 | 
					import { handleMessage } from "../services/WbotServices/wbotMessageListener";
 | 
				
			||||||
 | 
					import { join } from "path";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
let count: number = 0
 | 
					let count: number = 0
 | 
				
			||||||
let listOnline: any[] = []
 | 
					let listOnline: any[] = []
 | 
				
			||||||
| 
						 | 
					@ -46,6 +48,49 @@ export const initIO = (httpServer: Server): SocketIO => {
 | 
				
			||||||
    logger.info("Client Connected");
 | 
					    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) => {
 | 
					    socket.on("online", (userId: any) => {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // console.log('userId: ', userId)
 | 
					      // console.log('userId: ', userId)
 | 
				
			||||||
| 
						 | 
					@ -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.listOnlineUsers = listUserId
 | 
				
			||||||
// exports.listUserId 
 | 
					// exports.listUserId 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -17,5 +17,5 @@ if (global.gc) {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
initIO(server);
 | 
					initIO(server);
 | 
				
			||||||
StartAllWhatsAppsSessions();
 | 
					// StartAllWhatsAppsSessions();
 | 
				
			||||||
gracefulShutdown(server);
 | 
					gracefulShutdown(server);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -25,15 +25,6 @@ let counter = 0
 | 
				
			||||||
export const StartAllWhatsAppsSessions = async (): Promise<void> => {
 | 
					export const StartAllWhatsAppsSessions = async (): Promise<void> => {
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  
 | 
					 | 
				
			||||||
  console.log('KKKKKKKKKKKKKKKKKKKKKKKK')
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  const io = require("socket.io-client");
 | 
					 | 
				
			||||||
  const socket = io("http://localhost:8020/", {
 | 
					 | 
				
			||||||
    withCredentials: true
 | 
					 | 
				
			||||||
  });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  const cacheLength = await cacheSize()
 | 
					  const cacheLength = await cacheSize()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  console.log('cacheSize: ', cacheLength)
 | 
					  console.log('cacheSize: ', cacheLength)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -69,8 +69,9 @@ interface Session extends Client {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const writeFileAsync = promisify(writeFile);
 | 
					const writeFileAsync = promisify(writeFile);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const verifyContact = async (msgContact: WbotContact): Promise<Contact> => {
 | 
					const verifyContact = async (msgContact: any): Promise<Contact> => {
 | 
				
			||||||
  const profilePicUrl = await msgContact.getProfilePicUrl();
 | 
					  // const profilePicUrl = await msgContact.getProfilePicUrl();
 | 
				
			||||||
 | 
					  const profilePicUrl = msgContact.getProfilePicUrl;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const contactData = {
 | 
					  const contactData = {
 | 
				
			||||||
    name: msgContact.name || msgContact.pushname || msgContact.id.user,
 | 
					    name: msgContact.name || msgContact.pushname || msgContact.id.user,
 | 
				
			||||||
| 
						 | 
					@ -85,11 +86,14 @@ const verifyContact = async (msgContact: WbotContact): Promise<Contact> => {
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const verifyQuotedMessage = async (
 | 
					const verifyQuotedMessage = async (
 | 
				
			||||||
  msg: WbotMessage
 | 
					  msg: any,
 | 
				
			||||||
 | 
					  _quotedMsg?: any
 | 
				
			||||||
): Promise<Message | null> => {
 | 
					): Promise<Message | null> => {
 | 
				
			||||||
  if (!msg.hasQuotedMsg) return null;
 | 
					  if (!msg.hasQuotedMsg) return null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const wbotQuotedMsg = await msg.getQuotedMessage();
 | 
					  // const wbotQuotedMsg = await msg.getQuotedMessage();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const wbotQuotedMsg = _quotedMsg;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const quotedMsg = await Message.findOne({
 | 
					  const quotedMsg = await Message.findOne({
 | 
				
			||||||
    where: { id: wbotQuotedMsg.id.id }
 | 
					    where: { id: wbotQuotedMsg.id.id }
 | 
				
			||||||
| 
						 | 
					@ -101,11 +105,12 @@ const verifyQuotedMessage = async (
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const verifyMediaMessage = async (
 | 
					const verifyMediaMessage = async (
 | 
				
			||||||
  msg: WbotMessage,
 | 
					  msg: any,
 | 
				
			||||||
  ticket: Ticket,
 | 
					  ticket: Ticket,
 | 
				
			||||||
  contact: Contact
 | 
					  contact: Contact,
 | 
				
			||||||
 | 
					  quotedMsg?: any,
 | 
				
			||||||
): Promise<Message> => {
 | 
					): Promise<Message> => {
 | 
				
			||||||
  const quotedMsg = await verifyQuotedMessage(msg);
 | 
					  // const quotedMsg = await verifyQuotedMessage(msg);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const media = await msg.downloadMedia();
 | 
					  const media = await msg.downloadMedia();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -144,7 +149,8 @@ const verifyMediaMessage = async (
 | 
				
			||||||
    read: msg.fromMe,
 | 
					    read: msg.fromMe,
 | 
				
			||||||
    mediaUrl: media.filename,
 | 
					    mediaUrl: media.filename,
 | 
				
			||||||
    mediaType: media.mimetype.split("/")[0],
 | 
					    mediaType: media.mimetype.split("/")[0],
 | 
				
			||||||
    quotedMsgId: quotedMsg?.id
 | 
					    quotedMsgId: quotedMsg
 | 
				
			||||||
 | 
					    // quotedMsgId: quotedMsg?.id
 | 
				
			||||||
  };
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  await ticket.update({ lastMessage: msg.body || media.filename });
 | 
					  await ticket.update({ lastMessage: msg.body || media.filename });
 | 
				
			||||||
| 
						 | 
					@ -156,13 +162,16 @@ const verifyMediaMessage = async (
 | 
				
			||||||
const verifyMessage = async (
 | 
					const verifyMessage = async (
 | 
				
			||||||
  msg: WbotMessage, 
 | 
					  msg: WbotMessage, 
 | 
				
			||||||
  ticket: Ticket,
 | 
					  ticket: Ticket,
 | 
				
			||||||
  contact: Contact
 | 
					  contact: Contact,
 | 
				
			||||||
 | 
					  quotedMsg?: any,
 | 
				
			||||||
) => {
 | 
					) => {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  console.log('Entrou no verify message...')
 | 
					  console.log('Entrou no verify message...')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const quotedMsg = await verifyQuotedMessage(msg);
 | 
					  // const quotedMsg = await verifyQuotedMessage(msg);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // const quotedMsg = await verifyQuotedMessage(msg);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const messageData = {
 | 
					  const messageData = {
 | 
				
			||||||
    id: msg.id.id,
 | 
					    id: msg.id.id,
 | 
				
			||||||
| 
						 | 
					@ -172,7 +181,8 @@ const verifyMessage = async (
 | 
				
			||||||
    fromMe: msg.fromMe,
 | 
					    fromMe: msg.fromMe,
 | 
				
			||||||
    mediaType: msg.type,
 | 
					    mediaType: msg.type,
 | 
				
			||||||
    read: msg.fromMe,
 | 
					    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 (
 | 
					const handleMessage = async (
 | 
				
			||||||
  msg: WbotMessage,
 | 
					  msg: any,
 | 
				
			||||||
  wbot: Session
 | 
					  wbot: any
 | 
				
			||||||
): Promise<void> => {
 | 
					): Promise<void> => {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (!isValidMsg(msg)) {
 | 
					  if (!isValidMsg(msg)) {
 | 
				
			||||||
| 
						 | 
					@ -389,8 +399,8 @@ const handleMessage = async (
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  try {
 | 
					  try {
 | 
				
			||||||
    let msgContact: WbotContact;
 | 
					    let msgContact: any = wbot.msgContact
 | 
				
			||||||
    let groupContact: Contact | undefined;
 | 
					    // let groupContact: Contact | undefined;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (msg.fromMe) {
 | 
					    if (msg.fromMe) {
 | 
				
			||||||
      // messages sent automatically by wbot have a special character in front of it
 | 
					      // 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;
 | 
					      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('1 --------------> msgContat: ', JSON.parse(JSON.stringify(msgContact)))
 | 
				
			||||||
      console.log('     # msg.type: ', msg.type )
 | 
					      // console.log('     # msg.type: ', msg.type )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    } else {
 | 
					    } 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: 
 | 
					      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 (msg.fromMe) {
 | 
				
			||||||
 | 
					    //     msgGroupContact = await wbot.getContactById(msg.to);
 | 
				
			||||||
 | 
					    //   } else {
 | 
				
			||||||
 | 
					    //     msgGroupContact = await wbot.getContactById(msg.from);
 | 
				
			||||||
 | 
					    //   }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    //   groupContact = await verifyContact(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);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    const whatsapp = await ShowWhatsAppService(wbot.id!);
 | 
					    const whatsapp = await ShowWhatsAppService(wbot.id!);
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
 | 
					    // const whatsapp = await ShowWhatsAppService(46);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const unreadMessages = msg.fromMe ? 0 : chat.unreadCount;
 | 
					    const unreadMessages = msg.fromMe ? 0 : chat.unreadCount;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const contact = await verifyContact(msgContact);
 | 
					    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,
 | 
					      contact,
 | 
				
			||||||
      wbot.id!,
 | 
					      wbot.id!,
 | 
				
			||||||
      unreadMessages,
 | 
					      unreadMessages,
 | 
				
			||||||
      groupContact
 | 
					      // groupContact
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // 
 | 
					    // 
 | 
				
			||||||
| 
						 | 
					@ -473,9 +489,9 @@ const handleMessage = async (
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    if (msg.hasMedia) {
 | 
					    if (msg.hasMedia) {
 | 
				
			||||||
      await verifyMediaMessage(msg, ticket, contact);
 | 
					      await verifyMediaMessage(msg, ticket, contact, wbot.quotedMsg);
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      await verifyMessage(msg, ticket, contact);
 | 
					      await verifyMessage(msg, ticket, contact, wbot.quotedMsg);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    //setTimeout(()=>verifyQueue(wbot, msg, ticket, contact), 3000);   
 | 
					    //setTimeout(()=>verifyQueue(wbot, msg, ticket, contact), 3000);   
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -14,6 +14,8 @@ const ShowWhatsAppService = async (id: string | number): Promise<Whatsapp> => {
 | 
				
			||||||
    order: [["queues", "id", "ASC"]]
 | 
					    order: [["queues", "id", "ASC"]]
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  console.log('kkkkkkkkkkkkkkkkkkkk: ', whatsapp)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (!whatsapp) {
 | 
					  if (!whatsapp) {
 | 
				
			||||||
    throw new AppError("ERR_NO_WAPP_FOUND", 404);
 | 
					    throw new AppError("ERR_NO_WAPP_FOUND", 404);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue