import Widget from './widget' ; import { IMessage } from "../typings" ; export default class Api { widget : Widget ; constructor ( widget ) { this . widget = widget ; } open ( ) { this . widget . open ( ) ; } close ( ) { this . widget . close ( ) ; } toggle ( ) { this . widget . toggle ( ) ; } isOpen ( ) { return this . widget . state . isChatOpen === true ; } callChatWidget ( payload ) { if ( this . isOpen ( ) ) { ( document . getElementById ( 'chatBotManFrame' ) as HTMLIFrameElement ) . contentWindow . postMessage ( payload , '*' ) ; } else { try { this . open ( ) ; setTimeout ( ( ) => { ( document . getElementById ( 'chatBotManFrame' ) as HTMLIFrameElement ) . contentWindow . postMessage ( payload , '*' ) ; } , 750 ) ; } catch ( e ) { console . error ( e ) ; } } } writeToMessages ( message ) { this . callChatWidget ( { method : 'writeToMessages' , params : [ message ] } ) ; } sayAsBot ( text ) { this . callChatWidget ( { method : 'sayAsBot' , params : [ text ] } ) ; } say ( text ) { this . callChatWidget ( { method : 'say' , params : [ text ] } ) ; } whisper ( text ) { this . callChatWidget ( { method : 'whisper' , params : [ text ] } ) ; } }