import omit from 'lodash/omit' ; import pick from 'lodash/pick' ; import * as Messenger from './Messenger' ; import * as MessengerTypes from './MessengerTypes' ; function omitUndefinedFields ( obj = { } ) { return JSON . parse ( JSON . stringify ( obj ) ) ; } function pickBatchOptions < T extends MessengerTypes . BatchRequestOptions > ( options ) { return pick ( options , [ 'name' , 'dependsOn' , 'omitResponseOnSuccess' ] ) ; } function omitBatchOptions < T extends MessengerTypes . BatchRequestOptions > ( options ) { return omit ( options , [ 'name' , 'dependsOn' , 'omitResponseOnSuccess' ] ) ; } export function sendRequest ( body , options ? ) { return { method : 'POST' , relativeUrl : 'me/messages' , body , ... options , } ; } export function sendMessage ( psidOrRecipient , msg , options = { } ) { const recipient = typeof psidOrRecipient === 'string' ? { id : psidOrRecipient , } : psidOrRecipient ; let messagingType = 'UPDATE' ; if ( options . messagingType ) { messagingType = options . messagingType ; } else if ( options . tag ) { messagingType = 'MESSAGE_TAG' ; } const batchRequestOptions = pickBatchOptions ( options ) ; return sendRequest ( { messagingType , recipient , message : Messenger . createMessage ( msg , options ) , ... omitUndefinedFields ( omitBatchOptions ( options ) ) , } , batchRequestOptions ) ; } export function sendText ( psidOrRecipient , text , options ? ) { return sendMessage ( psidOrRecipient , Messenger . createText ( text , options ) , options ) ; } export function sendAttachment ( psidOrRecipient , attachment , options ? ) { return sendMessage ( psidOrRecipient , Messenger . createAttachment ( attachment , options ) , options ) ; } export function sendAudio ( psidOrRecipient , audio , options ? ) { return sendMessage ( psidOrRecipient , Messenger . createAudio ( audio , options ) , options ) ; } export function sendImage ( psidOrRecipient , image , options ? ) { return sendMessage ( psidOrRecipient , Messenger . createImage ( image , options ) , options ) ; } export function sendVideo ( psidOrRecipient , video , options ? ) { return sendMessage ( psidOrRecipient , Messenger . createVideo ( video , options ) , options ) ; } export function sendFile ( psidOrRecipient , file , options ? ) { return sendMessage ( psidOrRecipient , Messenger . createFile ( file , options ) , options ) ; } export function sendTemplate ( psidOrRecipient , payload , options ? ) { return sendMessage ( psidOrRecipient , Messenger . createTemplate ( payload , options ) , options ) ; } export function sendButtonTemplate ( psidOrRecipient , text , buttons , options ? ) { return sendMessage ( psidOrRecipient , Messenger . createButtonTemplate ( text , buttons , options ) , options ) ; } export function sendGenericTemplate ( psidOrRecipient , elements , { imageAspectRatio = 'horizontal' , ... options } = { } ) { return sendMessage ( psidOrRecipient , Messenger . createGenericTemplate ( elements , { ... options , imageAspectRatio , } ) , options ) ; } export function sendReceiptTemplate ( psidOrRecipient , receipt , options ? ) { return sendMessage ( psidOrRecipient , Messenger . createReceiptTemplate ( receipt , options ) , options ) ; } export function sendMediaTemplate ( psidOrRecipient , elements , options ? ) { return sendMessage ( psidOrRecipient , Messenger . createMediaTemplate ( elements , options ) , options ) ; } export function sendAirlineBoardingPassTemplate ( psidOrRecipient , attrs , options ? ) { return sendMessage ( psidOrRecipient , Messenger . createAirlineBoardingPassTemplate ( attrs , options ) , options ) ; } export function sendAirlineCheckinTemplate ( psidOrRecipient , attrs , options ? ) { return sendMessage ( psidOrRecipient , Messenger . createAirlineCheckinTemplate ( attrs , options ) , options ) ; } export function sendAirlineItineraryTemplate ( psidOrRecipient , attrs , options ? ) { return sendMessage ( psidOrRecipient , Messenger . createAirlineItineraryTemplate ( attrs , options ) , options ) ; } export function sendAirlineUpdateTemplate ( psidOrRecipient , attrs , options ? ) { return sendMessage ( psidOrRecipient , Messenger . createAirlineUpdateTemplate ( attrs , options ) , options ) ; } export function sendOneTimeNotifReqTemplate ( psidOrRecipient , attrs , options ? ) { return sendMessage ( psidOrRecipient , Messenger . createOneTimeNotifReqTemplate ( attrs , options ) , options ) ; } export function getUserProfile ( userId , options = { } ) { const batchRequestOptions = pickBatchOptions ( options ) ; const fields = options . fields || [ 'id' , 'name' , 'first_name' , 'last_name' , 'profile_pic' , ] ; return { method : 'GET' , relativeUrl : `${ userId }?fields=${ fields . join ( ',' ) }` . concat ( options . accessToken ? `&access_token=${ options . accessToken }` : '' ) , ... batchRequestOptions , } ; } export function getUserPersistentMenu ( userId , options = { } ) { const batchRequestOptions = pickBatchOptions ( options ) ; return { method : 'GET' , relativeUrl : `/me/custom_user_settings?psid=${ userId }` . concat ( options . accessToken ? `&access_token=${ options . accessToken }` : '' ) , ... batchRequestOptions , } ; } export function setUserPersistentMenu ( userId , menuItems , options = { } ) { const batchRequestOptions = pickBatchOptions ( options ) ; if ( menuItems . some ( ( item ) => 'locale' in item && item . locale === 'default' ) ) { return { method : 'POST' , relativeUrl : `/me/custom_user_settings` . concat ( options . accessToken ? `?access_token=${ options . accessToken }` : '' ) , body : { psid : userId , persistentMenu : menuItems , } , ... batchRequestOptions , } ; } return { method : 'POST' , relativeUrl : `/me/custom_user_settings` . concat ( options . accessToken ? `?access_token=${ options . accessToken }` : '' ) , body : { psid : userId , persistentMenu : [ { locale : 'default' , composerInputDisabled : false , callToActions : menuItems as MessengerTypes . MenuItem [ ] , } , ] , } , ... batchRequestOptions , } ; } export function deleteUserPersistentMenu ( userId , options = { } ) { const batchRequestOptions = pickBatchOptions ( options ) ; return { method : 'DELETE' , relativeUrl : `/me/custom_user_settings?psid=${ userId }&params=[%22persistent_menu%22]` . concat ( options . accessToken ? `&access_token=${ options . accessToken }` : '' ) , ... batchRequestOptions , } ; } export function sendSenderAction ( psidOrRecipient , senderAction , options = { } ) { const recipient = typeof psidOrRecipient === 'string' ? { id : psidOrRecipient , } : psidOrRecipient ; const batchRequestOptions = pickBatchOptions ( options ) ; return sendRequest ( { recipient , senderAction , ... omitUndefinedFields ( omitBatchOptions ( options ) ) , } , batchRequestOptions ) ; } export function typingOn ( idOrRecipient , options ? ) { return sendSenderAction ( idOrRecipient , 'typing_on' , options ) ; } export function typingOff ( idOrRecipient , options ? ) { return sendSenderAction ( idOrRecipient , 'typing_off' , options ) ; } export function markSeen ( idOrRecipient , options ? ) { return sendSenderAction ( idOrRecipient , 'mark_seen' , options ) ; } export function passThreadControl ( recipientId , targetAppId , metadata ? , options = { } ) { const batchRequestOptions = pickBatchOptions ( options ) ; return { method : 'POST' , relativeUrl : 'me/pass_thread_control' , body : { recipient : { id : recipientId } , targetAppId , metadata , ... omitUndefinedFields ( omitBatchOptions ( options ) ) , } , ... batchRequestOptions , } ; } export function passThreadControlToPageInbox ( recipientId , metadata ? , options = { } ) { return passThreadControl ( recipientId , 263902037430900 , metadata , options ) ; } export function takeThreadControl ( recipientId , metadata ? , options = { } ) { const batchRequestOptions = pickBatchOptions ( options ) ; return { method : 'POST' , relativeUrl : 'me/take_thread_control' , body : { recipient : { id : recipientId } , metadata , ... omitUndefinedFields ( omitBatchOptions ( options ) ) , } , ... batchRequestOptions , } ; } export function requestThreadControl ( recipientId , metadata ? , options = { } ) { const batchRequestOptions = pickBatchOptions ( options ) ; return { method : 'POST' , relativeUrl : 'me/request_thread_control' , body : { recipient : { id : recipientId } , metadata , ... omitUndefinedFields ( omitBatchOptions ( options ) ) , } , ... batchRequestOptions , } ; } export function getThreadOwner ( recipientId , options = { } ) { const batchRequestOptions = pickBatchOptions ( options ) ; return { method : 'GET' , relativeUrl : `me/thread_owner?recipient=${ recipientId }` . concat ( options . accessToken ? `&access_token=${ options . accessToken }` : '' ) , responseAccessPath : 'data[0].threadOwner' , ... batchRequestOptions , } ; } export function associateLabel ( userId , labelId , options = { } ) { const batchRequestOptions = pickBatchOptions ( options ) ; return { method : 'POST' , relativeUrl : `${ labelId }/label` , body : { user : userId , ... omitUndefinedFields ( omitBatchOptions ( options ) ) , } , ... batchRequestOptions , } ; } export function dissociateLabel ( userId , labelId , options = { } ) { const batchRequestOptions = pickBatchOptions ( options ) ; return { method : 'DELETE' , relativeUrl : `${ labelId }/label` , body : { user : userId , ... omitUndefinedFields ( omitBatchOptions ( options ) ) , } , ... batchRequestOptions , } ; } export function getAssociatedLabels ( userId , options = { } ) { const batchRequestOptions = pickBatchOptions ( options ) ; return { method : 'GET' , relativeUrl : `${ userId }/custom_labels` . concat ( options . accessToken ? `?access_token=${ options . accessToken }` : '' ) , ... batchRequestOptions , } ; }