-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(whtsapp): implementado rotina de envio de mensagem com rabbitmq.
- Loading branch information
Showing
9 changed files
with
224 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
declare namespace NodeJS { | ||
interface Global { | ||
_loopDb: any; | ||
rabbitWhatsapp: any; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* eslint-disable no-restricted-syntax */ | ||
/* eslint-disable no-await-in-loop */ | ||
import { join } from "path"; | ||
import { MessageMedia } from "whatsapp-web.js"; | ||
import Message from "../../models/Message"; | ||
import { logger } from "../../utils/logger"; | ||
// import { sleepRandomTime } from "../../utils/sleepRandomTime"; | ||
import { getWbot } from "../../libs/wbot"; | ||
// import SetTicketMessagesAsRead from "../../helpers/SetTicketMessagesAsRead"; | ||
|
||
const SendMessage = async (message: Message): Promise<void> => { | ||
const wbot = getWbot(message.ticket.whatsappId); | ||
let sendedMessage; | ||
|
||
// logger.info( | ||
// `SystemWbot SendMessages | Count: ${messages.length} | Tenant: ${tenantId} ` | ||
// ); | ||
|
||
let quotedMsgSerializedId: string | undefined; | ||
const { ticket } = message; | ||
const contactNumber = message.contact.number; | ||
const typeGroup = ticket?.isGroup ? "g" : "c"; | ||
const chatId = `${contactNumber}@${typeGroup}.us`; | ||
|
||
if (message.quotedMsg) { | ||
quotedMsgSerializedId = `${message.quotedMsg.fromMe}_${contactNumber}@${typeGroup}.us_${message.quotedMsg.messageId}`; | ||
} | ||
|
||
if (message.mediaType !== "chat" && message.mediaName) { | ||
const customPath = join(__dirname, "..", "..", "..", "public"); | ||
const mediaPath = join(customPath, message.mediaName); | ||
const newMedia = MessageMedia.fromFilePath(mediaPath); | ||
sendedMessage = await wbot.sendMessage(chatId, newMedia, { | ||
quotedMessageId: quotedMsgSerializedId, | ||
linkPreview: false, // fix: send a message takes 2 seconds when there's a link on message body | ||
sendAudioAsVoice: true | ||
}); | ||
logger.info("rabbit::sendMessage media"); | ||
} else { | ||
sendedMessage = await wbot.sendMessage(chatId, message.body, { | ||
quotedMessageId: quotedMsgSerializedId, | ||
linkPreview: false // fix: send a message takes 2 seconds when there's a link on message body | ||
}); | ||
logger.info("rabbit::sendMessage text"); | ||
} | ||
|
||
// enviar old_id para substituir no front a mensagem corretamente | ||
const messageToUpdate = { | ||
...message, | ||
...sendedMessage, | ||
id: message.id, | ||
messageId: sendedMessage.id.id, | ||
status: "sended" | ||
}; | ||
|
||
await Message.update({ ...messageToUpdate }, { where: { id: message.id } }); | ||
|
||
logger.info("rabbit::Message Update"); | ||
// await SetTicketMessagesAsRead(ticket); | ||
|
||
logger.info("rabbit::sendMessage", sendedMessage.id.id); | ||
// throw new Error("SIMULANDO ERRO"); | ||
}; | ||
|
||
const WhatsappConsumer = tenantId => { | ||
const queue = `whatsapp::${tenantId}`; | ||
global.rabbitWhatsapp.consumeWhatsapp(queue, async message => { | ||
const content = JSON.parse(message.content.toString()); | ||
await SendMessage(content); | ||
}); | ||
}; | ||
|
||
export default WhatsappConsumer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.