(function (w) { const includeVersion = "includeConvyAI_v1.js"; let fullPage, autoStart, autoStartDelay, hideButton; let isMinimized = true; const eventsMap = {}; const triggerCb = (event, payload) => { if (eventsMap[event]) { payload = payload ? payload : {}; eventsMap[event](payload); } }; const CustomerGuiEventType = { OPEN_CUSTOMER_GUI: "OPEN_CUSTOMER_GUI", CLOSE_CUSTOMER_GUI: "CLOSE_CUSTOMER_GUI", READY_CUSTOMER_GUI: "READY_CUSTOMER_GUI", CONVERSATION_CREATED: "CONVERSATION_CREATED", CONVERSATION_CLOSED: "CONVERSATION_CLOSED", MESSAGE_RECEIVED: "MESSAGE_RECEIVED", EVENT_RECEIVED: "EVENT_RECEIVED", HANDOVER_STARTED: "HANDOVER_STARTED", CAN_START: "CAN_START", HANDOVER_SUCCESSFUL: "HANDOVER_SUCCESSFUL", HANDOVER_FAILED: "HANDOVER_FAILED", HANDOVER_CLOSED: "HANDOVER_CLOSED", SET_IFRAME_SIZE: "SET_IFRAME_SIZE", }; const IncludeConvyAIEventType = { INITIAL_WINDOW_SIZE: "INITIAL_WINDOW_SIZE", WINDOW_RESIZED: "WINDOW_RESIZED", }; const ExternalConvyAIMethods = { GUI_INITIALIZATION: "GUI_INITIALIZATION", ATTACHED_DATA: "ATTACHED_DATA", MESSAGE_SENT: "MESSAGE_SENT", CONVERSATION_STARTED: "CONVERSATION_STARTED", CONVERSATION_ENDED: "CONVERSATION_ENDED", EVENT_SENT: "EVENT_SENT", }; const getIFrameId = () => { return "convy_ai_v1_" + rnd; }; const getSelfScript = () => { const scripts = document.getElementsByTagName("script"); for (const script of scripts) { if (script.src.indexOf(includeVersion) !== -1) { return script; } } }; const selfScriptURL = new URL(getSelfScript().src); const rnd = selfScriptURL.searchParams.get("rnd"); const iframeInjected = document.getElementById(getIFrameId()) ? true : false; let iframeSize = { minimized: { width: "255px", height: "85px", afterFirstInteractionWidth: "105px" }, fullPage: { width: "100%", height: "100%" }, standard: { width: "435px", height: "90%" }, fullPageBreakpoint: 748, position: { bottom: "0", right: "0", position: "fixed", zIndex: "999999999", overflow: "hidden", border: "0" } }; const init = () => { let src = "https://admin.convy.ai/static/include_v2/interfaces/companies/245f1351934c429e8f842b5472ae36e5/GuiDeghi/dist/index.html"; if (src === "%URL%") { const guiPath = (selfScriptURL.searchParams.get("guiPath") || "dist") + "/"; src = selfScriptURL.href.replace(includeVersion, guiPath) + "&page=" + window.location.pathname + "&domain=" + window.location.hostname; if (selfScriptURL.searchParams.get("developerLocalPath")) { src = selfScriptURL.searchParams.get("developerLocalPath") + selfScriptURL.search + "#/"; } } else { src += "?" + selfScriptURL.searchParams.toString() + "&page=" + window.location.pathname + "&domain=" + window.location.hostname; } const iframe = document.createElement("iframe"); iframe.setAttribute("id", getIFrameId()); iframe.setAttribute("src", src); iframe.setAttribute("frameBorder", "0"); iframe.setAttribute("scrolling", "no"); iframe.setAttribute("allow", "display-capture; microphone; camera; geolocation; clipboard-read; clipboard-write"); iframe.setAttribute("allowFullScreen", ""); iframe.setAttribute("webkitallowfullscreen", ""); iframe.setAttribute("mozallowfullscreen", ""); iframe.style.width = iframeSize.minimized.width; iframe.style.height = iframeSize.minimized.height; iframe.style.background = "none"; for (const attr in iframeSize.position) { iframe.style[attr] = iframeSize.position[attr]; } document.body.appendChild(iframe); registerCustomerGUIPostMessageListener(); registerWindowResize(); }; const registerWindowResize = () => { let resizeId; window.addEventListener("resize", () => { clearTimeout(resizeId); resizeId = setTimeout(resizeEnd, 500); }); }; const resizeEnd = () => { sendPostMessageToCustomerGUI(IncludeConvyAIEventType.WINDOW_RESIZED, { width: window.innerWidth, height: window.innerHeight }); const iframe = document.getElementById(getIFrameId()); setFrameSize(iframe); }; const sendPostMessageToCustomerGUI = (type, payload = {}) => { const iframe = document.getElementById(getIFrameId()); iframe.contentWindow.postMessage( { type, payload, }, "*", ); }; //registra i listener che arrivano dalla customer gui const registerCustomerGUIPostMessageListener = () => { w.addEventListener("message", receiveEventFromCustomerGui, false); }; const receiveEventFromCustomerGui = (event) => { const iframe = document.getElementById(getIFrameId()); switch (event.data.type) { case CustomerGuiEventType.SET_IFRAME_SIZE: iframeSize = Object.assign({}, iframeSize, event.data.payload); for (const attr in iframeSize.position) { iframe.style[attr] = iframeSize.position[attr]; } setFrameSize(iframe); break; case CustomerGuiEventType.OPEN_CUSTOMER_GUI: isMinimized = false; //gestire breakpoint mobile 748 + eventuali resize della pagina su desktop setFrameSize(iframe); break; case CustomerGuiEventType.CLOSE_CUSTOMER_GUI: isMinimized = true; setFrameSize(iframe); break; case CustomerGuiEventType.READY_CUSTOMER_GUI: sendPostMessageToCustomerGUI(IncludeConvyAIEventType.INITIAL_WINDOW_SIZE, { width: window.innerWidth, height: window.innerHeight }); break; case CustomerGuiEventType.CAN_START: w.ConvyAI.init({ autoStart, autoStartDelay, hideButton, fullPage, }); break; case CustomerGuiEventType.MESSAGE_RECEIVED: triggerCb(event.data.type, { type: event.data.type, message: event.data.payload.message }); break; case CustomerGuiEventType.EVENT_RECEIVED: triggerCb(event.data.type, { type: event.data.type, event: event.data.payload }); break; case CustomerGuiEventType.CONVERSATION_CREATED: case CustomerGuiEventType.CONVERSATION_CLOSED: case CustomerGuiEventType.HANDOVER_STARTED: case CustomerGuiEventType.HANDOVER_SUCCESSFUL: case CustomerGuiEventType.HANDOVER_FAILED: case CustomerGuiEventType.HANDOVER_CLOSED: triggerCb(event.data.type, { type: event.data.type }); break; } }; const setFrameSize = (frame) => { if (isMinimized) { frame.style.width = iframeSize.minimized.width; frame.style.height = iframeSize.minimized.height; } else { iframeSize.minimized.width = iframeSize.minimized.afterFirstInteractionWidth; const maxWidth = Math.max(document?.body?.clientWidth, document?.documentElement?.clientWidth); if (maxWidth < iframeSize.fullPageBreakpoint || fullPage) { frame.style.width = iframeSize.fullPage.width; frame.style.height = iframeSize.fullPage.height; } else { frame.style.width = iframeSize.standard.width; frame.style.height = iframeSize.standard.height; } } }; const logger = (action, data) => { console.log(`${new Date().toISOString()} [EMBEDDING-SDK] ${action} ${data ? "\n" + JSON.stringify(data) : ""}`); }; const initConvyAI = () => { return { getVersion: () => { return includeVersion; }, init: (conf) => { const iframe = document.getElementById(getIFrameId()); fullPage = conf.fullPage; if (fullPage) { setFrameSize(iframe); } const initParams = { autoStart: typeof conf.autoStart !== "undefined" ? conf.autoStart : false, autoStartDelay: conf.autoStartDelay || 0, hideButton: typeof conf.hideButton !== "undefined" ? conf.hideButton : true, fullPage: typeof conf.fullPage !== "undefined" ? conf.fullPage : false, }; sendPostMessageToCustomerGUI(ExternalConvyAIMethods.GUI_INITIALIZATION, initParams); logger("INIT DONE", initParams); }, resetFrameSize: () => { iframeSize.minimized.width = "255px"; }, listen: { conversationCreated: (callBack) => { eventsMap[CustomerGuiEventType.CONVERSATION_CREATED] = callBack; logger("ADDED LISTENER ON CONVERSATION CREATED"); }, conversationClosed: (callBack) => { eventsMap[CustomerGuiEventType.CONVERSATION_CLOSED] = callBack; logger("ADDED LISTENER ON CONVERSATION CLOSED"); }, messageReceived: (callBack) => { eventsMap[CustomerGuiEventType.MESSAGE_RECEIVED] = callBack; logger("ADDED LISTENER ON MESSAGE RECEIVED"); }, eventReceived: (callBack) => { eventsMap[CustomerGuiEventType.EVENT_RECEIVED] = callBack; logger("ADDED LISTENER ON EVENT RECEIVED"); }, handoverStarted: (callBack) => { eventsMap[CustomerGuiEventType.HANDOVER_STARTED] = callBack; logger("ADDED LISTENER ON HANDOVER STARTED"); }, handoverSuccessful: (callBack) => { eventsMap[CustomerGuiEventType.HANDOVER_SUCCESSFUL] = callBack; logger("ADDED LISTENER ON HANDOVER SUCCESSFUL"); }, handoverFailed: (callBack) => { eventsMap[CustomerGuiEventType.HANDOVER_FAILED] = callBack; logger("ADDED LISTENER ON HANDOVER FAILED"); }, handoverClosed: (callBack) => { eventsMap[CustomerGuiEventType.HANDOVER_CLOSED] = callBack; logger("ADDED LISTENER ON HANDOVER CLOSED"); }, }, attachData: (data) => { sendPostMessageToCustomerGUI(ExternalConvyAIMethods.ATTACHED_DATA, { attachedData: data, }); logger("EXTERNAL ATTACH DATA", data); }, sendMessage: (message) => { sendPostMessageToCustomerGUI(ExternalConvyAIMethods.MESSAGE_SENT, { message, }); logger("EXTERNAL SEND MESSAGE", message); }, startConversation: () => { sendPostMessageToCustomerGUI(ExternalConvyAIMethods.CONVERSATION_STARTED); logger("EXTERNAL START CONVERSATION"); }, endConversation: () => { sendPostMessageToCustomerGUI(ExternalConvyAIMethods.CONVERSATION_ENDED); logger("EXTERNAL END CONVERSATION"); }, sendEvent: (name, parameters) => { sendPostMessageToCustomerGUI(ExternalConvyAIMethods.EVENT_SENT, { name, parameters }); logger("EXTERNAL SEND EVENT"); }, }; }; const initIframe = () => { if (!iframeInjected && document.body) { (autoStart = selfScriptURL.searchParams.has("autostart")), (autoStartDelay = selfScriptURL.searchParams.get("autostartdelay") || 500), (hideButton = selfScriptURL.searchParams.has("hidebutton")), (fullPage = selfScriptURL.searchParams.has("fullpage")), init(); if (typeof w.ConvyAI === "undefined") { w.ConvyAI = initConvyAI(); } } }; w.addEventListener("load", () => { initIframe(); }); const autoinclude = selfScriptURL.searchParams.get("autoinclude"); if (autoinclude) { initIframe(); } })(window);