mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-21 05:09:18 +00:00
migrated receive message logic to typescript
This commit is contained in:
@@ -10,6 +10,7 @@ interface Request {
|
||||
name: string;
|
||||
number: string;
|
||||
email?: string;
|
||||
profilePicUrl?: string;
|
||||
extraInfo?: ExtraInfo[];
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,18 @@
|
||||
import AppError from "../../errors/AppError";
|
||||
import Message from "../../models/Message";
|
||||
import ShowTicketService from "../TicketServices/ShowTicketService";
|
||||
|
||||
interface MessageData {
|
||||
id: string;
|
||||
body: string;
|
||||
fromMe: boolean;
|
||||
read: boolean;
|
||||
mediaType: string;
|
||||
mediaUrl?: string;
|
||||
}
|
||||
interface Request {
|
||||
ticketId: string;
|
||||
messageData: Message;
|
||||
ticketId: string | number;
|
||||
messageData: MessageData;
|
||||
}
|
||||
|
||||
const CreateMessageService = async ({
|
||||
@@ -13,10 +22,10 @@ const CreateMessageService = async ({
|
||||
const ticket = await ShowTicketService(ticketId);
|
||||
|
||||
if (!ticket) {
|
||||
throw new Error("No ticket found with this ID");
|
||||
throw new AppError("No ticket found with this ID");
|
||||
}
|
||||
|
||||
const message = Message.create({ ...messageData, ticketId });
|
||||
const message: Message = await ticket.$create("message", messageData);
|
||||
|
||||
return message;
|
||||
};
|
||||
|
||||
@@ -21,7 +21,7 @@ const ShowTicketService = async (id: string | number): Promise<Ticket> => {
|
||||
});
|
||||
|
||||
if (!ticket) {
|
||||
throw new AppError("No ticket found with this conditions.", 404);
|
||||
throw new AppError("No ticket found with this ID");
|
||||
}
|
||||
|
||||
return ticket;
|
||||
|
||||
23
backend/src/services/WbotServices/CheckIsValidContact.ts
Normal file
23
backend/src/services/WbotServices/CheckIsValidContact.ts
Normal 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;
|
||||
14
backend/src/services/WbotServices/GetProfilePicUrl.ts
Normal file
14
backend/src/services/WbotServices/GetProfilePicUrl.ts
Normal 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;
|
||||
29
backend/src/services/WbotServices/SendWhatsAppMedia.ts
Normal file
29
backend/src/services/WbotServices/SendWhatsAppMedia.ts
Normal 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;
|
||||
25
backend/src/services/WbotServices/SendWhatsAppMessage.ts
Normal file
25
backend/src/services/WbotServices/SendWhatsAppMessage.ts
Normal 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;
|
||||
@@ -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" ||
|
||||
|
||||
Reference in New Issue
Block a user