import crypto from 'crypto' ; import fs from 'fs' ; import querystring from 'querystring' ; import url from 'url' ; import AxiosError from 'axios-error' ; import FormData from 'form-data' ; import appendQuery from 'append-query' ; import axios , { AxiosInstance , AxiosTransformer , AxiosError as BaseAxiosError , } from 'axios' ; import get from 'lodash/get' ; import invariant from 'ts-invariant' ; import isPlainObject from 'lodash/isPlainObject' ; import omit from 'lodash/omit' ; import warning from 'warning' ; import { OnRequestFunction , camelcaseKeysDeep , createRequestInterceptor , snakecaseKeysDeep , } from 'messaging-api-common' ; import * as Messenger from './Messenger' ; import * as MessengerTypes from './MessengerTypes' ; function extractVersion ( version ) { if ( version . startsWith ( 'v' ) ) { return version . slice ( 1 ) ; } return version ; } function handleError ( err ) { if ( err . response && err . response . data ) { const error = get ( err , 'response.data.error' ) ; if ( error ) { const msg = `Messenger API - ${ error . code } ${ error . type } ${ error . message }` ; throw new AxiosError ( msg , err ) ; } } throw new AxiosError ( err . message , err ) ; } export default class MessengerClient { deprecated static connect ( config ) { warning ( false , '`MessengerClient.connect(...)` is deprecated. Use `new MessengerClient(...)` instead.' ) ; return new MessengerClient ( config ) ; } /**
     * The underlying axios instance.
     */ readonly axios : AxiosInstance ; /**
     * The version of the Facebook Graph API.
     */ readonly version : string ; /**
     * The access token used by the client.
     */ readonly accessToken : string ; /**
     * The app secret used by the client.
     */ readonly appSecret ? : string ; /**
     * The app ID used by the client.
     */ readonly appId ? : string ; /**
     * The callback to be called when receiving requests.
     */ private onRequest ? : OnRequestFunction ; constructor ( config ) { invariant ( typeof config !== 'string' , `MessengerClient: do not allow constructing client with ${ config } string. Use object instead.` ) ; this . accessToken = config . accessToken ; invariant ( ! config . version || typeof config . version === 'string' , 'Type of `version` must be string.' ) ; this . appId = config . appId ; this . appSecret = config . appSecret ; this . version = extractVersion ( config . version || '6.0' ) ; this . onRequest = config . onRequest ; const { origin } = config ; let skipAppSecretProof ; if ( typeof config . skipAppSecretProof === 'boolean' ) { skipAppSecretProof = config . skipAppSecretProof ; } else { skipAppSecretProof = this . appSecret == null ; } this . axios = axios . create ( { baseURL : `${ origin || 'https://graph.facebook.com' }/v${ this . version }/` , headers : { 'Content-Type' : 'application/json' } , transformRequest : [ ( data ) : any => data && isPlainObject ( data ) ? snakecaseKeysDeep ( data ) : data , ... ( axios . defaults . transformRequest as AxiosTransformer [ ] ) , ] , transformResponse : [ ... ( axios . defaults . transformResponse as AxiosTransformer [ ] ) , ( data ) : any => data && isPlainObject ( data ) ? camelcaseKeysDeep ( data ) : data , ] , } ) ; this . axios . interceptors . request . use ( createRequestInterceptor ( { onRequest : this . onRequest } ) ) ; if ( ! skipAppSecretProof ) { invariant ( this . appSecret , 'Must provide appSecret when skipAppSecretProof is false' ) ; const appSecret = this . appSecret as string ; this . axios . interceptors . request . use ( ( requestConfig ) => { const isBatch = requestConfig . url === '/' && Array . isArray ( requestConfig . data . batch ) ; if ( isBatch ) { requestConfig . data . batch = requestConfig . data . batch . map ( ( item ) => { const urlParts = url . parse ( item . relativeUrl , true ) ; let accessToken = get ( urlParts , 'query.access_token' ) ; if ( ! accessToken && item . body ) { const entries = decodeURIComponent ( item . body ) . split ( '&' ) . map ( ( pair ) => pair . split ( '=' ) ) ; const accessTokenEntry = entries . find ( ( [ key ] ) => key === 'access_token' ) ; if ( accessTokenEntry ) { accessToken = accessTokenEntry [ 1 ] ; } } if ( accessToken ) { const appSecretProof = crypto . createHmac ( 'sha256' , appSecret ) . update ( accessToken , 'utf8' ) . digest ( 'hex' ) ; return { ... item , relativeUrl : appendQuery ( item . relativeUrl , { appsecret_proof : appSecretProof , } ) , } ; } return item ; } ) ; } const urlParts = url . parse ( requestConfig . url || '' , true ) ; const accessToken = get ( urlParts , 'query.access_token' , this . accessToken ) ; const appSecretProof = crypto . createHmac ( 'sha256' , appSecret ) . update ( accessToken , 'utf8' ) . digest ( 'hex' ) ; requestConfig . url = appendQuery ( requestConfig . url || '' , { appsecret_proof : appSecretProof , } ) ; return requestConfig ; } ) ; } } returns see https example getPageInfo ( { fields , } = { } ) { return this . axios . get ( '/me' , { params : { access_token : this . accessToken , fields : fields ? fields . join ( ',' ) : undefined , } , } ) . then ( ( res ) => res . data , handleError ) ; } returns see https example debugToken ( ) { invariant ( this . appId , 'App ID is required to debug token' ) ; invariant ( this . appSecret , 'App Secret is required to debug token' ) ; const accessToken = `${ this . appId }|${ this . appSecret }` ; return this . axios . get ( `/debug_token` , { params : { input_token : this . accessToken , access_token : accessToken , } , } ) . then ( ( res ) => res . data . data , handleError ) ; } param subscription param subscription . accessToken param subscription . callbackUrl param subscription . verifyToken param subscription . fields param subscription . object param subscription . includeValues returns see https example createSubscription ( { object = 'page' , callbackUrl , fields = [ 'messages' , 'messaging_postbacks' , 'messaging_optins' , 'messaging_referrals' , 'messaging_handovers' , 'messaging_policy_enforcement' , ] , includeValues , verifyToken , accessToken : appAccessToken , } ) { const { appId } = this ; invariant ( appId , 'App ID is required to create subscription' ) ; invariant ( this . appSecret || appAccessToken , 'App Secret or App Token is required to create subscription' ) ; const accessToken = appAccessToken || `${ appId }|${ this . appSecret }` ; return this . axios . post ( `/${ appId }/subscriptions?access_token=${ accessToken }` , { object , callbackUrl , fields : fields . join ( ',' ) , includeValues , verifyToken , } ) . then ( ( res ) => res . data , handleError ) ; } param options param options . accessToken returns see https example getSubscriptions ( { accessToken : appAccessToken , } = { } ) { const { appId } = this ; invariant ( appId , 'App ID is required to get subscriptions' ) ; invariant ( this . appSecret || appAccessToken , 'App Secret or App Token is required to get subscriptions' ) ; const accessToken = appAccessToken || `${ appId }|${ this . appSecret }` ; return this . axios . get ( `/${ appId }/subscriptions?access_token=${ accessToken }` ) . then ( ( res ) => res . data . data , handleError ) ; } param options param options . accessToken returns see https example getPageSubscription ( { accessToken : appAccessToken , } = { } ) { const { appId } = this ; invariant ( appId , 'App ID is required to get subscription' ) ; invariant ( this . appSecret || appAccessToken , 'App Secret or App Token is required to get subscription' ) ; const accessToken = appAccessToken || `${ appId }|${ this . appSecret }` ; return this . getSubscriptions ( { accessToken , } ) . then ( ( subscriptions ) => subscriptions . filter ( ( subscription ) => subscription . object === 'page' ) [ 0 ] || null ) ; } returns see https example getMessagingFeatureReview ( ) { return this . axios . get < { data : MessengerTypes . MessagingFeatureReview [ ] ; } > ( `/me/messaging_feature_review?access_token=${ this . accessToken }` ) . then ( ( res ) => res . data . data , handleError ) ; } param userId param options param options . fields returns see https example getUserProfile ( userId , { fields = [ 'id' , 'name' , 'first_name' , 'last_name' , 'profile_pic' ] , } = { } ) { return this . axios . get < MessengerTypes . User > ( `/${ userId }?fields=${ fields . join ( ',' ) }&access_token=${ this . accessToken }` ) . then ( ( res ) => res . data , handleError ) ; } param fields returns see https example getMessengerProfile ( fields ) { return this . axios . get < { data : MessengerTypes . MessengerProfile [ ] ; } > ( `/me/messenger_profile?fields=${ fields . join ( ',' ) }&access_token=${ this . accessToken }` ) . then ( ( res ) => res . data . data , handleError ) ; } param profile returns see https example setMessengerProfile ( profile ) { return this . axios . post < MessengerTypes . MutationSuccessResponse > ( `/me/messenger_profile?access_token=${ this . accessToken }` , profile ) . then ( ( res ) => res . data , handleError ) ; } param fields returns see https example deleteMessengerProfile ( fields ) { return this . axios . delete < MessengerTypes . MutationSuccessResponse > ( `/me/messenger_profile?access_token=${ this . accessToken }` , { data : { fields , } , } ) . then ( ( res ) => res . data , handleError ) ; } returns see https example getGetStarted ( ) { return this . getMessengerProfile ( [ 'get_started' ] ) . then ( ( res ) => res [ 0 ] ? ( res [ 0 ] . getStarted as { payload : string ; } ) : null ) ; } param payload returns see https example setGetStarted ( payload ) { return this . setMessengerProfile ( { getStarted : { payload , } , } ) ; } returns see https example deleteGetStarted ( ) { return this . deleteMessengerProfile ( [ 'get_started' ] ) ; } returns see https example getPersistentMenu ( ) { return this . getMessengerProfile ( [ 'persistent_menu' ] ) . then ( ( res ) => res [ 0 ] ? ( res [ 0 ] . persistentMenu as MessengerTypes . PersistentMenu ) : null ) ; } param menu see https example note setPersistentMenu ( menuItems , { composerInputDisabled = false , } = { } ) { if ( menuItems . some ( ( item ) => 'locale' in item && item . locale === 'default' ) ) { return this . setMessengerProfile ( { persistentMenu : menuItems as MessengerTypes . PersistentMenu , } ) ; } return this . setMessengerProfile ( { persistentMenu : [ { locale : 'default' , composerInputDisabled , callToActions : menuItems as MessengerTypes . MenuItem [ ] , } , ] , } ) ; } see https example deletePersistentMenu ( ) { return this . deleteMessengerProfile ( [ 'persistent_menu' ] ) ; } see https example getUserPersistentMenu ( userId ) { return this . axios . get ( `/me/custom_user_settings?psid=${ userId }&access_token=${ this . accessToken }` ) . then ( ( res ) => res . data . data [ 0 ] ? ( res . data . data [ 0 ] . userLevelPersistentMenu as MessengerTypes . PersistentMenu ) : null , handleError ) ; } example setUserPersistentMenu ( userId , menuItems , { composerInputDisabled = false , } = { } ) { if ( menuItems . some ( ( item ) => 'locale' in item && item . locale === 'default' ) ) { return this . axios . post < MessengerTypes . MutationSuccessResponse > ( `/me/custom_user_settings?access_token=${ this . accessToken }` , { psid : userId , persistentMenu : menuItems as MessengerTypes . PersistentMenu , } ) . then ( ( res ) => res . data , handleError ) ; } return this . axios . post ( `/me/custom_user_settings?access_token=${ this . accessToken }` , { psid : userId , persistentMenu : [ { locale : 'default' , composerInputDisabled , callToActions : menuItems as MessengerTypes . MenuItem [ ] , } , ] , } ) . then ( ( res ) => res . data , handleError ) ; } example deleteUserPersistentMenu ( userId ) { return this . axios . delete ( `/me/custom_user_settings?psid=${ userId }&params=[%22persistent_menu%22]&access_token=${ this . accessToken }` ) . then ( ( res ) => res . data , handleError ) ; } returns see https example getGreeting ( ) { return this . getMessengerProfile ( [ 'greeting' ] ) . then ( ( res ) => res [ 0 ] ? ( res [ 0 ] . greeting as MessengerTypes . GreetingConfig [ ] ) : null ) ; } param greeting returns see https example setGreeting ( greeting ) { if ( typeof greeting === 'string' ) { return this . setMessengerProfile ( { greeting : [ { locale : 'default' , text : greeting , } , ] , } ) ; } return this . setMessengerProfile ( { greeting , } ) ; } returns see https example deleteGreeting ( ) { return this . deleteMessengerProfile ( [ 'greeting' ] ) ; } returns see https example getIceBreakers ( ) { return this . getMessengerProfile ( [ 'ice_breakers' ] ) . then ( ( res ) => res [ 0 ] ? ( res [ 0 ] . iceBreakers as MessengerTypes . IceBreaker [ ] ) : null ) ; } param iceBreakers returns example setIceBreakers ( iceBreakers ) { return this . setMessengerProfile ( { iceBreakers , } ) ; } returns example deleteIceBreakers ( ) { return this . deleteMessengerProfile ( [ 'ice_breakers' ] ) ; } returns see https example getWhitelistedDomains ( ) { return this . getMessengerProfile ( [ 'whitelisted_domains' ] ) . then ( ( res ) => res [ 0 ] ? ( res [ 0 ] . whitelistedDomains as string [ ] ) : null ) ; } param whitelistedDomains returns see https example setWhitelistedDomains ( whitelistedDomains ) { return this . setMessengerProfile ( { whitelistedDomains , } ) ; } returns see https example deleteWhitelistedDomains ( ) { return this . deleteMessengerProfile ( [ 'whitelisted_domains' ] ) ; } returns see https example getAccountLinkingURL ( ) { return this . getMessengerProfile ( [ 'account_linking_url' ] ) . then ( ( res ) => res [ 0 ] ? ( res [ 0 ] . accountLinkingUrl as string ) : null ) ; } param url returns see https example setAccountLinkingURL ( accountLinkingUrl ) { return this . setMessengerProfile ( { accountLinkingUrl , } ) ; } returns see https example deleteAccountLinkingURL ( ) { return this . deleteMessengerProfile ( [ 'account_linking_url' ] ) ; } param body returns see https example sendRawBody ( body ) { return this . axios . post < MessengerTypes . SendMessageSuccessResponse > ( `/me/messages?access_token=${ this . accessToken }` , body ) . then ( ( res ) => res . data , handleError ) ; } param psidOrRecipient param message param options returns example sendMessage ( psidOrRecipient , message , 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' ; } return this . sendRawBody ( { messagingType , recipient , message : Messenger . createMessage ( message , options ) , ... omit ( options , 'quickReplies' ) , } ) ; } param psidOrRecipient param formdata param options returns example sendMessageFormData ( psidOrRecipient , formdata , 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' ; } formdata . append ( 'messaging_type' , messagingType ) ; formdata . append ( 'recipient' , JSON . stringify ( snakecaseKeysDeep ( recipient ) ) ) ; return this . axios . post < MessengerTypes . SendMessageSuccessResponse > ( `/me/messages?access_token=${ this . accessToken }` , formdata , { headers : formdata . getHeaders ( ) , } ) . then ( ( res ) => res . data , handleError ) ; } param psidOrRecipient param attachment param options returns see https example sendAttachment ( psidOrRecipient , attachment , options ? ) { return this . sendMessage ( psidOrRecipient , Messenger . createAttachment ( attachment , options ) , options ) ; } param psidOrRecipient param text param options returns see https example sendText ( psidOrRecipient , text , options ? ) { return this . sendMessage ( psidOrRecipient , Messenger . createText ( text , options ) , options ) ; } param psidOrRecipient param audio param options param options . filename returns see https example sendAudio ( psidOrRecipient , audio , options ? ) { if ( Buffer . isBuffer ( audio ) || audio instanceof fs . ReadStream ) { const message = Messenger . createAudioFormData ( audio , options ) ; return this . sendMessageFormData ( psidOrRecipient , message , options ) ; } const message = Messenger . createAudio ( audio , options ) ; return this . sendMessage ( psidOrRecipient , message , options ) ; } param psidOrRecipient param image param options param options . filename returns see https example sendImage ( psidOrRecipient , image , options ? ) { if ( Buffer . isBuffer ( image ) || image instanceof fs . ReadStream ) { const message = Messenger . createImageFormData ( image , options ) ; return this . sendMessageFormData ( psidOrRecipient , message , options ) ; } const message = Messenger . createImage ( image , options ) ; return this . sendMessage ( psidOrRecipient , message , options ) ; } param psidOrRecipient param video param options param options . filename returns see https example sendVideo ( psidOrRecipient , video , options ? ) { if ( Buffer . isBuffer ( video ) || video instanceof fs . ReadStream ) { const message = Messenger . createVideoFormData ( video , options ) ; return this . sendMessageFormData ( psidOrRecipient , message , options ) ; } const message = Messenger . createVideo ( video , options ) ; return this . sendMessage ( psidOrRecipient , message , options ) ; } param psidOrRecipient param file param options param options . filename returns see https example sendFile ( psidOrRecipient , file , options ? ) { if ( Buffer . isBuffer ( file ) || file instanceof fs . ReadStream ) { const message = Messenger . createFileFormData ( file , options ) ; return this . sendMessageFormData ( psidOrRecipient , message , options ) ; } const message = Messenger . createFile ( file , options ) ; return this . sendMessage ( psidOrRecipient , message , options ) ; } param psidOrRecipient param payload param options returns see https example sendTemplate ( psidOrRecipient , payload , options ? ) { return this . sendMessage ( psidOrRecipient , Messenger . createTemplate ( payload , options ) , options ) ; } param psidOrRecipient param title param buttons param options returns see https example sendButtonTemplate ( psidOrRecipient , text , buttons , options ? ) { return this . sendMessage ( psidOrRecipient , Messenger . createButtonTemplate ( text , buttons , options ) , options ) ; } param psidOrRecipient param elements param options returns see https example sendGenericTemplate ( psidOrRecipient , elements , options = { } ) { return this . sendMessage ( psidOrRecipient , Messenger . createGenericTemplate ( elements , options ) , omit ( options , [ 'imageAspectRatio' ] ) ) ; } param psidOrRecipient param receipt param options returns see https example sendReceiptTemplate ( psidOrRecipient , receipt , options ? ) { return this . sendMessage ( psidOrRecipient , Messenger . createReceiptTemplate ( receipt , options ) , options ) ; } param psidOrRecipient param elements param options returns see https example sendMediaTemplate ( psidOrRecipient , elements , options ? ) { return this . sendMessage ( psidOrRecipient , Messenger . createMediaTemplate ( elements , options ) , options ) ; } param psidOrRecipient param attrs param options returns see https example sendAirlineBoardingPassTemplate ( psidOrRecipient , attrs , options ? ) { return this . sendMessage ( psidOrRecipient , Messenger . createAirlineBoardingPassTemplate ( attrs , options ) , options ) ; } param psidOrRecipient param attrs param options returns see https example sendAirlineCheckinTemplate ( psidOrRecipient , attrs , options ? ) { return this . sendMessage ( psidOrRecipient , Messenger . createAirlineCheckinTemplate ( attrs , options ) , options ) ; } param psidOrRecipient param attrs param options returns see https example sendAirlineItineraryTemplate ( psidOrRecipient , attrs , options ? ) { return this . sendMessage ( psidOrRecipient , Messenger . createAirlineItineraryTemplate ( attrs , options ) , options ) ; } param psidOrRecipient param attrs param options returns see https example sendAirlineUpdateTemplate ( psidOrRecipient , attrs , options ? ) { return this . sendMessage ( psidOrRecipient , Messenger . createAirlineUpdateTemplate ( attrs , options ) , options ) ; } param psidOrRecipient param options returns see https example sendOneTimeNotifReqTemplate ( psidOrRecipient , attrs , options ? ) { return this . sendMessage ( psidOrRecipient , Messenger . createOneTimeNotifReqTemplate ( attrs , options ) , options ) ; } param psidOrRecipient param senderAction param options param options . personaId returns see https example sendSenderAction ( psidOrRecipient , senderAction , options ? ) { const recipient = typeof psidOrRecipient === 'string' ? { id : psidOrRecipient , } : psidOrRecipient ; return this . sendRawBody ( { recipient , senderAction , ... options , } ) ; } param psidOrRecipient returns see https example markSeen ( psidOrRecipient ) { return this . sendSenderAction ( psidOrRecipient , 'mark_seen' ) ; } param psidOrRecipient param options param options . personaId returns see https example typingOn ( psidOrRecipient , options ? ) { return this . sendSenderAction ( psidOrRecipient , 'typing_on' , options ) ; } param psidOrRecipient param options param options . personaId returns see https example typingOff ( psidOrRecipient , options ? ) { return this . sendSenderAction ( psidOrRecipient , 'typing_off' , options ) ; } param requests returns see https example sendBatch ( batch , { includeHeaders = true } = { } ) { invariant ( batch . length <= 50 , 'limit the number of requests which can be in a batch to 50' ) ; const responseAccessPaths = batch . map ( ( item ) => item . responseAccessPath ) ; const bodyEncodedbatch = batch . map ( ( item ) => omit ( item , 'responseAccessPath' ) ) . map ( ( item ) => { if ( item . body ) { const body = snakecaseKeysDeep ( item . body ) as Record < string , any > ; return { ... item , body : Object . keys ( body ) . map ( ( key ) => { const val = body [ key ] ; return `${ encodeURIComponent ( key ) }=${ encodeURIComponent ( typeof val === 'object' ? JSON . stringify ( val ) : val ) }` ; } ) . join ( '&' ) , } ; } return item ; } ) ; return this . axios . post ( '/' , { accessToken : this . accessToken , includeHeaders , batch : bodyEncodedbatch , } ) . then ( ( res ) => res . data . map ( ( item , index ) => { const responseAccessPath = responseAccessPaths [ index ] ; const datum = camelcaseKeysDeep ( item ) as Record < string , any > ; if ( datum . body ) { const parsedBody = camelcaseKeysDeep ( JSON . parse ( datum . body ) ) ; return { ... datum , body : responseAccessPath ? get ( parsedBody , responseAccessPath ) : parsedBody , } ; } return datum ; } ) , handleError ) ; } see https param name returns see https example createLabel ( name ) { return this . axios . post < { id : string ; } > ( `/me/custom_labels?access_token=${ this . accessToken }` , { name , } ) . then ( ( res ) => res . data , handleError ) ; } param userId param labelId returns see https example associateLabel ( userId , labelId ) { return this . axios . post < { success : true ; } > ( `/${ labelId }/label?access_token=${ this . accessToken }` , { user : userId , } ) . then ( ( res ) => res . data , handleError ) ; } param userId param labelId returns see https example dissociateLabel ( userId , labelId ) { return this . axios . delete < { success : true ; } > ( `/${ labelId }/label?access_token=${ this . accessToken }` , { data : { user : userId } , } ) . then ( ( res ) => res . data , handleError ) ; } param userId returns see https example getAssociatedLabels ( userId , options = { } ) { const fields = options . fields ? options . fields . join ( ',' ) : 'name' ; return this . axios . get < { data : { name : string ; id : string ; } [ ] ; paging : { cursors : { before : string ; after : string ; } ; } ; } > ( `/${ userId }/custom_labels?fields=${ fields }&access_token=${ this . accessToken }` ) . then ( ( res ) => res . data , handleError ) ; } param labelId param options param options . fields see https example getLabelDetails ( labelId , options = { } ) { const fields = options . fields ? options . fields . join ( ',' ) : 'name' ; return this . axios . get < { name : string ; id : string ; } > ( `/${ labelId }?fields=${ fields }&access_token=${ this . accessToken }` ) . then ( ( res ) => res . data , handleError ) ; } returns see https example getLabelList ( options = { } ) { const fields = options . fields ? options . fields . join ( ',' ) : 'name' ; return this . axios . get < { data : { name : string ; id : string ; } [ ] ; paging : { cursors : { before : string ; after : string ; } ; } ; } > ( `/me/custom_labels?fields=${ fields }&access_token=${ this . accessToken }` ) . then ( ( res ) => res . data , handleError ) ; } param labelId returns see https example deleteLabel ( labelId ) { return this . axios . delete < { success : true ; } > ( `/${ labelId }?access_token=${ this . accessToken }` ) . then ( ( res ) => res . data , handleError ) ; } param type param attachment param options param options . isReusable param options . filename returns see https example uploadAttachment ( type , attachment , options = { } ) { const args = [ ] ; const isReusable = options . isReusable || false ; if ( typeof attachment === 'string' ) { args . push ( { message : { attachment : { type , payload : { url : attachment , isReusable , } , } , } , } ) ; } else { const form = new FormData ( ) ; form . append ( 'message' , JSON . stringify ( { attachment : { type , payload : { is_reusable : isReusable , } , } , } ) ) ; form . append ( 'filedata' , attachment , omit ( options , [ 'is_reusable' ] ) ) ; args . push ( form , { headers : form . getHeaders ( ) , maxContentLength : Infinity , } ) ; } return this . axios . post < { attachmentId : string ; } > ( `/me/message_attachments?access_token=${ this . accessToken }` , ... args ) . then ( ( res ) => res . data , handleError ) ; } param audio param options param options . isReusable param options . filename returns example uploadAudio ( attachment , options ? ) { return this . uploadAttachment ( 'audio' , attachment , options ) ; } param image param options param options . isReusable param options . filename returns example uploadImage ( attachment , options ? ) { return this . uploadAttachment ( 'image' , attachment , options ) ; } param video param options param options . isReusable param options . filename returns example uploadVideo ( attachment , options ? ) { return this . uploadAttachment ( 'video' , attachment , options ) ; } param file param options param options . isReusable param options . filename returns example uploadFile ( attachment , options ? ) { return this . uploadAttachment ( 'file' , attachment , options ) ; } see https param recipientId param targetAppId param metadata returns see https example passThreadControl ( recipientId , targetAppId , metadata ? ) { return this . axios . post < { success : true ; } > ( `/me/pass_thread_control?access_token=${ this . accessToken }` , { recipient : { id : recipientId } , targetAppId , metadata , } ) . then ( ( res ) => res . data , handleError ) ; } param recipientId param metadata returns see https example passThreadControlToPageInbox ( recipientId , metadata ? ) { return this . passThreadControl ( recipientId , 263902037430900 , metadata ) ; } param recipientId param metadata returns see https example takeThreadControl ( recipientId , metadata ? ) { return this . axios . post < { success : true ; } > ( `/me/take_thread_control?access_token=${ this . accessToken }` , { recipient : { id : recipientId } , metadata , } ) . then ( ( res ) => res . data , handleError ) ; } param recipientId param metadata returns see https example requestThreadControl ( recipientId , metadata ? ) { return this . axios . post < { success : true ; } > ( `/me/request_thread_control?access_token=${ this . accessToken }` , { recipient : { id : recipientId } , metadata , } ) . then ( ( res ) => res . data , handleError ) ; } returns see https example getSecondaryReceivers ( ) { return this . axios . get < { data : { id : string ; name : string ; } [ ] ; } > ( `/me/secondary_receivers?fields=id,name&access_token=${ this . accessToken }` ) . then ( ( res ) => res . data . data , handleError ) ; } param recipientId returns see https example getThreadOwner ( recipientId ) { return this . axios . get < { data : [ { threadOwner : { appId : string ; } ; } ] ; } > ( `/me/thread_owner?recipient=${ recipientId }&access_token=${ this . accessToken }` ) . then ( ( res ) => res . data . data [ 0 ] . threadOwner , handleError ) ; } param metrics param options param options . since param options . until see https example getInsights ( metrics , options = { } ) { return this . axios . get ( `/me/insights/?${ querystring . stringify ( { metric : metrics . join ( ',' ) , access_token : this . accessToken , ... options , } ) }` ) . then ( ( res ) => res . data . data , handleError ) ; } param options param options . since param options . until see https example getBlockedConversations ( options ) { return this . getInsights ( [ 'page_messages_blocked_conversations_unique' ] , options ) . then ( ( result ) => result [ 0 ] ) ; } param options param options . since param options . until see https example getReportedConversations ( options ) { return this . getInsights ( [ 'page_messages_reported_conversations_unique' ] , options ) . then ( ( result ) => result [ 0 ] ) ; } param options param options . since param options . until see https example getTotalMessagingConnections ( options ) { return this . getInsights ( [ 'page_messages_total_messaging_connections' ] , options ) . then ( ( result ) => result [ 0 ] ) ; } param options param options . since param options . until see https example getNewConversations ( options ) { return this . getInsights ( [ 'page_messages_new_conversations_unique' ] , options ) . then ( ( result ) => result [ 0 ] ) ; } param config param config . nlpEnabled param config . model param config . customToken param config . verbose param config . nest returns see https example setNLPConfigs ( config = { } ) { return this . axios . post ( `/me/nlp_configs?${ querystring . stringify ( snakecaseKeysDeep ( { ... config , accessToken : this . accessToken , } ) as Record < string , any > ) }` ) . then ( ( res ) => res . data , handleError ) ; } returns see https example enableNLP ( ) { return this . setNLPConfigs ( { nlpEnabled : true } ) ; } returns see https example disableNLP ( ) { return this . setNLPConfigs ( { nlpEnabled : false } ) ; } param activity param activity . appId param activity . pageId param activity . pageScopedUserId param activity . events returns see https example logCustomEvents ( { appId , pageId , pageScopedUserId , events , } ) { return this . axios . post < any > ( `/${ appId }/activities` , { event : 'CUSTOM_APP_EVENTS' , customEvents : JSON . stringify ( events ) , advertiserTrackingEnabled : 0 , applicationTrackingEnabled : 0 , extinfo : JSON . stringify ( [ 'mb1' ] ) , pageId , pageScopedUserId , } ) . then ( ( res ) => res . data , handleError ) ; } see https example getUserField ( { field , userId , appSecret , app , page , } ) { const appsecretProof = crypto . createHmac ( 'sha256' , appSecret ) . update ( this . accessToken ) . digest ( 'hex' ) ; const appQueryString = app ? `&app=${ app }` : '' ; const pageQueryString = page ? `&page=${ page }` : '' ; return this . axios . get ( `/${ userId }/${ field }?access_token=${ this . accessToken }&appsecret_proof=${ appsecretProof }${ appQueryString }${ pageQueryString }` ) . then ( ( res ) => res . data , handleError ) ; } param params param params . userId param params . appSecret param params . app param params . page returns see https example getIdsForApps ( { userId , appSecret , app , page , } ) { return this . getUserField ( { field : 'ids_for_apps' , userId , appSecret , app , page , } ) ; } param params param params . userId param params . appSecret param params . app param params . page returns see https example getIdsForPages ( { userId , appSecret , app , page , } ) { return this . getUserField ( { field : 'ids_for_pages' , userId , appSecret , app , page , } ) ; } see https param persona param persona . name param persona . profilePictureUrl returns see https example createPersona ( persona ) { return this . axios . post < { id : string ; } > ( `/me/personas?access_token=${ this . accessToken }` , persona ) . then ( ( res ) => res . data , handleError ) ; } param personaId returns see https example getPersona ( personaId ) { return this . axios . get < { id : string ; name : string ; profilePictureUrl : string ; } > ( `/${ personaId }?access_token=${ this . accessToken }` ) . then ( ( res ) => res . data , handleError ) ; } param cursor returns see https example getPersonas ( cursor ? ) { return this . axios . get < { data : { id : string ; name : string ; profilePictureUrl : string ; } [ ] ; paging : { cursors : { before : string ; after : string ; } ; } ; } > ( `/me/personas?access_token=${ this . accessToken }${ cursor ? `&after=${ cursor }` : '' }` ) . then ( ( res ) => res . data , handleError ) ; } returns example async getAllPersonas ( ) { let allPersonas = [ ] ; let cursor ; do { const { data , paging , } = await this . getPersonas ( cursor ) ; allPersonas = allPersonas . concat ( data ) ; cursor = paging ? paging . cursors . after : null ; } while ( cursor ) ; return allPersonas ; } param personaId returns see https example deletePersona ( personaId ) { return this . axios . delete < { success : true ; } > ( `/${ personaId }?access_token=${ this . accessToken }` ) . then ( ( res ) => res . data , handleError ) ; } }