diff --git a/backend/src/controllers/ContactController.ts b/backend/src/controllers/ContactController.ts index 02843a1..918c1fd 100644 --- a/backend/src/controllers/ContactController.ts +++ b/backend/src/controllers/ContactController.ts @@ -257,10 +257,10 @@ export const contacsBulkInsertOnQueue = async ( }; function addStartPhoneNumber(phoneNumber: string) { - const regex = /^55/; + const regex = /^57/; if (!regex.test(phoneNumber)) { - phoneNumber = "55" + phoneNumber; + phoneNumber = "57" + phoneNumber; } return phoneNumber; diff --git a/backend/src/helpers/CloseBotTickets.ts b/backend/src/helpers/CloseBotTickets.ts index 2276dad..4eb45b1 100644 --- a/backend/src/helpers/CloseBotTickets.ts +++ b/backend/src/helpers/CloseBotTickets.ts @@ -51,10 +51,10 @@ const schedule = async () => { } catch (error) { console.log("error on schedule: ", error); } finally { - timer = setInterval(schedule, 180000); + timer = setInterval(schedule, 299999); } }; -timer = setInterval(schedule, 180000); +timer = setInterval(schedule, 299999); export default schedule; diff --git a/backend/src/services/WbotServices/SendWhatsAppMedia.ts b/backend/src/services/WbotServices/SendWhatsAppMedia.ts index 9f439bd..11449cf 100644 --- a/backend/src/services/WbotServices/SendWhatsAppMedia.ts +++ b/backend/src/services/WbotServices/SendWhatsAppMedia.ts @@ -1,4 +1,5 @@ import fs from "fs"; +import path from "path"; import { MessageMedia, Message as WbotMessage } from "whatsapp-web.js"; import AppError from "../../errors/AppError"; import GetTicketWbot from "../../helpers/GetTicketWbot"; @@ -14,51 +15,108 @@ import { mediaTypeWhatsappOfficial } from "./wbotMessageListener"; import { bytesToMB } from "../../helpers/BytesToMB"; interface Request { - media: Express.Multer.File; + media?: Express.Multer.File; ticket: Ticket; - mic_audio?: any + mic_audio?: any; + filePath?: string; } const SendWhatsAppMedia = async ({ media, ticket, - mic_audio + mic_audio, + filePath }: Request): Promise => { const { phoneNumberId } = ticket; if (phoneNumberId) { - const { type, mbMaxSize }: any = mediaTypeWhatsappOfficial(media.mimetype); + if (media) { + const { type, mbMaxSize }: any = mediaTypeWhatsappOfficial(media.mimetype); - const filesize: any = bytesToMB(media.size); + const filesize: any = bytesToMB(media.size); - if (filesize > mbMaxSize) { - throw new AppError("FILE TOO LARGE!"); + if (filesize > mbMaxSize) { + throw new AppError("FILE TOO LARGE!"); + } + if (!type) { + throw new AppError("FILE TYPE NOT SUPPORTED!"); + } + + sendWhatsMediaOfficialAPI(ticket, media, type, mic_audio); + return; } - if (!type) { - throw new AppError("FILE TYPE NOT SUPPORTED!"); - } - - sendWhatsMediaOfficialAPI(ticket, media, type, mic_audio); - return; } try { - const newMedia = MessageMedia.fromFilePath(media.path); + let newMedia: MessageMedia; + if (filePath) { + newMedia = MessageMedia.fromFilePath(filePath); + } else if (media) { + newMedia = MessageMedia.fromFilePath(media.path); + } else { + throw new AppError("No media provided!"); + } sendWhatsAppMediaSocket(ticket, newMedia); - await ticket.update({ lastMessage: media.filename }); + const lastMessage = filePath ? path.basename(filePath) : media?.filename; + + await ticket.update({ lastMessage }); await updateTicketCacheByTicketId(ticket.id, { - lastMessage: media.filename, + lastMessage, updatedAt: new Date(ticket.updatedAt).toISOString() }); - console.log("media.path: ", media.path); - fs.unlinkSync(media.path); + if (media) { + fs.unlinkSync(media.path); + } } catch (err) { throw new AppError("ERR_SENDING_WAPP_MSG"); } + // const { phoneNumberId } = ticket; + + // if (phoneNumberId) { + // const { type, mbMaxSize }: any = mediaTypeWhatsappOfficial(media.mimetype); + + // const filesize: any = bytesToMB(media.size); + + // if (filesize > mbMaxSize) { + // throw new AppError("FILE TOO LARGE!"); + // } + // if (!type) { + // throw new AppError("FILE TYPE NOT SUPPORTED!"); + // } + + // sendWhatsMediaOfficialAPI(ticket, media, type, mic_audio); + // return; + // } + + // try { + // //const newMedia = MessageMedia.fromFilePath(media.path); + // let newMedia: MessageMedia; + // if (filePath) { + // newMedia = MessageMedia.fromFilePath(filePath); + // } else if (media) { + // newMedia = MessageMedia.fromFilePath(media.path); + // } else { + // throw new AppError("No media provided!"); + // } + + // sendWhatsAppMediaSocket(ticket, newMedia); + + // await ticket.update({ lastMessage: media.filename }); + + // await updateTicketCacheByTicketId(ticket.id, { + // lastMessage: media.filename, + // updatedAt: new Date(ticket.updatedAt).toISOString() + // }); + + // console.log("media.path: ", media.path); + // fs.unlinkSync(media.path); + // } catch (err) { + // throw new AppError("ERR_SENDING_WAPP_MSG"); + // } }; export default SendWhatsAppMedia; diff --git a/backend/src/services/WbotServices/wbotMessageListener.ts b/backend/src/services/WbotServices/wbotMessageListener.ts index b6f1b80..85959b4 100644 --- a/backend/src/services/WbotServices/wbotMessageListener.ts +++ b/backend/src/services/WbotServices/wbotMessageListener.ts @@ -110,7 +110,7 @@ import AssociateContatctQueue from "../ContactServices/AssociateContatctQueue"; import ContactQueue from "../../models/ContactQueues"; import { encodeFileToBase64 } from "../../helpers/EncodeFileToBase64"; import { Json } from "sequelize/types/lib/utils"; - +import SendWhatsAppMedia from "./SendWhatsAppMedia"; var lst: any[] = getWhatsappIds(); interface Session extends Client { @@ -478,13 +478,13 @@ const verifyMediaMessage = async ( phoneNumberId: msg?.phoneNumberId, fromAgent: false }; - if ( - messageData.mediaType === "video" || - (messageData.mediaType === "audio" && - getSettingValue("blockAudioVideoMedia")?.value === "enabled") - ) { - mediaAuthorized = false; - } + // if ( + // messageData.mediaType === "video" || + // (messageData.mediaType === "audio" && + // getSettingValue("blockAudioVideoMedia")?.value === "enabled") + // ) { + // mediaAuthorized = false; + // } if (msg?.fromMe) { messageData = { ...messageData, fromAgent: true }; } @@ -1257,7 +1257,7 @@ const handleMessage = async ( if (msg.type == "chat" && String(msg.body).length > 120) { botSendMessage( ticket, - `Desculpe, nao compreendi!\nTexto acima de 120 caracteres!\n _Digite *0* para voltar ao menu principal._` + `¡Disculpa, no comprendí!\n\n¡El mensaje tiene más de 120 caracteres!\n\n_Digite 0 para volver al menú principal._` ); return; } @@ -1394,14 +1394,14 @@ const handleMessage = async ( return; } - const menuMsg: any = await menu(msg.body, wbot.id, contact.id); + const menuMsg: any = await menu(msg.body, wbot.id, contact.id, ticket); console.log("menuMsg: ", menuMsg); await botSendMessage( ticket, menuMsg.value + - "\n\nSi desea volver al menú principal, escriba *0* o *#* para volver al menú anterior" + "\n\nSi deseas volver al menú principal, escriba *0* o *#* para volver al menú anterior" ); if ( @@ -1484,7 +1484,7 @@ const handleMessage = async ( }, ticketId: ticket.id }); - const menuMsg: any = await menu(msg.body, wbot.id, contact.id); + const menuMsg: any = await menu(msg.body, wbot.id, contact.id, ticket); await botSendMessage(ticket, menuMsg.value); } @@ -1507,7 +1507,7 @@ const handleMessage = async ( ticketId: ticket.id }); - const menuMsg: any = await menu(msg.body, wbot.id, contact.id); + const menuMsg: any = await menu(msg.body, wbot.id, contact.id, ticket); await botSendMessage(ticket, menuMsg.value); @@ -1549,7 +1549,7 @@ const handleMessage = async ( }; async function previousUra(wbot: any, contact: Contact, ticket: any) { - const menuMsg: any = await menu("#", wbot.id, contact.id); + const menuMsg: any = await menu("#", wbot.id, contact.id, ticket); botSendMessage(ticket, menuMsg.value); } @@ -1602,7 +1602,7 @@ function delForm(ticket: any) { del(`form:${ticket.id}:confirmation`); } -const menu = async (userTyped: string, whatsappId: any, contactId: any) => { +const menu = async (userTyped: string, whatsappId: any, contactId: any, ticket: any) => { let whatsapp = await whatsappCache(whatsappId); let lastId = await findObject( @@ -1624,6 +1624,21 @@ const menu = async (userTyped: string, whatsappId: any, contactId: any) => { value: data[1].id, history: `|${data[1].id}` }); + if(whatsapp && whatsapp.number === '573168762241'){ + const imagePath = path.join(__dirname, '..','..','utils','OET_inicio.jpg'); + await SendWhatsAppMedia({ + ticket, + filePath: imagePath + }); + console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Send image init') + }else if(whatsapp && whatsapp.number === '573159260397'){ + const imagePath = path.join(__dirname, '..','..','utils','Faro_inicio.jpeg'); + await SendWhatsAppMedia({ + ticket, + filePath: imagePath + }); + console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Send image init') + } } lastId = await findObject( @@ -1884,10 +1899,13 @@ async function whatsappCache(whatsappId: any) { let whatsapp = await get({ key: "whatsapp:*", parse: true }); let uraByNumber = await get({ key: "ura_*", parse: true }); - // console.log("------------------------> uraByNumber: ", uraByNumber); + whatsapp = whatsapp.filter((w: any) => +w?.id == +whatsappId); + console.log("------------------------> whatsapp: ", whatsapp); + + if (whatsapp && whatsapp.length > 0 && uraByNumber) return whatsapp[0]; } diff --git a/backend/src/utils/Faro_inicio.jpeg b/backend/src/utils/Faro_inicio.jpeg new file mode 100644 index 0000000..7c47236 Binary files /dev/null and b/backend/src/utils/Faro_inicio.jpeg differ diff --git a/backend/src/utils/OET_inicio.jpg b/backend/src/utils/OET_inicio.jpg new file mode 100644 index 0000000..fcace56 Binary files /dev/null and b/backend/src/utils/OET_inicio.jpg differ diff --git a/frontend/src/components/MessageInput/index.js b/frontend/src/components/MessageInput/index.js index 9f0b895..f20b25a 100644 --- a/frontend/src/components/MessageInput/index.js +++ b/frontend/src/components/MessageInput/index.js @@ -247,7 +247,7 @@ const MessageInput = ({ ticketStatus, ticketLastMessage, ticketIsRemote }) => { setInputMessage(ticketLastMessage) } else { - setInputMessage("") + //setInputMessage("") } }, [countTicketMsg, ticketIsRemote, ticketLastMessage])