migrated receive message logic to typescript

This commit is contained in:
canove
2020-09-22 09:15:18 -03:00
parent 391cd5495c
commit 48dbf7e859
14 changed files with 423 additions and 403 deletions

View File

@@ -0,0 +1,23 @@
import AppError from "../../errors/AppError";
import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp";
import { getWbot } from "../../libs/wbot";
const CheckIsValidContact = async (number: string): Promise<void> => {
const defaultWhatsapp = await GetDefaultWhatsApp();
const wbot = getWbot(defaultWhatsapp.id);
try {
const isValidNumber = await wbot.isRegisteredUser(`${number}@c.us`);
if (!isValidNumber) {
throw new AppError("The suplied number is not a valid Whatsapp number");
}
} catch (err) {
console.log(err);
throw new AppError(
"Could not valid WhatsApp contact. Check connections page"
);
}
};
export default CheckIsValidContact;

View File

@@ -0,0 +1,14 @@
import GetDefaultWhatsApp from "../../helpers/GetDefaultWhatsApp";
import { getWbot } from "../../libs/wbot";
const GetProfilePicUrl = async (number: string): Promise<string> => {
const defaultWhatsapp = await GetDefaultWhatsApp();
const wbot = getWbot(defaultWhatsapp.id);
const profilePicUrl = await wbot.getProfilePicUrl(`${number}@c.us`);
return profilePicUrl;
};
export default GetProfilePicUrl;

View File

@@ -0,0 +1,29 @@
import { MessageMedia, Message as WbotMessage } from "whatsapp-web.js";
import GetTicketWbot from "../../helpers/GetTicketWbot";
import Ticket from "../../models/Ticket";
interface Request {
media: Express.Multer.File;
ticket: Ticket;
}
const SendWhatsAppMedia = async ({
media,
ticket
}: Request): Promise<WbotMessage> => {
const wbot = await GetTicketWbot(ticket);
const newMedia = MessageMedia.fromFilePath(media.path);
const mediaUrl = media.filename;
const sentMessage = await wbot.sendMessage(
`${ticket.contact.number}@c.us`,
newMedia
);
await ticket.update({ lastMessage: mediaUrl });
return sentMessage;
};
export default SendWhatsAppMedia;

View File

@@ -0,0 +1,25 @@
import { Message as WbotMessage } from "whatsapp-web.js";
import GetTicketWbot from "../../helpers/GetTicketWbot";
import Ticket from "../../models/Ticket";
interface Request {
body: string;
ticket: Ticket;
}
const SendWhatsAppMessage = async ({
body,
ticket
}: Request): Promise<WbotMessage> => {
const wbot = await GetTicketWbot(ticket);
const sentMessage = await wbot.sendMessage(
`${ticket.contact.number}@c.us`,
body
);
await ticket.update({ lastMessage: body });
return sentMessage;
};
export default SendWhatsAppMessage;

View File

@@ -124,7 +124,6 @@ const handleMessage = async (
ticket: Ticket,
contact: Contact
) => {
const io = getIO();
let newMessage: Message;
if (msg.hasMedia) {
@@ -138,6 +137,7 @@ const handleMessage = async (
await ticket.update({ lastMessage: msg.body });
}
const io = getIO();
io.to(ticket.id.toString()).to("notification").emit("appMessage", {
action: "create",
message: newMessage,
@@ -157,7 +157,7 @@ const wbotMessageListener = (whatsapp: Whatsapp): void => {
}
wbot.on("message_create", async msg => {
// console.log(msg);
console.log(msg.type);
if (
msg.from === "status@broadcast" ||