UNPKG

@invincible_rd/test-two

Version:

Ultron SDK for integrating conversational AI Avatars into your web application

73 lines (60 loc) 2.5 kB
export function initUltronAI(divId: string, iframeLink: string, loadingElementId: string): void { const div = document.getElementById(divId); if (!div) { console.error(`Div with id ${divId} not found`); return; } const loadingElement = document.getElementById(loadingElementId); if (!loadingElement) { console.warn(`Loading element with id ${loadingElementId} not found`); } const iframe = createIframe(iframeLink, loadingElement); div.appendChild(iframe); unlockAudioContext(() => { notifyUserInteraction(iframe, iframeLink); }); // setupMessageListener(iframe, iframeLink); } function createIframe(iframeLink: string, loadingElement: HTMLElement | null): HTMLIFrameElement { const iframe = document.createElement('iframe'); iframe.src = iframeLink; iframe.width = '100%'; iframe.height = '100%'; iframe.style.display = 'none'; iframe.allow = 'microphone; camera; autoplay'; iframe.onload = () => { iframe.style.display = 'block'; if (loadingElement) { loadingElement.style.display = 'none'; } }; return iframe; } function unlockAudioContext(callback: () => void): void {} export function notifyUserInteraction(iframe: HTMLIFrameElement, url: string): void{ sendMessageToIframe(iframe, { type: 'USER_INTERACTION' }, url); }; export function setupMessageListener(iframe: HTMLIFrameElement, iframeLink: string, callback: (data: any) => void): void { window.addEventListener('message', (event) => { callback(event.data); }); } export function sendMessageToIframe(iframe: HTMLIFrameElement, message: any, url: string): void { if (iframe.contentWindow) { iframe.contentWindow.postMessage(message, url); } else { console.error('Iframe contentWindow is null'); } } export function sendMessageContent(iframe: HTMLIFrameElement, message: string, speak: boolean, lipsync: boolean, url: string): void { sendMessageToIframe(iframe, { type: 'MESSAGE_CONTENT', message, speak, lipsync }, url); } export function sendSubmitEvent(iframe: HTMLIFrameElement, url: string): void { sendMessageToIframe(iframe, { type: 'SUBMIT_EVENT' }, url); } export function sendAudioMessage(iframe: HTMLIFrameElement, audioData: string, url: string): void { sendMessageToIframe(iframe, { type: 'MESSAGE_AUDIO', audioData }, url); } export function sendAudioUrlMessage(iframe: HTMLIFrameElement, audioUrl: string, url: string): void { sendMessageToIframe(iframe, { type: 'MESSAGE_AUDIO_URL', audioUrl }, url); }