import { AudioMessageEvent , BotonicMessageEvent , Button , CarouselElement , CarouselMessageEvent , CustomMessageEvent , DocumentMessageEvent , EventTypes , ImageMessageEvent , LocationMessageEvent , PostbackMessageEvent , Reply , TextMessageEvent , VideoMessageEvent , WithButtons , WithReplies , } from '../models' ; import { TEXT_NODE_NAME } from './botonic-output-parser' ; export function parseNumber ( strNumber ) { return parseInt ( strNumber ) ; } export function parseBoolean ( strNumber ) { if ( strNumber === '0' ) return false ; return true ; } export type ParseFunction < Out > = ( args : { toParse : any ; parsed ? : any ; } ) => { toParse : any ; parsed : Partial < Out > ; } ; export const parseMessage = args => { const typingAndDelay = { } ; if ( args . toParse . delay !== undefined ) { typingAndDelay [ 'delay' ] = parseNumber ( args . toParse . delay ) ; } if ( args . toParse . typing !== undefined ) { typingAndDelay [ 'typing' ] = parseNumber ( args . toParse . typing ) ; } return { toParse : args . toParse , parsed : { eventType : EventTypes . MESSAGE , type : args . toParse . type , ack : args . toParse . ack , from : args . toParse . from , channel : args . toParse . channel , idFromChannel : args . toParse . idFromChannel , ... typingAndDelay , } , } ; } ; export const parseButton = ( button ) : Button => { const title = button [ TEXT_NODE_NAME ] ; if ( 'payload' in button ) { return { title , payload : button . payload } ; } if ( 'url' in button ) { const b = { title , url : button . url , target : button . target } ; if ( button . target ) b . target = button . target ; return b ; } if ( 'webview' in button ) { return { title , webview : button . webview , params : button . params , } ; } throw new Error ( 'Invalid parsed Button' ) ; } ; export const parseButtons = args => { const hasButtons = args . toParse ?. button ?. length > 0 ; let buttons = [ ] ; if ( hasButtons ) { buttons = args . toParse . button . map ( button => parseButton ( button ) ) ; } return { toParse : args . toParse , parsed : { ... args . parsed , buttons , } , } ; } ; export const parseReply = ( reply ) : Reply => ( { title : reply [ TEXT_NODE_NAME ] , payload : reply . payload , } ) ; export const parseReplies = args => { const hasReplies = args . toParse ?. reply ?. length > 0 ; let replies = [ ] ; if ( hasReplies ) { replies = args . toParse . reply . map ( reply => parseReply ( reply ) ) ; } return { toParse : args . toParse , parsed : { ... args . parsed , replies , } , } ; } ; export const parseText = args => { return { toParse : args . toParse , parsed : { ... args . parsed , text : args . toParse [ TEXT_NODE_NAME ] , markdown : parseBoolean ( args . toParse . markdown ) , } , } ; } ; export const parsePostback = args => { return { toParse : args . toParse , parsed : { ... args . parsed , payload : args . toParse . payload , } , } ; } ; export const parseMedia = args => { return { toParse : args . toParse , parsed : { ... args . parsed , src : args . toParse . src , } , } ; } ; export const parseLocation = args => { return { toParse : args . toParse , parsed : { ... args . parsed , lat : args . toParse . lat , long : args . toParse . long , } , } ; } ; export const parseElement = ( element ) : CarouselElement => { const e = { pic : element . pic , title : element . title , subtitle : element . desc , } ; const hasButtons = element . button !== undefined ; if ( hasButtons ) e . buttons = element . button . map ( b => parseButton ( b ) ) ; return e ; } ; export const parseCarousel = args => { return { toParse : args . toParse , parsed : { ... args . parsed , elements : args . toParse . element . map ( element => parseElement ( element ) ) , } , } ; } ; export const parseCustom = args => { return { toParse : args . toParse , parsed : { ... args . parsed , json : JSON . parse ( args . toParse . json ) , } , } ; } ;