diff --git a/backend/src/controllers/MessageController.ts b/backend/src/controllers/MessageController.ts index 9555e14..37259da 100644 --- a/backend/src/controllers/MessageController.ts +++ b/backend/src/controllers/MessageController.ts @@ -1,5 +1,4 @@ import { Request, Response } from "express"; -import whatsappOfficialAPI from "../helpers/WhatsappOfficialAPI"; import SetTicketMessagesAsRead from "../helpers/SetTicketMessagesAsRead"; import { getIO } from "../libs/socket"; @@ -22,6 +21,7 @@ import Whatsapp from "../models/Whatsapp"; import checkLastClientMsg24hs from "../helpers/CheckLastClientMsg24hs"; import AppError from "../errors/AppError"; import { get } from "../helpers/RedisClient"; +import createApiClientWhatsOfficial from "../helpers/WhatsappOfficialAPI"; type IndexQuery = { pageNumber: string; @@ -36,7 +36,7 @@ type MessageData = { params: any; }; -export const index = async (req: Request, res: Response): Promise => { +export const index = async (req: Request, res: Response): Promise => { const { ticketId } = req.params; const { pageNumber } = req.query as IndexQuery; @@ -123,7 +123,12 @@ export const store = async (req: Request, res: Response): Promise => { } } else { try { - const { wabaId }: any = await Whatsapp.findByPk(whatsappId); + const { wabaId, whatsappOfficialToken }: any = + await Whatsapp.findByPk(whatsappId); + + const whatsappOfficialAPI = createApiClientWhatsOfficial( + whatsappOfficialToken + ); const { data } = await whatsappOfficialAPI.get( `/${process.env.VERSION}/${wabaId}/message_templates?language=pt_BR` diff --git a/backend/src/controllers/WhatsAppController.ts b/backend/src/controllers/WhatsAppController.ts index 3a0dda6..0158748 100644 --- a/backend/src/controllers/WhatsAppController.ts +++ b/backend/src/controllers/WhatsAppController.ts @@ -35,8 +35,7 @@ import ShowUserService from "../services/UserServices/ShowUserService"; import fs from "fs"; import receiveWhatsAppMediaOfficialAPI from "../helpers/ReceiveWhatsAppMediaOfficialAPI"; - -import whatsappOfficialAPI from "../helpers/WhatsappOfficialAPI"; + import whatsappOfficialNumberInfo from "../helpers/WhatsappOfficialNumberInfo"; import { getSettingValue } from "../helpers/WhaticketSettings"; import ListWhatsAppsNumber from "../services/WhatsappService/ListWhatsAppsNumber"; @@ -288,7 +287,8 @@ export const weebhook = async ( let filename = await receiveWhatsAppMediaOfficialAPI( mediaId, - whatsapp.phoneNumberId + whatsapp.phoneNumberId, + whatsapp.whatsappOfficialToken ); if (!filename) throw new AppError("There was an error"); diff --git a/backend/src/database/migrations/20240610183159-add-column-whatsappOfficialToken-to-whatsapp.ts b/backend/src/database/migrations/20240610183159-add-column-whatsappOfficialToken-to-whatsapp.ts new file mode 100644 index 0000000..bf5a724 --- /dev/null +++ b/backend/src/database/migrations/20240610183159-add-column-whatsappOfficialToken-to-whatsapp.ts @@ -0,0 +1,14 @@ +import { QueryInterface, DataTypes } from "sequelize"; + +module.exports = { + up: (queryInterface: QueryInterface) => { + return queryInterface.addColumn("Whatsapps", "whatsappOfficialToken", { + type: DataTypes.STRING, + allowNull: true + }); + }, + + down: (queryInterface: QueryInterface) => { + return queryInterface.removeColumn("Whatsapps", "whatsappOfficialToken"); + } +}; diff --git a/backend/src/helpers/ReceiveWhatsAppMediaOfficialAPI.ts b/backend/src/helpers/ReceiveWhatsAppMediaOfficialAPI.ts index fec1aa9..5cf3fc3 100644 --- a/backend/src/helpers/ReceiveWhatsAppMediaOfficialAPI.ts +++ b/backend/src/helpers/ReceiveWhatsAppMediaOfficialAPI.ts @@ -10,7 +10,6 @@ import { import { writeFile } from "fs"; -import whatsappOfficialAPI from "./WhatsappOfficialAPI"; import path, { join } from "path"; import { promisify } from "util"; @@ -18,14 +17,20 @@ import mime from "mime"; import fs from "fs"; import { response } from "express"; +import createApiClientWhatsOfficial from "./WhatsappOfficialAPI"; const writeFileAsync = promisify(writeFile); async function receiveWhatsAppMediaOfficialAPI( mediaId: string, - phoneNumberId: string + phoneNumberId: string, + whatsappOfficialToken: string ) { try { + const whatsappOfficialAPI = createApiClientWhatsOfficial( + whatsappOfficialToken + ); + const { data } = await whatsappOfficialAPI.get( `/${process.env.VERSION}/${mediaId}?phone_number_id=${phoneNumberId}` ); @@ -33,7 +38,7 @@ async function receiveWhatsAppMediaOfficialAPI( if (data && data?.url) { const config: any = { headers: { - Authorization: `Bearer ${process.env.TOKEN}` + Authorization: `Bearer ${whatsappOfficialToken}` }, responseType: "arraybuffer" }; @@ -44,7 +49,7 @@ async function receiveWhatsAppMediaOfficialAPI( const ext = response.headers["content-type"].split("/")[1]; - const filename_ext = `${filename}.${ext}`; + const filename_ext = `${filename}.${ext}`; const destPath = path.join( __dirname, diff --git a/backend/src/helpers/SetMessageAsRead.ts b/backend/src/helpers/SetMessageAsRead.ts index d29d297..e7aef63 100644 --- a/backend/src/helpers/SetMessageAsRead.ts +++ b/backend/src/helpers/SetMessageAsRead.ts @@ -3,9 +3,7 @@ import { getWbot } from "../libs/wbot"; import Message from "../models/Message" import Ticket from "../models/Ticket"; import Whatsapp from "../models/Whatsapp"; -import endPointQuery from "./old_EndPointQuery"; - -import whatsappOfficialAPI from "./WhatsappOfficialAPI"; +import endPointQuery from "./old_EndPointQuery"; export async function setMessageAsRead(ticket: Ticket) { if (ticket?.phoneNumberId) { diff --git a/backend/src/helpers/WhatsappOfficialAPI.ts b/backend/src/helpers/WhatsappOfficialAPI.ts index 03b27a4..3412751 100644 --- a/backend/src/helpers/WhatsappOfficialAPI.ts +++ b/backend/src/helpers/WhatsappOfficialAPI.ts @@ -3,14 +3,28 @@ import https from "https" import http from "http" -const api = axios.create({ - baseURL: process.env.URL_WHATSAPP_API, - headers: { - Accept: "application/json", - Authorization: `Bearer ${process.env.TOKEN}` - }, - httpAgent: new http.Agent({ keepAlive: true }), - httpsAgent: new https.Agent({ keepAlive: true }) -}); +// const api = axios.create({ +// baseURL: process.env.URL_WHATSAPP_API, +// headers: { +// Accept: "application/json", +// Authorization: `Bearer ${process.env.TOKEN}` +// }, +// httpAgent: new http.Agent({ keepAlive: true }), +// httpsAgent: new https.Agent({ keepAlive: true }) +// }); -export default api; +// export default api; + +const createApiClientWhatsOfficial = (token: string) => { + return axios.create({ + baseURL: process.env.URL_WHATSAPP_API, + headers: { + Accept: "application/json", + Authorization: `Bearer ${token}` + }, + httpAgent: new http.Agent({ keepAlive: true }), + httpsAgent: new https.Agent({ keepAlive: true }) + }); +}; + +export default createApiClientWhatsOfficial; diff --git a/backend/src/helpers/WhatsappOfficialNumberInfo.ts b/backend/src/helpers/WhatsappOfficialNumberInfo.ts index 77ae2b3..9e08ea2 100644 --- a/backend/src/helpers/WhatsappOfficialNumberInfo.ts +++ b/backend/src/helpers/WhatsappOfficialNumberInfo.ts @@ -1,7 +1,15 @@ -import whatsappOfficialAPI from "./WhatsappOfficialAPI"; +import Whatsapp from "../models/Whatsapp"; +import createApiClientWhatsOfficial from "./WhatsappOfficialAPI"; async function whatsappOfficialNumberInfo(wabaId: string) { try { + const { whatsappOfficialToken }: any = await Whatsapp.findOne({ + where: { wabaId } + }); + + const whatsappOfficialAPI = createApiClientWhatsOfficial( + whatsappOfficialToken + ); const { data } = await whatsappOfficialAPI.get( `/${process.env.VERSION}/${wabaId}/phone_numbers` ); diff --git a/backend/src/helpers/sendWhatsAppMessageOfficialAPI.ts b/backend/src/helpers/sendWhatsAppMessageOfficialAPI.ts index ec87574..91d9967 100644 --- a/backend/src/helpers/sendWhatsAppMessageOfficialAPI.ts +++ b/backend/src/helpers/sendWhatsAppMessageOfficialAPI.ts @@ -1,3 +1,4 @@ +import { where } from "sequelize"; import { getIO } from "../libs/socket"; import Contact from "../models/Contact"; import Ticket from "../models/Ticket"; @@ -5,8 +6,9 @@ import { isValidMsg, verifyMessage } from "../services/WbotServices/wbotMessageListener"; - -import whatsappOfficialAPI from "./WhatsappOfficialAPI"; +import { get } from "./RedisClient"; +import createApiClientWhatsOfficial from "./WhatsappOfficialAPI"; +import Whatsapp from "../models/Whatsapp"; async function sendWhatsAppMessageOfficialAPI( ticket: Ticket, @@ -52,8 +54,15 @@ async function sendWhatsAppMessageOfficialAPI( return; } - console.log("SEND MESSAGE: ", JSON.stringify(data, null,2)); - + const { whatsappOfficialToken }: any = await Whatsapp.findOne({ + where: { phoneNumberId } + }); + + console.log("SEND MESSAGE: ", JSON.stringify(data, null, 2)); + const whatsappOfficialAPI = createApiClientWhatsOfficial( + whatsappOfficialToken + ); + whatsappOfficialAPI .post(`/${process.env.VERSION}/${phoneNumberId}/messages`, data) .then(response => { diff --git a/backend/src/helpers/sendWhatsMediaOfficialAPI.ts b/backend/src/helpers/sendWhatsMediaOfficialAPI.ts index d2c6390..dff553e 100644 --- a/backend/src/helpers/sendWhatsMediaOfficialAPI.ts +++ b/backend/src/helpers/sendWhatsMediaOfficialAPI.ts @@ -12,13 +12,14 @@ import { import ffmpeg from "fluent-ffmpeg"; import fs from "fs"; -import whatsappOfficialAPI from "./WhatsappOfficialAPI"; import path from "path"; import { convertAudioToOgg } from "../helpers/ConvertAudio"; import { bytesToMB } from "./BytesToMB"; import isThisHour from "date-fns/esm/isThisHour/index"; import AppError from "../errors/AppError"; +import createApiClientWhatsOfficial from "./WhatsappOfficialAPI"; +import Whatsapp from "../models/Whatsapp"; async function sendWhatsMediaOfficialAPI( ticket: Ticket, @@ -79,6 +80,14 @@ async function sendWhatsMediaOfficialAPI( return; } + const { whatsappOfficialToken }: any = await Whatsapp.findOne({ + where: { phoneNumberId } + }); + + const whatsappOfficialAPI = createApiClientWhatsOfficial( + whatsappOfficialToken + ); + whatsappOfficialAPI .post(`/${process.env.VERSION}/${phoneNumberId}/messages`, data) .then(response => { diff --git a/backend/src/models/Whatsapp.ts b/backend/src/models/Whatsapp.ts index 28cae6a..60ff078 100644 --- a/backend/src/models/Whatsapp.ts +++ b/backend/src/models/Whatsapp.ts @@ -87,6 +87,9 @@ class Whatsapp extends Model { @UpdatedAt updatedAt: Date; + @Column + whatsappOfficialToken: string; + @HasMany(() => Ticket) tickets: Ticket[];