Desenvolvimento parcial do recebimento de midia

pull/21/head
adriano 2023-01-25 17:44:02 -03:00
parent 5c44656328
commit 01478d91d1
5 changed files with 107 additions and 49 deletions

View File

@ -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,6 +48,49 @@ 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)
@ -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

View File

@ -17,5 +17,5 @@ if (global.gc) {
}
initIO(server);
StartAllWhatsAppsSessions();
// StartAllWhatsAppsSessions();
gracefulShutdown(server);

View File

@ -25,15 +25,6 @@ let counter = 0
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()
console.log('cacheSize: ', cacheLength)

View File

@ -69,8 +69,9 @@ interface Session extends Client {
const writeFileAsync = promisify(writeFile);
const verifyContact = async (msgContact: WbotContact): Promise<Contact> => {
const profilePicUrl = await msgContact.getProfilePicUrl();
const verifyContact = async (msgContact: any): Promise<Contact> => {
// 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<Contact> => {
};
const verifyQuotedMessage = async (
msg: WbotMessage
msg: any,
_quotedMsg?: any
): Promise<Message | null> => {
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<Message> => {
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 });
@ -156,13 +162,16 @@ const verifyMediaMessage = async (
const verifyMessage = async (
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<void> => {
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 (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(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
);
//
@ -473,9 +489,9 @@ 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);

View File

@ -14,6 +14,8 @@ const ShowWhatsAppService = async (id: string | number): Promise<Whatsapp> => {
order: [["queues", "id", "ASC"]]
});
console.log('kkkkkkkkkkkkkkkkkkkk: ', whatsapp)
if (!whatsapp) {
throw new AppError("ERR_NO_WAPP_FOUND", 404);
}