projeto-hit/backend/src/controllers/WhatsAppController.ts

723 lines
20 KiB
TypeScript
Raw Normal View History

import { Request, Response } from "express";
import { getIO } from "../libs/socket";
import { removeWbot } from "../libs/wbot";
import { StartWhatsAppSession } from "../services/WbotServices/StartWhatsAppSession";
import { removeDir } from "../helpers/DeleteDirectory";
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";
import AppError from "../errors/AppError";
import getNumberFromName from "../helpers/GetNumberSequence";
2023-08-28 17:05:53 +00:00
import phoneNumberStart from "../helpers/PhoneNumberStatusCode";
2023-09-08 19:50:51 +00:00
import path, { join } from "path";
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 {
2023-09-08 19:50:51 +00:00
handleMessage,
2023-08-28 17:05:53 +00:00
handleMsgAck,
verifyContact,
verifyMessage
} from "../services/WbotServices/wbotMessageListener";
import Contact from "../models/Contact";
import CreateOrUpdateContactService from "../services/ContactServices/CreateOrUpdateContactService";
2023-09-08 19:50:51 +00:00
import GetDefaultWhatsApp from "../helpers/GetDefaultWhatsApp";
import ShowUserService from "../services/UserServices/ShowUserService";
import fs from "fs";
import receiveWhatsAppMediaOfficialAPI from "../helpers/ReceiveWhatsAppMediaOfficialAPI";
import whatsappOfficialNumberInfo from "../helpers/WhatsappOfficialNumberInfo";
import { getSettingValue } from "../helpers/WhaticketSettings";
import ListWhatsAppsNumber from "../services/WhatsappService/ListWhatsAppsNumber";
import SettingTicket from "../models/SettingTicket";
import { Op } from "sequelize";
import { del, get, getKeysByPattern, set, setCBPWhatsappOfficial } from "../helpers/RedisClient";
import axios from "axios";
interface WhatsappData {
name: string;
queueIds: number[];
url: string;
urlApi: string;
greetingMessage?: string;
farewellMessage?: string;
status?: string;
isDefault?: boolean;
isOfficial?: boolean;
phoneNumberId?: string;
number?: string;
wabaId?: string;
}
let count: number = 0;
export const index = async (req: Request, res: Response): Promise<Response> => {
let whatsapps = await ListWhatsAppsService();
if (getSettingValue("whatsaAppCloudApi")?.value == "enabled") {
// Atualizar isso quando tiver tempo
if (count > 12) count = 0;
if (count == 0) {
for (let i in whatsapps) {
const { id, wabaId, isOfficial } = whatsapps[i];
if (isOfficial && wabaId) {
try {
const info = await whatsappOfficialNumberInfo(wabaId);
if (info) {
const whatsapp = await Whatsapp.findByPk(id);
if (whatsapp) {
whatsapp.update({
classification: info.quality_rating
});
whatsapps[i].classification = info.quality_rating;
}
}
} catch (error) {
console.log(
"error on try update classification number from oficial whatsapp in WhatsappController.ts: ",
error
);
}
}
}
}
console.log("count: ", count);
count++;
}
2022-06-29 00:34:43 +00:00
return res.status(200).json(whatsapps);
};
2023-09-08 19:50:51 +00:00
export const whatsAppOfficialMatchQueue = async (
req: Request,
res: Response
): Promise<Response> => {
const { userId, queueId }: any = req.query;
let whatsapps = await GetDefaultWhatsApp({ userId, queueId });
if (whatsapps && Array.isArray(whatsapps)) {
const uniqueWhatsApps = whatsapps.filter(
(whatsapp, index, self) =>
index === self.findIndex(w => w.number === whatsapp.number)
);
whatsapps = uniqueWhatsApps;
}
2023-09-08 19:50:51 +00:00
return res.status(200).json(whatsapps);
};
export const whatsAppOfficialMatchQueueUser = async (
req: Request,
res: Response
): Promise<Response> => {
const { userId, queueId }: any = req.query;
let whatsApps: any = await ListWhatsAppsService();
let user: any = await ShowUserService(userId);
// console.log(JSON.stringify(user, null, 2));
let queuesConnected = whatsApps
.filter((w: any) => w.status === "CONNECTED")
.map((item: any) => {
const { queues } = item;
return {
queues: queues.map((q: any) => {
return { id: q.id };
})
};
})
.flatMap((item: any) => item.queues.map((queue: any) => queue.id));
queuesConnected = [...new Set(queuesConnected)].map(q => {
return { id: q };
});
const userQueues = user.queues.map((item: any) => {
const { id, name, color } = item;
return {
id,
name,
color,
disable: queuesConnected.find((queue: any) => queue.id === id)
? false
: true
};
});
return res.status(200).json(userQueues);
};
export const media = async (req: Request, res: Response) => {
const { filename } = req.params;
const filePath = join(__dirname, "..", "..", "..", "..", "public", filename);
console.log("filePath: ", filePath);
console.log(filename);
if (!fs.existsSync(filePath)) {
return res.status(404).json({ message: "File not folund!" });
}
// Set appropriate headers for the download.
res.setHeader("Content-Disposition", `attachment; filename=${filename}`);
res.sendFile(filePath);
};
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));
2023-09-08 19:50:51 +00:00
console.log("req.method: ", req.method);
if (req.method == "GET") {
/**
* UPDATE YOUR VERIFY TOKEN
*This will be the Verify Token value when you set up webhook
**/
const verify_token = process.env.VERIFY_TOKEN;
// Parse params from the webhook verification request
let mode = req.query["hub.mode"];
let token = req.query["hub.verify_token"];
let challenge = req.query["hub.challenge"];
// Check if a token and mode were sent
if (mode && token) {
// Check the mode and token sent are correct
if (mode === "subscribe" && token === verify_token) {
// Respond with 200 OK and challenge token from the request
console.log("WEBHOOK_VERIFIED");
return res.status(200).send(challenge);
} else {
// Responds with '403 Forbidden' if verify tokens do not match
return res.sendStatus(403);
}
}
return res.sendStatus(500);
}
if(
req?.body?.entry?.length > 0 &&
req?.body?.entry[0]?.changes?.length > 0 &&
req?.body?.entry[0]?.changes?.length > 0 &&
req?.body?.entry[0]?.changes[0]?.value?.statuses?.length>0 &&
req?.body?.entry[0]?.changes[0]?.value?.statuses[0]?.recipient_id &&
req?.body?.entry[0]?.changes[0]?.value?.statuses[0]?.conversation?.origin?.type){
const company_phone = req?.body?.entry[0]?.changes[0]?.value?.metadata?.display_phone_number
const client_phone = req?.body?.entry[0]?.changes[0]?.value?.statuses[0]?.recipient_id
const conversation_type = req?.body?.entry[0]?.changes[0]?.value?.statuses[0].conversation.origin.type
const billable = req?.body?.entry[0]?.changes[0]?.value?.statuses[0].pricing.billable
const pricing_model = req?.body?.entry[0]?.changes[0]?.value?.statuses[0].pricing.pricing_model
const conversation_type_category = req?.body?.entry[0]?.changes[0]?.value?.statuses[0].pricing.category
const msg_id = req?.body?.entry[0]?.changes[0]?.value?.statuses[0].id
const _contact_to_exist = await get({
key: "whatsapp:*",
value: `${company_phone}`
});
if(_contact_to_exist){
const lst_services_cbp = await getKeysByPattern(company_phone, client_phone, conversation_type_category)
if(lst_services_cbp && lst_services_cbp.length > 0){
for(const item of lst_services_cbp){
if(!item.split(':').includes(conversation_type_category)){
await setCBP(msg_id, company_phone, client_phone, conversation_type_category, billable, pricing_model)
}
}
}else{
await setCBP(msg_id, company_phone, client_phone, conversation_type_category, billable, pricing_model)
}
console.log('_contact_to_exist: ', _contact_to_exist)
}
}
2023-08-28 17:05:53 +00:00
// 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]
) {
2023-09-08 19:50:51 +00:00
const message = req.body.entry[0].changes[0].value.messages[0];
const contact_from = message.from; // extract the phone number from the webhook payload
2023-08-28 17:05:53 +00:00
const contact_to =
req.body.entry[0].changes[0].value.metadata.display_phone_number;
2023-09-08 19:50:51 +00:00
let type = message.type;
2023-08-28 17:05:53 +00:00
const contact_to_exist = await get({
key: "whatsapp:*",
value: `${contact_to}`
});
if (contact_to_exist == null) {
console.log(
"WHATSAPP OFFICIAL",
" | CONCTACT_FROM: ",
contact_from,
" | CONTACT_TO: ",
contact_to
);
console.log(
"NUMBER IGNORED. WHATSAPP NUMBER FROM ANOTHER OMNIHIT APPLICATION!"
);
return res.status(403).json({ error: "Unauthorized" });
}
2023-09-08 19:50:51 +00:00
let wbot = {};
2023-08-28 17:05:53 +00:00
let msg = {};
2023-09-08 19:50:51 +00:00
let contacts = req.body.entry[0].changes[0].value.contacts[0];
2023-08-28 17:05:53 +00:00
msg = {
...msg,
2023-09-08 19:50:51 +00:00
id: { id: message.id },
2023-08-28 17:05:53 +00:00
fromMe: false,
2023-09-08 19:50:51 +00:00
type: type,
2023-08-28 17:05:53 +00:00
read: false,
2023-09-08 19:50:51 +00:00
hasMedia: false
2023-08-28 17:05:53 +00:00
};
2023-09-08 19:50:51 +00:00
// NEW
const whatsapp = await ShowWhatsAppService(null, {
number: contact_to
});
if (type == "text") {
if (!message?.text?.body) {
return res.status(400).json({ error: "body not found" });
}
2023-09-08 19:50:51 +00:00
type = "chat";
msg = {
...msg,
body: message.text.body, // extract the message text from the webhook payload,
type
2023-09-08 19:50:51 +00:00
};
} else {
if (!message[message?.type]?.id) {
return res.status(400).json({ error: "id not found" });
}
2023-09-08 19:50:51 +00:00
const mediaId = message[message.type].id;
const mimetype = message[message.type].mime_type;
let filename = await receiveWhatsAppMediaOfficialAPI(
mediaId,
whatsapp.phoneNumberId,
whatsapp.whatsappOfficialToken
2023-09-08 19:50:51 +00:00
);
if (!filename) throw new AppError("There was an error");
msg = {
...msg,
hasMedia: true
};
wbot = { ...wbot, media: { filename, mimetype } };
}
msg = { ...msg, phoneNumberId: whatsapp.phoneNumberId };
2023-09-08 19:50:51 +00:00
console.log("from: ", contact_from);
console.log("to: ", contact_to);
console.log("msg type: ", type);
wbot = {
...wbot,
id: whatsapp.id,
msgContact: {
number: contact_from,
name: contacts?.profile?.name
},
chat: { isGroup: false, unreadCount: 1 },
quotedMsg: message && message?.context ? message.context.id : undefined
};
handleMessage(msg, wbot, true);
2023-08-28 17:05:53 +00:00
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);
};
export const store = async (req: Request, res: Response): Promise<Response> => {
let {
name,
status,
isDefault,
greetingMessage,
farewellMessage,
queueIds,
url,
urlApi,
phoneNumberId,
wabaId,
isOfficial,
number
}: WhatsappData = req.body;
if (req.user.profile !== "master") {
throw new AppError("ERR_NO_PERMISSION", 403);
}
2022-06-29 00:34:43 +00:00
const invalid = checkWhatsAppData({
urlApi,
isOfficial,
phoneNumberId,
wabaId,
number
});
if (invalid) {
return res.status(400).json(invalid);
}
if (isOfficial) {
urlApi = "";
url = "";
} else if (!isOfficial) {
phoneNumberId = "";
wabaId = "";
number = "";
}
let invalidPhoneName = validatePhoneName(name);
if (invalidPhoneName) {
return res.status(200).json({ message: invalidPhoneName });
}
2023-08-28 17:05:53 +00:00
const { whatsapp, oldDefaultWhatsapp } = await CreateWhatsAppService({
name,
url,
urlApi,
status,
isDefault,
greetingMessage,
farewellMessage,
queueIds,
phoneNumberId,
wabaId,
isOfficial,
number
});
2023-08-28 17:05:53 +00:00
console.log("whatsapp.id: ", whatsapp.id);
if (!isOfficial) {
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}`
});
} else {
await set(
`whatsapp:${whatsapp.id}`,
JSON.stringify({
number: whatsapp?.number,
id: whatsapp?.id,
greetingMessage: whatsapp?.greetingMessage,
phoneNumberId: whatsapp?.phoneNumberId
})
);
}
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
return res.status(200).json(whatsapp);
};
export const update = async (
req: Request,
res: Response
): Promise<Response> => {
const { whatsappId } = req.params;
const whatsappData = req.body;
let invalidPhoneName = validatePhoneName(whatsappData.name);
if (invalidPhoneName) {
return res.status(200).json({ message: invalidPhoneName });
}
const { urlApi, isOfficial, phoneNumberId, number, wabaId } = whatsappData;
const invalid = checkWhatsAppData({
urlApi,
isOfficial,
phoneNumberId,
wabaId,
number
});
if (invalid) {
return res.status(400).json(invalid);
}
if (isOfficial) {
whatsappData.urlApi = "";
whatsappData.url = "";
} else if (!isOfficial) {
whatsappData.phoneNumberId = "";
whatsappData.wabaId = "";
whatsappData.number = "";
}
2023-08-28 17:05:53 +00:00
const { whatsapp, oldDefaultWhatsapp } = await UpdateWhatsAppService({
whatsappData,
whatsappId
});
if (!whatsappData?.isOfficial) {
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}`
});
} else {
await set(
`whatsapp:${whatsapp.id}`,
JSON.stringify({
number: whatsapp?.number,
id: whatsapp?.id,
greetingMessage: whatsapp?.greetingMessage,
phoneNumberId: whatsapp?.phoneNumberId
})
);
}
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> => {
if (req.user.profile !== "master") {
throw new AppError("ERR_NO_PERMISSION", 403);
2022-06-29 00:34:43 +00:00
}
2022-06-29 00:34:43 +00:00
const { whatsappId } = req.params;
2023-08-28 17:05:53 +00:00
const whatsapp: any = await Whatsapp.findByPk(whatsappId, { raw: true });
if (!whatsapp?.isOfficial) {
postData(`${whatsapp.urlApi}/api/session/del`, {
app_name: process.env.APP_NAME,
whatsappId: whatsappId
});
}
await del(`whatsapp:${whatsappId}`);
let whats = await ListWhatsAppsNumber(whatsappId);
// Remove tickets business hours config
if (whats?.whatsapps?.length == 1) {
const configIds = await SettingTicket.findAll({
where: { number: whats?.whatsapps[0]?.number },
raw: true,
attributes: ["id"]
});
const whatsappTicketConfig = await SettingTicket.findOne({
where: { number: whats.whatsapps[0].number }
});
if (whatsappTicketConfig) {
try {
await SettingTicket.destroy({
where: {
id: {
[Op.in]: configIds.map(config => config.id)
}
}
});
} catch (error) {
console.log(
"Error on delete SettingTicket by number: ",
whats?.whatsapps[0]?.number
);
}
}
}
await DeleteWhatsAppService(whatsappId);
2023-08-28 17:05:53 +00:00
removeDir(
path.join(process.cwd(), ".wwebjs_auth", `session-bd_${whatsappId}`)
);
2023-08-28 17:05:53 +00:00
removeDir(
path.join(
process.cwd(),
".wwebjs_auth",
"sessions",
`session-bd_${whatsappId}`
)
);
removeWbot(+whatsappId);
const io = getIO();
io.emit("whatsapp", {
action: "delete",
whatsappId: +whatsappId
2022-06-29 00:34:43 +00:00
});
return res.status(200).json({ message: "Whatsapp deleted." });
};
interface WhatsappDataValidate {
urlApi?: string;
isOfficial?: boolean;
phoneNumberId?: string;
wabaId?: string;
number?: string;
}
const checkWhatsAppData = ({
urlApi,
isOfficial,
phoneNumberId,
wabaId,
number
}: WhatsappDataValidate) => {
if (isOfficial && (!phoneNumberId || phoneNumberId.trim() == "")) {
return { message: "Phone number Id is required!" };
} else if (isOfficial && (!wabaId || wabaId.trim() == "")) {
return { message: "WABA ID is required!" };
} else if (isOfficial && (!number || number.trim() == "")) {
return { message: "Phone number is required!" };
} else if (!isOfficial && (!urlApi || urlApi.trim() == "")) {
return { message: "urlApi is required!" };
}
};
async function setCBP(msg_id: any, company_phone: any, client_phone: any, conversation_type_category: any, billable:string, pricing_model:string) {
const message = await Message.findByPk(msg_id)
if (message) {
await setCBPWhatsappOfficial(company_phone, client_phone, conversation_type_category, msg_id, `${message.ticketId}`)
await sendToAPIUsage(msg_id,
company_phone,
client_phone,
conversation_type_category,
`${message.ticketId}`,
billable,
pricing_model
)
}
}
async function sendToAPIUsage(msg_id: any, company_phone: any, client_phone: any, conversation_type_category: any, ticketId: any, billable:string, pricing_model:string) {
const data = JSON.stringify({
"companyId": company_phone,
"provider": "meta",
"product": "whatsapp",
"type": conversation_type_category,
"msgId": msg_id,
"ticketId": `${ticketId}`,
"billable": billable,
"pricing_model": pricing_model
});
const config = {
method: 'post',
url: 'http://172.31.187.24:6008/api/v1/billing/usage-whatsapp',
headers: {
'Authorization': 'Bearer 2ivck10D3o9qAZi0pkKudVDl9bdEVXY2s8gdxZ0jYgL1DZWTgDz6wDiIjlWgYmJtVOoqf0b42ZTLBRrfo8WoAaScRsujz3jQUNXdchSg0o43YilZGmVhheGJNAeIQRknHEll4nRJ7avcFgmDGoYbEey7TSC8EHS4Z3gzeufYYSfnKNDBwwzBURIQrTOxYFe3tBHsGOzwnuD2lU5tnEx7tr2XRO4zRNYeNY4lMBOFM0mRuyAe4kuqTrKXmJ8As200',
'Content-Type': 'application/json'
},
data: data
};
try {
const response = await axios(config);
console.log('Response from whatsapp api usage: ',JSON.stringify(response.data));
} catch (error) {
console.log('Error on try register the whatsapp usage: ', error);
}
}