import FormData from 'form-data' ; import omit from 'lodash/omit' ; import { camelcaseKeysDeep , snakecaseKeysDeep } from 'messaging-api-common' ; import * as MessengerTypes from './MessengerTypes' ; export function createMessage ( payload , options = { } ) { const message = { ... payload , } ; const quickReplies = options . quickReplies || options . quick_replies ; if ( quickReplies && Array . isArray ( quickReplies ) && quickReplies . length >= 1 ) { message . quickReplies = quickReplies ; } return camelcaseKeysDeep ( message ) ; } export function createText ( text , options ? ) { return createMessage ( { text } , options ) ; } export function createMessageFormData ( payload , filedata , options = { } ) { const message = { ... payload , } ; const quickReplies = options . quickReplies || options . quick_replies ; if ( quickReplies && Array . isArray ( quickReplies ) && quickReplies . length >= 1 ) { message . quickReplies = quickReplies ; } const formdata = new FormData ( ) ; formdata . append ( 'message' , JSON . stringify ( snakecaseKeysDeep ( message ) ) ) ; formdata . append ( 'filedata' , filedata , omit ( options , [ 'quickReplies' ] ) ) ; return formdata ; } export function createAttachment ( attachment , options ? ) { return createMessage ( { attachment , } , options ) ; } export function createAttachmentFormData ( attachment , filedata , options ? ) { return createMessageFormData ( { attachment , } , filedata , options ) ; } export function createAudio ( audio , options ? ) { if ( typeof audio === 'string' ) { const attachment = { type : 'audio' , payload : { url : audio , } , } ; return createAttachment ( attachment , options ) ; } const attachment = { type : 'audio' , payload : audio as MessengerTypes . MediaAttachmentPayload , } ; return createAttachment ( attachment , options ) ; } export function createAudioFormData ( audio , options ? ) { const attachment = { type : 'audio' , payload : { } , } ; return createAttachmentFormData ( attachment , audio , options ) ; } export function createImage ( image , options ? ) { if ( typeof image === 'string' ) { const attachment = { type : 'image' , payload : { url : image , } , } ; return createAttachment ( attachment , options ) ; } const attachment = { type : 'image' , payload : image as MessengerTypes . MediaAttachmentPayload , } ; return createAttachment ( attachment , options ) ; } export function createImageFormData ( image , options ? ) { const attachment = { type : 'image' , payload : { } , } ; return createAttachmentFormData ( attachment , image , options ) ; } export function createVideo ( video , options ? ) { if ( typeof video === 'string' ) { const attachment = { type : 'video' , payload : { url : video , } , } ; return createAttachment ( attachment , options ) ; } const attachment = { type : 'video' , payload : video as MessengerTypes . MediaAttachmentPayload , } ; return createAttachment ( attachment , options ) ; } export function createVideoFormData ( video , options ? ) { const attachment = { type : 'video' , payload : { } , } ; return createAttachmentFormData ( attachment , video , options ) ; } export function createFile ( file , options ? ) { if ( typeof file === 'string' ) { const attachment = { type : 'file' , payload : { url : file , } , } ; return createAttachment ( attachment , options ) ; } const attachment = { type : 'file' , payload : file as MessengerTypes . MediaAttachmentPayload , } ; return createAttachment ( attachment , options ) ; } export function createFileFormData ( file , options ? ) { const attachment = { type : 'file' , payload : { } , } ; return createAttachmentFormData ( attachment , file , options ) ; } export function createTemplate ( payload , options ? ) { return createAttachment ( { type : 'template' , payload , } , options ) ; } export function createButtonTemplate ( text , buttons , options ? ) { return createTemplate ( { templateType : 'button' , text , buttons , } , options ) ; } export function createGenericTemplate ( elements , options = { } ) { return createTemplate ( { templateType : 'generic' , elements , ... ( options . imageAspectRatio ? { imageAspectRatio : options . imageAspectRatio } : { } ) , } , options ) ; } export function createMediaTemplate ( elements , options ? ) { return createTemplate ( { templateType : 'media' , elements , } , options ) ; } export function createReceiptTemplate ( receipt , options ? ) { return createTemplate ( { templateType : 'receipt' , ... receipt , } , options ) ; } export function createAirlineBoardingPassTemplate ( attrs , options ? ) { return createTemplate ( { templateType : 'airline_boardingpass' , ... attrs , } , options ) ; } export function createAirlineCheckinTemplate ( attrs , options ? ) { return createTemplate ( { templateType : 'airline_checkin' , ... attrs , } , options ) ; } export function createAirlineItineraryTemplate ( attrs , options ? ) { return createTemplate ( { templateType : 'airline_itinerary' , ... attrs , } , options ) ; } export function createAirlineUpdateTemplate ( attrs , options ? ) { return createTemplate ( { templateType : 'airline_update' , ... attrs , } , options ) ; } export function createOneTimeNotifReqTemplate ( attrs , options ? ) { return createTemplate ( { templateType : 'one_time_notif_req' , ... attrs , } , options ) ; }