import axios from 'axios' ; import { IAttachment , IMessage } from './../typings' ; class BotMan { userId : string ; chatServer : string ; setUserId ( userId ) { this . userId = userId ; } setChatServer ( chatServer ) { this . chatServer = chatServer ; } callAPI = ( text , interactive = false , attachment = null , perMessageCallback , callback ) => { let data = new FormData ( ) ; const postData = { driver : 'web' , userId : this . userId , message : text , attachment : attachment as Blob , interactive : interactive ? '1' : '0' } ; Object . keys ( postData ) . forEach ( key => data . append ( key , postData [ key ] ) ) ; axios . post ( this . chatServer , data ) . then ( response => { const messages = response . data . messages || [ ] ; if ( perMessageCallback ) { messages . forEach ( ( msg ) => { perMessageCallback ( msg ) ; } ) ; } if ( callback ) { callback ( response . data ) ; } } ) ; } ; } export let botman = new BotMan ( ) ;