mirror of
https://github.com/cheveguerra/whaticket-community.git
synced 2026-04-19 12:19:16 +00:00
migrated receive message logic to typescript
This commit is contained in:
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