2022-01-06 01:26:15 +00:00
|
|
|
import { Request, Response } from "express";
|
|
|
|
import { getIO } from "../libs/socket";
|
|
|
|
import { removeWbot } from "../libs/wbot";
|
|
|
|
import { StartWhatsAppSession } from "../services/WbotServices/StartWhatsAppSession";
|
|
|
|
|
2022-02-14 01:39:33 +00:00
|
|
|
import { removeDir } from "../helpers/DeleteDirectory";
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
import CreateWhatsAppService from "../services/WhatsappService/CreateWhatsAppService";
|
|
|
|
import DeleteWhatsAppService from "../services/WhatsappService/DeleteWhatsAppService";
|
|
|
|
import ListWhatsAppsService from "../services/WhatsappService/ListWhatsAppsService";
|
|
|
|
import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService";
|
|
|
|
import UpdateWhatsAppService from "../services/WhatsappService/UpdateWhatsAppService";
|
|
|
|
|
2022-01-13 17:11:50 +00:00
|
|
|
import AppError from "../errors/AppError";
|
2023-02-13 12:43:00 +00:00
|
|
|
|
2023-04-10 12:08:09 +00:00
|
|
|
import getNumberFromName from "../helpers/GetNumberSequence";
|
2023-08-28 17:05:53 +00:00
|
|
|
import phoneNumberStart from "../helpers/PhoneNumberStatusCode";
|
2023-04-10 12:08:09 +00:00
|
|
|
|
2023-08-28 17:05:53 +00:00
|
|
|
import path from "path";
|
2023-04-10 12:08:09 +00:00
|
|
|
import validatePhoneName from "../helpers/ValidatePhoneName";
|
|
|
|
import postData from "../helpers/AxiosPost";
|
|
|
|
import Whatsapp from "../models/Whatsapp";
|
2023-08-28 17:05:53 +00:00
|
|
|
import Message from "../models/Message";
|
|
|
|
import FindOrCreateTicketService from "../services/TicketServices/FindOrCreateTicketService";
|
|
|
|
import {
|
|
|
|
handleMsgAck,
|
|
|
|
verifyContact,
|
|
|
|
verifyMessage
|
|
|
|
} from "../services/WbotServices/wbotMessageListener";
|
|
|
|
import Contact from "../models/Contact";
|
|
|
|
import CreateOrUpdateContactService from "../services/ContactServices/CreateOrUpdateContactService";
|
2022-02-14 01:39:33 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
interface WhatsappData {
|
|
|
|
name: string;
|
|
|
|
queueIds: number[];
|
2023-02-13 12:43:00 +00:00
|
|
|
url: string;
|
|
|
|
urlApi: string;
|
2022-01-06 01:26:15 +00:00
|
|
|
greetingMessage?: string;
|
|
|
|
farewellMessage?: string;
|
|
|
|
status?: string;
|
|
|
|
isDefault?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const index = async (req: Request, res: Response): Promise<Response> => {
|
2022-06-29 00:34:43 +00:00
|
|
|
const whatsapps = await ListWhatsAppsService();
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
return res.status(200).json(whatsapps);
|
|
|
|
};
|
|
|
|
|
2023-08-28 17:05:53 +00:00
|
|
|
export const weebhook = async (
|
|
|
|
req: Request,
|
|
|
|
res: Response
|
|
|
|
): Promise<Response> => {
|
|
|
|
// console.log(JSON.stringify(req.body, null, 2));
|
|
|
|
|
|
|
|
// MESSAGE
|
|
|
|
if (req.body.object) {
|
|
|
|
if (
|
|
|
|
req.body.entry &&
|
|
|
|
req.body.entry[0].changes &&
|
|
|
|
req.body.entry[0].changes[0] &&
|
|
|
|
req.body.entry[0].changes[0].value.messages &&
|
|
|
|
req.body.entry[0].changes[0].value.messages[0]
|
|
|
|
) {
|
|
|
|
const contact_from = req.body.entry[0].changes[0].value.messages[0].from; // extract the phone number from the webhook payload
|
|
|
|
const msg_body = req.body.entry[0].changes[0].value.messages[0].text.body; // extract the message text from the webhook payload
|
|
|
|
const type = req.body.entry[0].changes[0].value.messages[0].type;
|
|
|
|
const contact_to =
|
|
|
|
req.body.entry[0].changes[0].value.metadata.display_phone_number;
|
|
|
|
|
|
|
|
console.log("from: ", contact_from);
|
|
|
|
console.log("to: ", contact_to);
|
|
|
|
console.log("msg_body: ", msg_body);
|
|
|
|
console.log("msg type: ", type);
|
|
|
|
|
|
|
|
const contact: any = await verifyContact(null, { number: contact_from });
|
|
|
|
|
|
|
|
const whatsapp: any = await Whatsapp.findOne({
|
|
|
|
where: { number: contact_to }
|
|
|
|
});
|
|
|
|
|
|
|
|
const ticket = await FindOrCreateTicketService(contact, whatsapp.id, 1);
|
|
|
|
|
|
|
|
let msg = {};
|
|
|
|
|
|
|
|
msg = {
|
|
|
|
...msg,
|
|
|
|
id: { id: req.body.entry[0].changes[0].value.messages[0].id },
|
|
|
|
fromMe: false,
|
|
|
|
type: type === "text" ? "chat" : type,
|
|
|
|
read: false,
|
|
|
|
body: req.body.entry[0].changes[0].value.messages[0].text.body
|
|
|
|
};
|
|
|
|
|
|
|
|
await verifyMessage(msg, ticket, contact, undefined);
|
|
|
|
|
|
|
|
return res.sendStatus(200);
|
|
|
|
}
|
|
|
|
// STATUS MESSAGE SENT
|
|
|
|
else if (
|
|
|
|
req.body.entry &&
|
|
|
|
req.body.entry[0].changes &&
|
|
|
|
req.body.entry[0].changes[0] &&
|
|
|
|
req.body.entry[0].changes[0].value.statuses &&
|
|
|
|
req.body.entry[0].changes[0].value.statuses[0]
|
|
|
|
) {
|
|
|
|
const id = req.body.entry[0].changes[0].value.statuses[0].id;
|
|
|
|
const ack = req.body.entry[0].changes[0].value.statuses[0].status;
|
|
|
|
handleMsgAck(id, ack, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.sendStatus(200);
|
|
|
|
};
|
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
export const store = async (req: Request, res: Response): Promise<Response> => {
|
|
|
|
const {
|
|
|
|
name,
|
|
|
|
status,
|
|
|
|
isDefault,
|
|
|
|
greetingMessage,
|
|
|
|
farewellMessage,
|
2023-02-13 12:43:00 +00:00
|
|
|
queueIds,
|
|
|
|
url,
|
|
|
|
urlApi
|
2022-01-06 01:26:15 +00:00
|
|
|
}: WhatsappData = req.body;
|
|
|
|
|
2022-01-13 17:11:50 +00:00
|
|
|
if (req.user.profile !== "master") {
|
|
|
|
throw new AppError("ERR_NO_PERMISSION", 403);
|
|
|
|
}
|
2022-06-29 00:34:43 +00:00
|
|
|
|
2023-08-28 17:05:53 +00:00
|
|
|
let validate = validatePhoneName(name);
|
2023-04-10 12:08:09 +00:00
|
|
|
|
|
|
|
if (validate) {
|
|
|
|
return res.status(200).json({ message: validate });
|
|
|
|
}
|
2023-08-28 17:05:53 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const { whatsapp, oldDefaultWhatsapp } = await CreateWhatsAppService({
|
|
|
|
name,
|
2023-02-13 12:43:00 +00:00
|
|
|
url,
|
|
|
|
urlApi,
|
2022-01-06 01:26:15 +00:00
|
|
|
status,
|
|
|
|
isDefault,
|
|
|
|
greetingMessage,
|
|
|
|
farewellMessage,
|
|
|
|
queueIds
|
|
|
|
});
|
|
|
|
|
2023-08-28 17:05:53 +00:00
|
|
|
console.log("whatsapp.id: ", whatsapp.id);
|
2023-04-10 12:08:09 +00:00
|
|
|
|
2023-08-28 17:05:53 +00:00
|
|
|
postData(`${whatsapp.urlApi}/api/session`, {
|
|
|
|
app_name: process.env.APP_NAME,
|
|
|
|
whatsappId: whatsapp.id,
|
|
|
|
number: getNumberFromName(name),
|
|
|
|
client_url: `${process.env.BACKEND_URL_RAW}:${process.env.PORT}`
|
|
|
|
});
|
2023-04-10 12:08:09 +00:00
|
|
|
|
|
|
|
// StartWhatsAppSession(whatsapp);
|
2022-01-06 01:26:15 +00:00
|
|
|
|
|
|
|
const io = getIO();
|
|
|
|
io.emit("whatsapp", {
|
|
|
|
action: "update",
|
|
|
|
whatsapp
|
|
|
|
});
|
|
|
|
|
|
|
|
if (oldDefaultWhatsapp) {
|
|
|
|
io.emit("whatsapp", {
|
|
|
|
action: "update",
|
|
|
|
whatsapp: oldDefaultWhatsapp
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.status(200).json(whatsapp);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const show = async (req: Request, res: Response): Promise<Response> => {
|
|
|
|
const { whatsappId } = req.params;
|
|
|
|
|
|
|
|
const whatsapp = await ShowWhatsAppService(whatsappId);
|
2022-06-29 00:34:43 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
return res.status(200).json(whatsapp);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const update = async (
|
|
|
|
req: Request,
|
|
|
|
res: Response
|
|
|
|
): Promise<Response> => {
|
|
|
|
const { whatsappId } = req.params;
|
|
|
|
const whatsappData = req.body;
|
|
|
|
|
2023-08-28 17:05:53 +00:00
|
|
|
let validate = validatePhoneName(whatsappData.name);
|
2023-04-10 12:08:09 +00:00
|
|
|
|
2023-08-28 17:05:53 +00:00
|
|
|
console.log("validate", validate);
|
2023-04-10 12:08:09 +00:00
|
|
|
|
|
|
|
if (validate) {
|
|
|
|
return res.status(200).json({ message: validate });
|
|
|
|
}
|
2023-08-28 17:05:53 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const { whatsapp, oldDefaultWhatsapp } = await UpdateWhatsAppService({
|
|
|
|
whatsappData,
|
|
|
|
whatsappId
|
|
|
|
});
|
|
|
|
|
2023-08-28 17:05:53 +00:00
|
|
|
postData(`${whatsapp.urlApi}/api/session`, {
|
|
|
|
app_name: process.env.APP_NAME,
|
|
|
|
whatsappId: whatsapp.id,
|
|
|
|
number: getNumberFromName(whatsapp.name),
|
|
|
|
client_url: `${process.env.BACKEND_URL_RAW}:${process.env.PORT}`
|
|
|
|
});
|
2023-04-10 12:08:09 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const io = getIO();
|
|
|
|
io.emit("whatsapp", {
|
|
|
|
action: "update",
|
|
|
|
whatsapp
|
|
|
|
});
|
|
|
|
|
|
|
|
if (oldDefaultWhatsapp) {
|
|
|
|
io.emit("whatsapp", {
|
|
|
|
action: "update",
|
|
|
|
whatsapp: oldDefaultWhatsapp
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.status(200).json(whatsapp);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const remove = async (
|
|
|
|
req: Request,
|
|
|
|
res: Response
|
|
|
|
): Promise<Response> => {
|
2022-01-13 17:11:50 +00:00
|
|
|
if (req.user.profile !== "master") {
|
|
|
|
throw new AppError("ERR_NO_PERMISSION", 403);
|
2022-06-29 00:34:43 +00:00
|
|
|
}
|
2022-01-13 17:11:50 +00:00
|
|
|
|
2022-06-29 00:34:43 +00:00
|
|
|
const { whatsappId } = req.params;
|
2022-01-06 01:26:15 +00:00
|
|
|
|
2023-08-28 17:05:53 +00:00
|
|
|
const whatsapp: any = await Whatsapp.findByPk(whatsappId, { raw: true });
|
2023-04-10 12:08:09 +00:00
|
|
|
|
2023-08-28 17:05:53 +00:00
|
|
|
postData(`${whatsapp.urlApi}/api/session/del`, {
|
|
|
|
app_name: process.env.APP_NAME,
|
|
|
|
whatsappId: whatsappId
|
|
|
|
});
|
2023-04-10 12:08:09 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
await DeleteWhatsAppService(whatsappId);
|
2022-02-14 10:57:41 +00:00
|
|
|
|
2023-08-28 17:05:53 +00:00
|
|
|
removeDir(
|
|
|
|
path.join(process.cwd(), ".wwebjs_auth", `session-bd_${whatsappId}`)
|
|
|
|
);
|
2022-11-16 13:23:14 +00:00
|
|
|
|
2023-08-28 17:05:53 +00:00
|
|
|
removeDir(
|
|
|
|
path.join(
|
|
|
|
process.cwd(),
|
|
|
|
".wwebjs_auth",
|
|
|
|
"sessions",
|
|
|
|
`session-bd_${whatsappId}`
|
|
|
|
)
|
|
|
|
);
|
2022-11-16 13:23:14 +00:00
|
|
|
|
2023-02-13 12:43:00 +00:00
|
|
|
removeWbot(+whatsappId);
|
2022-02-14 01:39:33 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
const io = getIO();
|
|
|
|
io.emit("whatsapp", {
|
|
|
|
action: "delete",
|
|
|
|
whatsappId: +whatsappId
|
2022-06-29 00:34:43 +00:00
|
|
|
});
|
2022-02-14 01:39:33 +00:00
|
|
|
|
2022-01-06 01:26:15 +00:00
|
|
|
return res.status(200).json({ message: "Whatsapp deleted." });
|
|
|
|
};
|
2023-08-28 17:05:53 +00:00
|
|
|
|
|
|
|
|