diff --git a/backend/src/services/WbotServices/wbotMessageListener.ts b/backend/src/services/WbotServices/wbotMessageListener.ts index bacb1241..ca9566d4 100644 --- a/backend/src/services/WbotServices/wbotMessageListener.ts +++ b/backend/src/services/WbotServices/wbotMessageListener.ts @@ -52,6 +52,8 @@ import { cacheLayer } from "../../libs/cache"; import { debounce } from "../../helpers/Debounce"; import { getMessageOptions } from "./SendWhatsAppMedia"; import { makeRandomId } from "../../helpers/MakeRandomId"; +import { GetCompanySetting } from "../../helpers/CheckSettings"; +import Whatsapp from "../../models/Whatsapp"; type Session = WASocket & { @@ -1166,6 +1168,15 @@ const handleChartbot = async (ticket: Ticket, msg: WAMessage, wbot: Session, don const option = queue?.options.find((o) => o.option === messageBody); if (option) { await ticket.update({ queueOptionId: option?.id }); + } else if (await GetCompanySetting(ticket.companyId, "chatbotAutoExit") === "enabled" ) { + // message didn't identified an option and company setting to exit chatbot + await ticket.update({ chatbot: false }); + const whatsapp = await Whatsapp.findByPk(ticket.whatsappId); + const contact = await Contact.findByPk(ticket.contactId); + if (whatsapp.transferMessage) { + const body = formatBody(`\u200e${whatsapp.transferMessage}`, contact); + await SendWhatsAppMessage({ body, ticket }); + } } } diff --git a/frontend/src/components/Settings/Options.js b/frontend/src/components/Settings/Options.js index 66b1b8e3..6c072da0 100644 --- a/frontend/src/components/Settings/Options.js +++ b/frontend/src/components/Settings/Options.js @@ -88,6 +88,7 @@ export default function Options(props) { const [chatbotType, setChatbotType] = useState(""); const [quickMessages, setQuickMessages] = useState(""); const [allowSignup, setAllowSignup] = useState("disabled"); + const [chatbotAutoExit, setChatbotAutoExit] = useState("disabled"); const [CheckMsgIsGroup, setCheckMsgIsGroupType] = useState("enabled"); const [loadingUserRating, setLoadingUserRating] = useState(false); @@ -96,6 +97,7 @@ export default function Options(props) { const [loadingChatbotType, setLoadingChatbotType] = useState(false); const [loadingQuickMessages, setLoadingQuickMessages] = useState(false); const [loadingAllowSignup, setLoadingAllowSignup] = useState(false); + const [loadingChatbotAutoExit, setLoadingChatbotAutoExit] = useState(false); const [loadingCheckMsgIsGroup, setCheckMsgIsGroup] = useState(false); const { getCurrentUserInfo } = useAuth(); const [currentUser, setCurrentUser] = useState({}); @@ -130,6 +132,10 @@ export default function Options(props) { if (chatbotType) { setChatbotType(chatbotType.value); } + const chatbotAutoExit = settings.find((s) => s.key === "chatbotAutoExit"); + if (chatbotAutoExit) { + setChatbotAutoExit(chatbotAutoExit.value); + } const allowSignup = settings.find((s) => s.key === "allowSignup"); if (allowSignup) { setAllowSignup(allowSignup.value); @@ -196,6 +202,17 @@ export default function Options(props) { setLoadingChatbotType(false); } + async function handleChatbotAutoExit(value) { + setChatbotAutoExit(value); + setLoadingChatbotAutoExit(true); + await update({ + key: "chatbotAutoExit", + value, + }); + toast.success("Operação atualizada com sucesso."); + setLoadingChatbotAutoExit(false); + } + async function handleQuickMessages(value) { setQuickMessages(value); setLoadingQuickMessages(true); @@ -336,6 +353,26 @@ export default function Options(props) { + + + + Saída automática de chatbot + + + + {loadingChatbotAutoExit && "Atualizando..."} + + +