{"id":19024706,"url":"https://github.com/amuramoto/messenger-node","last_synced_at":"2025-04-23T11:58:20.110Z","repository":{"id":45883830,"uuid":"114296996","full_name":"amuramoto/messenger-node","owner":"amuramoto","description":"A Node.js SDK for using the Messenger Platform","archived":false,"fork":false,"pushed_at":"2020-03-23T21:38:12.000Z","size":5960,"stargazers_count":47,"open_issues_count":0,"forks_count":13,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-23T11:58:01.674Z","etag":null,"topics":["chatbot","facebook","facebook-messenger-bot","messenger-api","messenger-chatbot","messenger-platform","nodejs","sdk"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/amuramoto.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-12-14T21:17:12.000Z","updated_at":"2025-04-22T17:22:50.000Z","dependencies_parsed_at":"2022-09-05T05:20:24.090Z","dependency_job_id":null,"html_url":"https://github.com/amuramoto/messenger-node","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amuramoto%2Fmessenger-node","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amuramoto%2Fmessenger-node/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amuramoto%2Fmessenger-node/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amuramoto%2Fmessenger-node/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/amuramoto","download_url":"https://codeload.github.com/amuramoto/messenger-node/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250430589,"owners_count":21429323,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chatbot","facebook","facebook-messenger-bot","messenger-api","messenger-chatbot","messenger-platform","nodejs","sdk"],"created_at":"2024-11-08T20:38:06.671Z","updated_at":"2025-04-23T11:58:20.091Z","avatar_url":"https://github.com/amuramoto.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Messenger Platform Node.js SDK\n\n[![Build Status](https://travis-ci.org/amuramoto/messenger-node.svg?branch=master)](https://travis-ci.org/amuramoto/messenger-node) * Build is failing right now because one of my test accounts got permissions revoked or something, but the SDK still works as expected\n\nThe `messenger-node` module is a server-side SDK for building bots on Facebook's [Messenger Platform](https://developers.facebook.com/docs/messenger-platform/). The SDK includes two base classes:\n\n- [Webhook](#webhook): Creates an [express.js](expressjs.com) web server for receiving and processing [webhook events](https://developers.facebook.com/docs/messenger-platform/webhook) sent by the Messenger Platform.\n- [Client](#creating-a-client-instance): Creates a client object that simplifies sending requests to the Messenger Platform's various APIs.\n\n## Installing the SDK\n\nThe SDK is available via [NPM](https://www.npmjs.com/package/messenger-node):\n\n```\nnpm install messenger-node --save\n```\n\n\n## Importing the SDK\n\nTo use the SDK, start by importing it into your project:\n\n```js\nconst Messenger = require('messenger-node');\n```\n\nOnce the SDK is imported, you can create instances of the `Webhook` and `Client` classes.\n\n\n## Using `Messenger.Webhook`\n\nEvery Messenger bot has to have a webhook that the Messenger Platform can send [webhook events](https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/) to. This SDK provides a simple `Webhook` class that you can instantiate to create and run a fully-functional webhook. The webhook can do all these things for you:\n\n- Handle the webhook verification request sent by the Messenger Platform when you register your webhook. \n- Parse all incoming webhook events, and emit them for you to react to with event listeners.\n- Validate signed requests from the Messenger Extensions SDK [`getContext()`](https://developers.facebook.com/docs/messenger-platform/webview/context) function.\n\nBasically it saves you the hassle of writing the basic stuff so you can get to building your bot right away.\n\n### Creating a Webhook\n\nTo create a webhook, start by creating an instance of the `Webhook` class. The following configuration properties may be provided when the `Webhook` instance is created: \n\n- `verify_token`: __Required.__ The token to use for webhook verification. The Messenger Platform will send this with the challenge request when you register your webhook.\n- `port`: _Optional._ The port number your webhook should listen on. Defaults to the `process.env.PORT`.\n- `endpoint`: _Optional._ The endpoint for your webhook. For example, if your webhook base URL is `https://www.mywebhook.com` and `endpoint` is set to `bananas`, the full URL that you should tell the Messenger Platform to send webhook events to is `https://www.mywebhook.com/bananas/`. Defaults to `webhook`\n- `app_secret`: _Optional._ Your app secret. This is required if you want to validate signed webview requests using [Webhook.validateSignedRequest()](#validatesignedrequest).\n\n```js\nlet webhook_config = {\n  'verify_token':'MY_VERIFY_TOKEN'\n}\n\nconst Webhook = new Messenger.Webhook(webhook_config);\n```\n\n### Handling Webhook Events\n\nWhen the Messenger Platform sends your webhook a [webhook event](https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/) the SDK will emit it by name, and include info about the sender and the full body of the received event.\n\nFor a list of available webhook events, see the [list in the Messenger Platform docs](https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/).\n\nTo listen for a particular event, all you have to do is add an event listener with [`Webhook.on`](#on) or `[Webhook.once`](#once):\n\n```js\nWebhook.on('messaging_postbacks', (event_type, sender_info, webhook_event) =\u003e {\n  // do something\n});\n```\n\nYou can also [emit](#emit) events, which can be useful for testing:\n\n```js\nWebhook.emit('messaging_postbacks', event_type, sender_info, webhook_event);\n```\n\n#### Callback Arguments\n| **Name** | **Type** | **Description** | **Example** |\n|------|------|-------------|--------|\n| event_type | Object | Contains the event type and subtype. If the webhook has no subtype, then `event_type.subtype` will be `null` | `{'type': 'messaging_handovers', 'subtype': 'pass_thread_control}` |\n| sender_info | Object | Contains the ID and ID type. | `{'value': '84736289974242', 'type': 'PSID'}` |\n| webhook_event | Object | The complete webhook event parsed from the `messaging` array of the received `POST` request. | For webhook event formats and details, see the [webhook event reference](https://developers.facebook.com/docs/messenger-platform/reference/webhook-events/) in the Messenger Platform docs. |\n\n#### Supported Webhook Events\n\nAll webhook events sent by the Messenger Platform are supported. To listen for the event, all you have to do is attach an event listener for the event name:\n\n- `messages`\n- `messaging_postbacks`\n- `message_reads`\n- `message_echoes`\n- `message_deliveries`\n- `messaging_handovers`\n- `messaging_referrals`\n- `messaging_optins`\n- `messaging_payments`\n- `messaging_pre_checkouts`\n- `messaging_checkout_updates`\n- `messaging_game_plays`\n- `messaging_policy_enforcement`\n- `messaging_account_linking`\n- `standby`\n\n\n\n## Using `Messenger.Client`\n\nOnce you have a working webhook, you need a way to respond to the events and messages your bot receives. This means you need to be able to send allllll kiiinnndddsss of API requests to the Messenger Platform. The `Client` provided by this SDK makes this a much simpler declarative process by handling all the repetitive (and error prone) parts of formatting valid API requests for you, and making sure they get sent to the right place.\n\n### Creating a `Client` Instance\n\nTo make API calls, start by creating an instance of the `Client` class. The following configuration properties may be provided when the `Client` instance is created: \n\n- `page_token`: __Required__. A valid Page-scope access token for the Page associated with your bot. This will be sent with almost every API request.\n- `app_token`: _Optional._ A valid app-scoped access token. This is only used for certain calls to the Platform's [ID Matching API](https://developers.facebook.com/docs/messenger-platform/identity/id-matching).\n- `api_version`: _Optional._ The version of the Graph API to target for all API requests in the format `v2.11`. Defaults to the latests version of the Graph API.\n\n```js\nlet client_config = {\n  'page_token': 'EAAYcRUPT1JNby4BSexAmPMuy3QBAGYBAorwzZC7FnQZBZBkFZAOZAF9CBQzt6qsdCwg',\n  'app_token': 'qsdT1JNby4BSexAmPMuy3QBAGYBAorwzZC7FnQZBZBEAAYcRUPFkFZAOZACwg9CBQzt6',\n  'api_version': 'v2.11'\n}\n\nconst Client = new Messenger.Client(client_config);\n```\n\n### Making API Calls\n\nAll Messenger Platform API requests are included as instance functions that are called on the `Client` instance. Here are a few examples. For a complete list, see the [`Client` reference](#client) below. \n\n#### Examples\n\nHere are a few example requests. The [Reference](#reference) also includes examples for every call.\n\n__Send a Text Message__\n\n```js\n// define the message recipient\nlet recipient = {\n  'id': '1234556'\n};\n\n// set the message to send\nlet text = 'This is my amazing text message?!'\n\n// send the text message\nClient.sendText(recipient, text)\n  .then(res =\u003e {\n    // log the api response\n    console.log(res);\n  })\n  .catch(e =\u003e {\n    console.error(e);\n  });\n```\n\n__Send a Generic Template Message__\n\n```js\n// define the message recipient\nlet recipient = {\n  'id': '1234556'\n};\n\n// define the generic template\nlet generic_template = {\n  template_type: 'generic',\n  elements: [\n    {\n      'title':'This is a generic template',\n      'subtitle':'Plus a subtitle!',\n      'buttons':[\n        {\n        'type':'postback',\n        'title':'Postback Button',\n        'payload':'postback_payload'\n        },\n        {\n        'type': 'web_url',\n        'title': 'URL Button',\n        'url': 'https://messenger.fb.com/'\n        }\n      ]      \n    }\n  ]\n}\n\n// send the template\nClient.sendTemplate(recipient, generic_template)\n  .then(res =\u003e {\n    // log the api response\n    console.log(res);\n  });\n  .catch(e) {\n    console.error(e);\n  }\n```\n\n__Get a User's Profile__\n\n```js\n// PSID of the user\nlet psid = '12345324'\n\n// profile fields to retrieve\nlet fields = ['id', 'first_name', 'last_name', 'profile_pic']\n\nClient.getUserProfile(psid, fields)\n  .then(res =\u003e {\n    // log the api response\n    console.log(res);\n  });\n  .catch(e) {\n    console.error(e);\n  }\n```\n\n### API Responses \u0026 Error Handling\n\nAll SDK functions return a promise that resolves with the full API response received from the Messenger Platform on success, and rejects with an error string on failure. Functions also do light input-checking when called, and will reject if incorrect arguments are provided.\n\nThe SDK does not transform the API response in any way, other than ensuring you get back a JSON object.\n\n\n\n## Reference\n\u003c!-- Generated by documentation.js. Update this documentation by updating the source code. --\u003e\n\n### Table of Contents\n\n-   [Client][1]\n    -   [Parameters][2]\n    -   [Examples][3]\n    -   [uploadAttachment][4]\n        -   [Parameters][5]\n        -   [Examples][6]\n    -   [setPageToken][7]\n        -   [Parameters][8]\n        -   [Examples][9]\n    -   [getPageToken][10]\n        -   [Examples][11]\n    -   [setAppToken][12]\n        -   [Parameters][13]\n        -   [Examples][14]\n    -   [getAppToken][15]\n        -   [Examples][16]\n    -   [setApiVersion][17]\n        -   [Parameters][18]\n        -   [Examples][19]\n    -   [getApiVersion][20]\n        -   [Examples][21]\n    -   [passThreadControl][22]\n        -   [Parameters][23]\n        -   [Examples][24]\n    -   [takeThreadControl][25]\n        -   [Parameters][26]\n        -   [Examples][27]\n    -   [requestThreadControl][28]\n        -   [Parameters][29]\n        -   [Examples][30]\n    -   [getThreadOwner][31]\n        -   [Parameters][32]\n        -   [Examples][33]\n    -   [getSecondaryReceiverList][34]\n        -   [Parameters][35]\n        -   [Examples][36]\n    -   [sendBroadcast][37]\n        -   [Parameters][38]\n        -   [Examples][39]\n    -   [startBroadcastReachEstimation][40]\n        -   [Parameters][41]\n        -   [Examples][42]\n    -   [getBroadcastReachEstimation][43]\n        -   [Parameters][44]\n        -   [Examples][45]\n    -   [createCustomLabel][46]\n        -   [Parameters][47]\n        -   [Examples][48]\n    -   [getCustomLabelById][49]\n        -   [Parameters][50]\n        -   [Examples][51]\n    -   [getCustomLabelsByPsid][52]\n        -   [Parameters][53]\n        -   [Examples][54]\n    -   [getAllCustomLabels][55]\n        -   [Examples][56]\n    -   [deleteCustomLabel][57]\n        -   [Parameters][58]\n        -   [Examples][59]\n    -   [addPsidtoCustomLabel][60]\n        -   [Parameters][61]\n        -   [Examples][62]\n    -   [removePsidfromCustomLabel][63]\n        -   [Parameters][64]\n        -   [Examples][65]\n    -   [createMessageCreative][66]\n        -   [Parameters][67]\n        -   [Examples][68]\n    -   [sendText][69]\n        -   [Parameters][70]\n        -   [Examples][71]\n    -   [sendQuickReplies][72]\n        -   [Parameters][73]\n        -   [Examples][74]\n    -   [sendAttachment][75]\n        -   [Parameters][76]\n        -   [Examples][77]\n    -   [sendTemplate][78]\n        -   [Parameters][79]\n        -   [Examples][80]\n    -   [sendSenderAction][81]\n        -   [Parameters][82]\n        -   [Examples][83]\n    -   [sendSponsoredMessage][84]\n        -   [Parameters][85]\n        -   [Examples][86]\n    -   [getMessagingInsights][87]\n        -   [Parameters][88]\n        -   [Examples][89]\n    -   [generateMessengerCode][90]\n        -   [Parameters][91]\n        -   [Examples][92]\n    -   [setMessengerProfile][93]\n        -   [Parameters][94]\n        -   [Examples][95]\n    -   [getMessengerProfile][96]\n        -   [Parameters][97]\n        -   [Examples][98]\n    -   [deleteMessengerProfile][99]\n        -   [Parameters][100]\n        -   [Examples][101]\n    -   [setNlpConfigs][102]\n        -   [Parameters][103]\n        -   [Examples][104]\n    -   [getMatchingPsids][105]\n        -   [Parameters][106]\n        -   [Examples][107]\n    -   [getMatchingAsids][108]\n        -   [Parameters][109]\n        -   [Examples][110]\n    -   [getUserProfile][111]\n        -   [Parameters][112]\n        -   [Examples][113]\n-   [Webhook][114]\n    -   [Parameters][115]\n    -   [Examples][116]\n    -   [on][117]\n        -   [Parameters][118]\n        -   [Examples][119]\n    -   [once][120]\n        -   [Parameters][121]\n        -   [Examples][122]\n    -   [emit][123]\n        -   [Parameters][124]\n        -   [Examples][125]\n    -   [getInstance][126]\n        -   [Examples][127]\n    -   [stopInstance][128]\n        -   [Parameters][129]\n        -   [Examples][130]\n    -   [getPort][131]\n        -   [Examples][132]\n    -   [getEndpoint][133]\n        -   [Examples][134]\n    -   [getVerifyToken][135]\n        -   [Examples][136]\n    -   [setAppSecret][137]\n        -   [Parameters][138]\n        -   [Examples][139]\n    -   [validateSignedRequest][140]\n        -   [Parameters][141]\n        -   [Examples][142]\n\n## Client\n\nCreates an instance of `Client`, used for sending requests to the Messenger Platform APIs.\n\n### Parameters\n\n-   `options` **[Object][143]** An object that contains the configuration settings for the `Client`.\n    -   `options.page_token` **[String][144]** A valid Page-scoped access token.\n    -   `options.app_token` **[String][144]** _Optional._ A valid app-scoped access token. Required for ID Matching.\n    -   `options.graph_api_version` **[String][144]** _Optional._ The version of the Graph API to target for all API requests. Defaults to latest. Must be in the format `v2.11`.\n\n### Examples\n\n```javascript\nconst Messenger = require('messenger-node');\nlet options = {\n  'page_token': 'sd0we98h248n2g40gh4g80h32',\n  'app_token': 'ih908wh084ggh423940hg934g358h0358hg3', //optional\n  'api_version': 'v2.9' //optional\n}\nconst Client = new Messenger.Client(options);\n```\n\nReturns **[Client][145]** \n\n### uploadAttachment\n\nUploads media using the [Attachment Upload API][146].\n\n#### Parameters\n\n-   `attachment` **[Object][143]** An object that describes the attachment to send.\n    -   `attachment.type` **[String][144]** The type of asset being upload. Must be `image`, `video`, `audio`, or `file`.\n    -   `attachment.source` **[String][144]** The location of the asset. Must be a valid URL or complete filesystem location.\n    -   `attachment.is_reusable` **[String][144]** **Optional.** Set to `true` to return a reusable attachment ID.\n\n#### Examples\n\nUpload from URL\n\n\n```javascript\nlet recipient = {'id': '57024957309673'},\n    attachment = {\n      'type':'image', \n       'source':'https://www.example.com/dog.png', \n       'is_reusable':true           \n    }\nClient.uploadAttachment('url', 'https://www.example.com/image.jpg')\n .then(res =\u003e {\n    console.log(res) // {'attachment_id': 09754203957254}\n });\n```\n\nUpload from file\n\n\n```javascript\nlet recipient = {'id': '57024957309673'},\n    attachment = {\n      'type':'image', \n       'source':'/Users/me/Desktop/dog.jpg', \n       'is_reusable':true           \n    }\nClient.uploadAttachment(attachment)\n .then(res =\u003e {\n    console.log(res); // {'attachment_id': 09754203957254}\n });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API response\n\n### setPageToken\n\nSets a new page token to use for all Page-level requests\n\n#### Parameters\n\n-   `token`  \n-   `page_token` **[string][144]** The new page token\n\n#### Examples\n\n```javascript\nClient.setPageToken('sgh084th3t3ht340t34h8t3t940390th34')\n .then(res =\u003e {\n    console.log(res) // 'sgh084th3t3ht340t34h8t3t940390th34'\n });\n```\n\nReturns **[string][144]** Updated page token\n\n### getPageToken\n\nGets the current page token being used for page-level requests\n\n#### Examples\n\n```javascript\nClient.getPageToken()\n .then(res =\u003e {\n    console.log(res) // 'sgh084th3t3ht340t34h8t3t940390th34'\n });\n```\n\nReturns **[string][144]** Current page token\n\n### setAppToken\n\nSets a new app token to use for all app-level requests\n\n#### Parameters\n\n-   `token`  \n-   `app_token` **[string][144]** The new app token\n\n#### Examples\n\n```javascript\nClient.setAppToken('9h03t9h0ahtg409thw3t34h8t3t940390th34')\n .then(res =\u003e {\n    console.log(res) // '9h03t9h0ahtg409thw3t34h8t3t940390th34'\n });\n```\n\nReturns **[string][144]** Updated app token\n\n### getAppToken\n\nGets the current app token being used for app-level requests\n\n#### Examples\n\n```javascript\nClient.getAppToken()\n .then(res =\u003e {\n    console.log(res) // '9h03t9h0ahtg409thw3t34h8t3t940390th34'\n });\n```\n\nReturns **[string][144]** Current app token\n\n### setApiVersion\n\nSets a new Graph API version to use for all requests\n\n#### Parameters\n\n-   `version` **[string][144]** The new version in the format `v2.11`\n\n#### Examples\n\n```javascript\nClient.setApiVersion('v2.6')\n .then(res =\u003e {\n    console.log(res) // 'v2.6'\n });\n```\n\nReturns **[string][144]** Updated version number\n\n### getApiVersion\n\nGets the current Graph API version being used for all requests\n\n#### Examples\n\n```javascript\nClient.getApiVersion()\n .then(res =\u003e {\n    console.log(res) // 'v2.6'\n });\n```\n\nReturns **[string][144]** Current Graph API version\n\n### passThreadControl\n\nInitiates a new handover protocol [pass thread control][148] event.\n\n#### Parameters\n\n-   `psid` **Integer** The PSID of the user whose thread you want to initiate the pass thread control event for.\n-   `target_app_id` **Integer** _Optional._ The app ID of the app to pass thread control to. Set to `page_inbox` to pass thread control to the Page Inbox.\n-   `metadata` **[String][144]** _Optional._ An arbitrary string that will be delivered to the target app with the `messaging_handovers` webhook event.\n\n#### Examples\n\n```javascript\nlet psid = 1008372609250235,\n    target_app_id = 1719933432123212,\n    metadata = 'I passed control to you'; // optional\nClient.passThreadControl(psid, target_app_id, metadata)\n  .then(res =\u003e {\n    console.log(res); // {'success': true}\n  });\n```\n\nReturns **[Promise][147]** The API response\n\n### takeThreadControl\n\nInitiates a new handover protocol [take thread control][149] event.\nThis may only be called by the app with the Primary Receiver app role.\n\n#### Parameters\n\n-   `psid` **Integer** The PSID of the user whose thread you want to initiate the take thread control event for.\n-   `metadata` **[String][144]** _Optional._ An arbitrary string that will be delivered to the Secondary Receiver app with the `messaging_handovers` webhook event.\n\n#### Examples\n\n```javascript\nlet psid = 1008372609250235,\n    metadata = 'I'm taking control from you'; // optional\nClient.takeThreadControl(psid, metadata)\n  .then(res =\u003e {\n    console.log(res); // {'success': true}\n  });\n```\n\nReturns **[Promise][147]** The API response\n\n### requestThreadControl\n\nInitiates a new handover protocol [request thread control][150] event.\n\n#### Parameters\n\n-   `psid` **Integer** The PSID of the user whose thread you want to initiate the request thread control event for.\n-   `metadata` **[String][144]** _Optional._ An arbitrary string that will be delivered to the Primary Receiver app with the `messaging_handovers` webhook event.\n\n#### Examples\n\n```javascript\nlet psid = 1008372609250235,\n    metadata = 'I'm requesting control from you'; // optional\nClient.requestThreadControl(psid, metadata)\n  .then(res =\u003e {\n    console.log(res); // {'success': true}\n  });\n```\n\nReturns **[Promise][147]** The API response\n\n### getThreadOwner\n\nRetrieves the app ID of the current [thread owner][149].\n\n#### Parameters\n\n-   `psid` **Integer** The PSID of the user whose thread you want to get the thread owner of.\n-   `metadata` **[String][144]** _Optional._ An arbitrary string that will be delivered to the target app with the `messaging_handovers` webhook event.\n\n#### Examples\n\n```javascript\nlet psid = 1008372609250235\nClient.getThreadOwner(psid)\n  .then(res =\u003e {\n    console.log(res); \n    // {\n    //   \"data\": [{\n    //     \"thread_owner\": {\n    //       \"app_id\": \"1719933678308212\"\n    //     }\n    //   }]\n    // }\n  });\n```\n\nReturns **[Promise][147]** The API response\n\n### getSecondaryReceiverList\n\nRetrieves a list of app ID's of all [Secondary Receivers][151] for the Page.\n\n#### Parameters\n\n-   `psid` **Integer** The PSID of the user whose thread you want to get the list of Secondary Receiver apps for.\n\n#### Examples\n\n```javascript\nlet psid = 1008372609250235\nClient.getThreadOwner(psid)\n  .then(res =\u003e {\n    console.log(res); \n    // {\n    //   \"data\": [\n    //     {\n    //       \"id\": \"12345678910\",\n    //       \"name\": \"David's Composer\"\n    //     },\n    //     {\n    //       \"id\": \"23456789101\",\n    //       \"name\": \"Messenger Rocks\"\n    //     }\n    //   ]\n    // }\n  });\n```\n\nReturns **[Promise][147]** The API response\n\n### sendBroadcast\n\nSends a new broadcast message via the [Broadcast API][152].\n\n#### Parameters\n\n-   `message_creative_id` **Integer** The ID of a message creative to send in the broadcast. Created by calling [Client.createMessageCreative()][66].\n-   `custom_label_id` **Integer** _Optional._ The ID of a custom label to target for the broadcast. Created by calling [Client.createCustomLabel()][46].\n\n#### Examples\n\n```javascript\nlet message_creative_id = 499792492764246,\n    custom_label_id = 097046973276-46; // optional\nClient.sendBroadcast(message_creative_id, custom_label_id)\n  .then(res =\u003e {\n    console.log(res); // {'broadcast_id': 397230957240952}\n  });\n```\n\nReturns **[Promise][147]** The API response\n\n### startBroadcastReachEstimation\n\nStart a reach estimation for the number of people that will be \nreached by a broadcast to all users or to users associated with \na custom label.\n\n#### Parameters\n\n-   `custom_label_id` **Integer** _Optional._ The ID of a custom label targeted by the broadcast. Created by calling [Client.createCustomLabel()][46].\n\n#### Examples\n\n```javascript\nlet custom_label_id = 3467390467035645 //optional\nClient.startBroadcastReachEstimation(custom_label_id)\n  .then(res =\u003e {\n    console.log(res); // {\"reach_estimation_id\": \"9485676932424\"}\n  });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API Response\n\n### getBroadcastReachEstimation\n\nGet the current status of a broadcast reach estimation\n{@link #startbroadcastreachestimation|`startBroadcastReachEstimation` \nmust be run first to get a `reach_estimation_id`.\n\n#### Parameters\n\n-   `reach_estimation_id` **Integer** The reach estimation ID.\n\n#### Examples\n\n```javascript\nClient.getBroadcastReachEstimation(9485676932424)\n  .then(res =\u003e {\n    console.log(res); // {\"reach_estimation\": \"100\", \"id\": \"9485676932424\"}\n  });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API Response\n\n### createCustomLabel\n\nCreates a new custom label.\n\n#### Parameters\n\n-   `name` **[String][144]** The name of the custom label.\n\n#### Examples\n\n```javascript\nClient.createCustomLabel('my_custom_label')\n  .then(res =\u003e {\n    console.log(res); // {\"id\": \"9485676932424\"}\n  });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API response\n\n### getCustomLabelById\n\nRetrieves the id and name of a custom label.\n\n#### Parameters\n\n-   `label_id` **Integer** The ID of a custom label. Created with [createCustomLabel()][46].\n\n#### Examples\n\n```javascript\nlet custom_label_id = 9485676932424,\n    field = ['name', 'id']; //optional\nClient.getCustomLabelById(custom_label_id, fields)\n  .then(res =\u003e {\n    console.log(res); // {\"name\": \"my_custom_label\", \"id\": \"9485676932424\"}\n  });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API response\n\n### getCustomLabelsByPsid\n\nRetrieves the list of custom labels associated with a PSID.\n\n#### Parameters\n\n-   `psid` **Integer** The PSID of the user to retrieve the custom labels for.\n\n#### Examples\n\n```javascript\nClient.getCustomLabelsByPsid(950724069735075)\n  .then(res =\u003e {\n    console.log(res);\n    // {\n    //   \"data\": [\n    //     { \"name\": \"myLabel\", \"id\": \"1001200005003\"},\n    //     { \"name\": \"myOtherLabel\", \"id\": \"1001200005002\"}\n    //   ],\n    //   \"paging\": {\n    //     \"cursors\": {\n    //       \"before\": \"QVFIUmx1WTBpMGpJWXprYzVYaVhabW55dVpyc\",\n    //       \"after\": \"QVFIUmItNkpTbjVzakxFWGRydzdaVUFNNnNPaU\"\n    //     }\n    //   }\n    // }\n  });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API response\n\n### getAllCustomLabels\n\nRetrieves the list of all custom labels.\n\n#### Examples\n\n```javascript\nlet field = ['name', 'id']; //optional\nClient.getAllCustomLabels(fields)\n  .then(res =\u003e {\n    console.log(res);\n    // {\n    //   \"data\": [\n    //     { \"name\": \"myLabel\", \"id\": \"1001200005003\"},\n    //     { \"name\": \"myOtherLabel\", \"id\": \"1001200005002\"}\n    //   ],\n    //   \"paging\": {\n    //     \"cursors\": {\n    //       \"before\": \"QVFIUmx1WTBpMGpJWXprYzVYaVhabW55dVpyc\",\n    //       \"after\": \"QVFIUmItNkpTbjVzakxFWGRydzdaVUFNNnNPaU\"\n    //     }\n    //   }\n    // }\n  });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API response\n\n### deleteCustomLabel\n\nDeletes a custom label.\n\n#### Parameters\n\n-   `label_id` **Integer** The ID of the custom label to delete.\n\n#### Examples\n\n```javascript\nClient.deleteCustomLabel(094730967209673)\n  .then(res =\u003e {\n    console.log(res); // {\"success\": true}\n  });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API response\n\n### addPsidtoCustomLabel\n\nAssociates a user's PSID to a custom label.\n\n#### Parameters\n\n-   `psid` **Integer** PSID of the user to associate with the custom label.\n-   `label_id` **Integer** The ID of a custom label. Created with [createCustomLabel()][46].\n\n#### Examples\n\n```javascript\nlet psid = 49670354734069743,\n    custom_label_id = 0957209720496743; \nClient.addPsidtoCustomLabel(psid, custom_label_id)\n  .then(res =\u003e {\n    console.log(res); // {\"success\": true}\n  });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API response\n\n### removePsidfromCustomLabel\n\nRemoves a user PSID from a custom label.\n\n#### Parameters\n\n-   `psid` **Integer** PSID of the user to remove from the custom label.\n-   `label_id` **Integer** The ID of a custom label.\n\n#### Examples\n\n```javascript\nlet psid = 49670354734069743,\n    custom_label_id = 0957209720496743; \nClient.removePsidfromCustomLabel(psid, custom_label_id)\n  .then(res =\u003e {\n    console.log(res); // {\"success\": true}\n  });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API response\n\n### createMessageCreative\n\nCreates a new message creative.\n\n#### Parameters\n\n-   `message` **[Object][143]** An object that describes the message to send.\n\n#### Examples\n\nText Message\n\n\n```javascript\nlet message = {'text': 'my text message'};\nClient.createMessageCreative(message)\n  .then(res =\u003e {\n    console.log(res); // {\"message_creative_id\": \"953434576932424\"}\n  });\n```\n\nTemplate Message\n\n\n```javascript\nlet message = {\n  template_type: 'generic',\n  elements: [\n    {\n      'title':'This is a generic template',\n      'subtitle':'Plus a subtitle!',\n      'image_url':'https://www.example.com/dog.jpg',\n      'buttons':[\n        {\n          'type':'postback',\n          'title':'Postback Button',\n          'payload':'postback_payload'\n        },\n        {\n          'type': 'web_url',\n          'title': 'URL Button',\n          'url': 'https://www.example.com/'\n        }\n      ]      \n    }\n  ]\n};\nClient.createMessageCreative(message)\n  .then(res =\u003e {\n    console.log(res); // {\"message_creative_id\": \"953434576932424\"}\n  });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API response\n\n### sendText\n\nSends a text message via the [Send API][153].\n\n#### Parameters\n\n-   `recipient` **[Object][143]** An object that describes the message recipient in the format: `{\u003cid_type\u003e: \u003cid\u003e}`.\n    For example, sends to a PSID would be `{'id': 123456}`, to a phone number \\`{'phone_number': '+1 (408) 444-4444'}.\n-   `text` **[String][144]** The text to send.\n\n#### Examples\n\n```javascript\nlet recipient = {'id': '57024957309673'},\n    text = 'This is a text message';\nClient.sendText(text)\n  .then(res =\u003e {\n    console.log(res);\n    // {\n    //   \"recipient_id\": \"1008372609250235\", \n    //   \"message_id\": \"mid.1456970487936:c34767dfe57ee6e339\"\n    // }\n  });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API response\n\n### sendQuickReplies\n\nSends a set of quick reply buttons  via the [Send API][153].\n\n#### Parameters\n\n-   `recipient` **[Object][143]** An object that describes the message recipient in the format: `{\u003cid_type\u003e: \u003cid\u003e}`.\n    For example, sends to a PSID would be `{'id': 123456}`, to a phone number \\`{'phone_number': '+1 (408) 444-4444'}.\n-   `quick_replies` **[Object][143]** An object that describes the quick replies to send. This is the `message.quick_replies` property that would normally be included in a Send API request.\n-   `text` **[String][144]** _Optional._ Text message to send with quick replies.\n\n#### Examples\n\nGeneric Template\n\n\n```javascript\nlet recipient = {'id': '57024957309673'};\nlet quick_replies = [\n  {\n    'content_type':'text',\n    'title':'Quick Reply 1',\n    'image_url':'https://www.example.com/icon.png',\n    'payload':'quick_reply_payload'\n  },\n  {\n    'content_type':'location'\n  }\n];\nlet text = 'Text message to send with the quick replies'; //optional\nClient.sendQuickReplies(recipient, quick_replies, text)\n  .then(res =\u003e {\n    console.log(res);\n    // {\n    //   \"recipient_id\": \"1008372609250235\", \n    //   \"message_id\": \"mid.1456970487936:c34767dfe57ee6e339\"\n    // }\n  });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API response\n\n### sendAttachment\n\nSends a standalone attachment, including images, audio, video, and files  via the [Send API][153].\n\n#### Parameters\n\n-   `attachment` **[Object][143]** An object that describes the attachment to send.\n    -   `attachment.type` **[String][144]** The type of asset being upload. Must be `image`, `video`, `audio`, or `file`.\n    -   `attachment.source` **[String][144]** The location of the asset. Must be a valid URL or complete filesystem location.\n    -   `attachment.is_reusable` **[String][144]** **Optional.** Set to `true` to return a reusable attachment ID.\n-   `recipient` **[Object][143]** An object that describes the message recipient in the format: `{\u003cid_type\u003e: \u003cid\u003e}`.\n    For example, sends to a PSID would be `{'id': 123456}`, to a phone number \\`{'phone_number': '+1 (408) 444-4444'}.\n\n#### Examples\n\nSend attachment from URL\n\n\n```javascript\nlet recipient = {'id': '57024957309673'},\n    attachment = {\n      'type':'image', \n       'source':'https://www.example.com/dog.png', \n       'is_reusable':true           \n    }\nClient.sendAttachment(attachment, recipient)\n  .then(res =\u003e {\n    console.log(res); // {\"id\": \"9485676932424\"}\n    // {\n    //   \"recipient_id\": \"1008372609250235\", \n    //   \"message_id\": \"mid.1456970487936:c34767dfe57ee6e339\",\n    //   \"attachment_id\": \"395723096739076353\"\n    // }\n  });\n});\n```\n\nSend attachment from file\n\n\n```javascript\nlet recipient = {'id': '57024957309673'},\n    attachment = {\n      'type':'image', \n       'source':'/Users/me/Desktop/dog.jpg', \n       'is_reusable':true           \n    }\nClient.uploadAttachment(attachment, recipient)\n .then(res =\u003e {\n    console.log(res); // {'attachment_id': 09754203957254}\n    // {\n    //   \"recipient_id\": \"1008372609250235\", \n    //   \"message_id\": \"mid.1456970487936:c34767dfe57ee6e339\",\n    //   \"attachment_id\": \"395723096739076353\"\n    // }\n });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API response\n\n### sendTemplate\n\nSends a template message via the [Send API][153].\n\n#### Parameters\n\n-   `recipient` **[Object][143]** An object that describes the message recipient in the format: `{\u003cid_type\u003e: \u003cid\u003e}`.\n    For example, sends to a PSID would be `{'id': 123456}`, to a phone number \\`{'phone_number': '+1 (408) 444-4444'}.\n-   `template` **[Object][143]** An object that describes the template to send. This is the `message.attachment.payload` property that would normally be included in a Send API request.\n\n#### Examples\n\nGeneric Template\n\n\n```javascript\nlet recipient = {'id': '57024957309673'};\nlet message = {\n  template_type: 'generic',\n  elements: [\n    {\n      'title':'This is a generic template',\n      'subtitle':'Plus a subtitle!',\n      'image_url':'https://www.example.com/dog.jpg',\n      'buttons':[\n        {\n          'type':'postback',\n          'title':'Postback Button',\n          'payload':'postback_payload'\n        },\n        {\n          'type': 'web_url',\n          'title': 'URL Button',\n          'url': 'https://www.example.com/'\n        }\n      ]      \n    }\n  ]\n};\nClient.sendTemplate(message)\n  .then(res =\u003e {\n    console.log(res);\n    // {\n    //   \"recipient_id\": \"1008372609250235\", \n    //   \"message_id\": \"mid.1456970487936:c34767dfe57ee6e339\"\n    // }\n  });\n```\n\nMedia Template\n\n\n```javascript\nlet recipient = {'id': '57024957309673'};\nlet message = {\n  'template_type': 'media',\n  'elements': [\n    {\n      'media_type': 'image',\n      'url': 'https://www.example.com/dog.jpg'\n    },\n    'buttons':[\n      {\n        'type': 'web_url',\n        'title': 'URL Button',\n        'url': 'https://www.example.com/'\n      }\n    ]    \n  ]\n};\nClient.sendTemplate(message)\n  .then(res =\u003e {\n    console.log(res);\n    // {\n    //   \"recipient_id\": \"1008372609250235\", \n    //   \"message_id\": \"mid.1456970487936:c34767dfe57ee6e339\"\n    // }\n  });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API response\n\n### sendSenderAction\n\nSends a sender action via the [Send API][153].\n\n#### Parameters\n\n-   `recipient` **[Object][143]** An object that describes the message recipient in the format: `{\u003cid_type\u003e: \u003cid\u003e}`.\n    For example, sends to a PSID would be `{'id': 123456}`, to a phone number \\`{'phone_number': '+1 (408) 444-4444'}.\n-   `sender_action` **[String][144]** The sender action to send. Must be `typing_on`, `typing_off`, or `mark_seen`.\n\n#### Examples\n\n```javascript\nlet recipient = {'id': '57024957309673'},\n    sender_action = 'mark_seen';\nClient.sendSenderAction(recipient, sender_action)\n  .then(res =\u003e {\n    console.log(res); // {\"recipient_id\": \"1008372609250235\"}\n  });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API response\n\n### sendSponsoredMessage\n\nSends a new [Sponsored Message via the Messenger Platform][154].\n\n#### Parameters\n\n-   `ad_account_id` **Integer** Your Facebook Ads account ID.\n-   `options` **[Object][143]** An object that describes the sponsored message to send.\n    -   `options.message_creative_id` **[String][144]** The ID of a message creative to send. Created by calling [Client.createMessageCreative()][66].\n    -   `options.daily_budget` **[String][144]** The maximum daily budget of the ad campaign for the Sponsored Message send.\n    -   `options.bid_amount` **[String][144]** The maximum bid for each message recipient.\n    -   `options.targeting` **[String][144]** [Targeting spec][155] for the Sponsored Message send.\n\n#### Examples\n\n```javascript\nlet options = {\n  'message_creative_id': '34967347634346',\n  'daily_budget': '10',\n  'bid_amount': '1',\n  'targeting': '{'geo_locations': {'countries':['US']}}',\n  'ad_account_id': '9352379502706' \n};\nClient.sendSponsoredMessage('test', options)\n  .then(res =\u003e {\n    console.log(res);\n    // {\n    //   \"ad_group_id\": \"6088387928148\",\n    //   \"broadcast_id\": \"754911018029273\",\n    //   \"success\": true\n    // }\n  });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API response\n\n### getMessagingInsights\n\nRetrieves metrics from the [Messaging Insights API][156].\n\n#### Parameters\n\n-   `options` **[Object][143]** An object that describes the metrics data to retrieve.\n    -   `options.metrics` **[Array][157]\u0026lt;[String][144]\u003e** An array list of the metrics to retrieve.\n    -   `options.since` **[String][144]** _Optional._ UNIX timestamp of the start time to get the metric for.\n    -   `options.until` **[String][144]** _Optional._ UNIX timestamp of the end time to get the metric for.\n\n#### Examples\n\n```javascript\nlet today = new Date().getTime();\nlet options = {\n  'metrics': [\n    'page_messages_active_threads_unique',\n    'page_messages_blocked_conversations_unique',\n    'page_messages_reported_conversations_unique',\n    'page_messages_reported_conversations_by_report_type_unique'\n  ],\n  'since': today - 864000,\n  'until': today\n};\nClient.getMessagingInsights(options)\n  .then(res =\u003e {\n    console.log(res);\n    // { \n    //   \"data\": [ \n    //     { \n    //       \"name\": \"\u003cMETRIC\u003e\", \n    //       \"period\": \"day\", \n    //       \"values\": [ \n    //         { \n    //           \"value\": \"\u003cVALUE\u003e\", \n    //           \"end_time\": \"\u003cUTC_TIMESTAMP\u003e\" \n    //         }, \n    //         { \n    //           \"value\": \"\u003cVALUE\u003e\", \n    //           \"end_time\": \"\u003cUTC_TIMESTAMP\u003e\" \n    //         }\n    //      ]\n    //     } \n    //   ],\n    // } \n  });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API response\n\n### generateMessengerCode\n\nGenerate a new static or parametric [Messenger Code][158] for your bot.\n\n#### Parameters\n\n-   `options` **[Object][143]** An object that describes the Messenger Code to generate.\n    -   `options.ref` **Integer** The ref string to pass to your bot is opened via the code. Max 250 characters. Valid characters: `a-z A-Z 0-9 +/=-.:_`.\n    -   `options.image_size` **[Object][143]** The size, in pixels, for the image you are requesting. Supported range: 100-2000. Defaults to 1000.\n\n#### Examples\n\n```javascript\nlet options = {\n  'ref': 'referral_ref', //optional\n  'image_size': 500 //optional\n};\nClient.generateMessengerCode(options)\n  .then(res =\u003e {\n    console.log(res); // {\"uri\": \"https://scontent.xx.fbcdn.net/v/t39...\"}\n  });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API response\n\n### setMessengerProfile\n\nSets one or more properties of your bot's [Messenger Profile][159].\n\n#### Parameters\n\n-   `fields` **[Object][143]** An object that contains the Messenger Profile properties to set as key-value pairs.\n\n#### Examples\n\n```javascript\nlet fields = {\n  'whitelisted_domains': ['https://www.example.com'],    \n  'get_started': {\n    'payload': 'callback_payload'\n  },\n  'greeting': [\n    {\n      'locale':'default',\n      'text':'Hello!'\n    }, {\n      'locale':'en_US',\n      'text':'Timeless apparel for the masses.'\n    }\n  ]\n};\nClient.setMessengerProfile(fields)\n  .then(res =\u003e {\n    console.log(res); // {\"result\": \"success\"}\n  });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API response\n\n### getMessengerProfile\n\nRetrieves one or more properties of your bot's [Messenger Profile][159].\n\n#### Parameters\n\n-   `fields` **[Array][157]\u0026lt;[String][144]\u003e** _Optional._ An array list of the Messenger Profile filds to retrieve.\n\n#### Examples\n\n```javascript\nlet fields = ['whitelisted_domains', 'greeting'];\nClient.getMessengerProfile(fields)\n  .then(res =\u003e {\n    console.log(res);\n    // {\n    //    \"data\": [\n    //         {\n    //           \"whitelisted_domains\": [\n    //             \"https://facebook.com/\"\n    //           ],\n    //           \"greeting\": [\n    //             {\n    //                \"locale\": \"default\",\n    //                \"text\": \"Hello!\"\n    //             }\n    //          ]\n    //       }\n    //    ]\n    // } \n  });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API response\n\n### deleteMessengerProfile\n\nDeletes one or more properties of your bot's [Messenger Profile][159].\n\n#### Parameters\n\n-   `fields` **[Array][157]\u0026lt;[String][144]\u003e** _Optional._ An array list of the Messenger Profile filds to delete.\n\n#### Examples\n\n```javascript\nlet fields = ['whitelisted_domains', 'greeting'];\nClient.deleteMessengerProfile(fields)\n  .then(res =\u003e {\n    console.log(res); // {\"id\": \"9485676932424\"}\n  });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API response\n\n### setNlpConfigs\n\nSets config values for [built-in NLP]\\([https://developers.facebook.com/docs/messenger-platform/built-in-nlp][160].\n\n#### Parameters\n\n-   `configs` **[Object][143]** The NLP configs to set\n\n#### Examples\n\n```javascript\nlet configs = {\n  'nlp_enabled': true,\n  'model': 'ENGLISH',\n  'custom_token': '924t2904t7304ty3wo',\n  'verbose': true,\n  'n_best': 2\n}\nClient.setNlpConfigs(configs).then(res =\u003e {\n .then(res =\u003e {\n    console.log(res) // { 'success': true }\n });\n```\n\nReturns **[String][144]** configs.nlp_enabled  Enable/disable built-in NLP. Must be `true` or `false`\n\nReturns **[String][144]** configs.model  The default NLP model to use. For values, see the [Messenger Platform docs][161].\n\nReturns **[String][144]** configs.custom_token  [Wit.ai][162] server token for integrating a custom model.\n\nReturns **[String][144]** configs.verbose  Enables verbose mode, which returns extra information like the position of the detected entity in the query. Must be `true` or `false`.\n\nReturns **[String][144]** configs.n_best  The number of entities to return, in descending order of confidence. Minimum 1. Maximum 8.\n\n### getMatchingPsids\n\nReturns all Page-scoped IDs (PSIDs) for a user across all Pages in the same \nFacebook Business Manager account. Matches can be found using \na PSID or ASID. Uses the [ID Matching API][163].\n\n#### Parameters\n\n-   `id` **[String][144]** A valid ASID or PSID.\n-   `id_type` **[String][144]** The type of ID provided in the `id` argument: `ASID` or `PSID`.\n\n#### Examples\n\n```javascript\nClient.getMatchingPsids('95740976304764', 'PSID')\n  .then(res =\u003e {\n    console.log(res);\n    // {\n    //    \"data\": [\n    //      {\n    //        \"id\": \"1429384723454138\",\n    //        \"page\": {\n    //            \"name\": \"MyPage1\",\n    //            \"id\": \"9384723458738365\"\n    //        }\n    //      },\n    //      {\n    //        \"id\": \"1254459384723459\",\n    //        \"page\": {\n    //            \"name\": \"MyPage2\",\n    //            \"id\": \"689384723453165\"\n    //        }\n    //      }\n    //    ],\n    //    \"paging\": {\n    //        \"cursors\": {\n    //            \"before\": \"MTA4MDYxNjQ2ODczODM2NQZDZD\",\n    //            \"after\": \"NjgyNDk4MTcxOTQzMTY1\"\n    //        }\n    //    }\n    // } \n  });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API response\n\n### getMatchingAsids\n\nReturns all app-scoped IDs (ASIDs) for a user across all Pages in the same \nFacebook Business Manager account. Matches can be found using \na PSID or ASID. Uses the [ID Matching API][163].\n\n#### Parameters\n\n-   `id` **[String][144]** A valid ASID or PSID.\n-   `id_type` **[String][144]** The type of ID provided in the `id` argument: `ASID` or `PSID`.\n\n#### Examples\n\n```javascript\nClient.getMatchingAsids('95740976304764', 'PSID')\n  .then(res =\u003e {\n    console.log(res);\n    // {\n    //    \"data\": [\n    //      {\n    //        \"id\": \"7234541381429384\",\n    //        \"app\": {\n    //            \"link\": \"https://www.facebook.com/games/?app_id=299493827472589\",\n    //            \"name\": \"MyApp1\",\n    //            \"id\": \"9948573218738365\"\n    //        }\n    //      },\n    //      {\n    //        \"id\": \"9384723459125445\",\n    //        \"app\": {\n    //            \"link\": \"https://www.facebook.com/games/?app_id=299490394856589\",\n    //            \"name\": \"MyApp2\",\n    //            \"id\": \"689384785732187\"\n    //        }\n    //      }\n    //    ],\n    //    \"paging\": {\n    //        \"cursors\": {\n    //            \"before\": \"ODczODM2NQZDZDMTA4MDYxNjQ2\",\n    //            \"after\": \"TcxOTQzMTY1NjgyNDk4M\"\n    //        }\n    //    }\n    // } \n  });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API response\n\n### getUserProfile\n\nRetrieves a user's profile via the [User Profile API][164].\n\n#### Parameters\n\n-   `psid` **Integer** A valid user PSID.\n-   `fields` **[Array][157]\u0026lt;[String][144]\u003e** _Optional._ An array list of the user profile filds to retrieve. For a list of available fields, see the [Messenger Platform docs][165].\n\n#### Examples\n\n```javascript\nlet profile_fields = [\n  'id',\n  'first_name',\n  'last_name',\n  'profile_pic',\n  'locale',\n];\nClient.getUserProfile('490730697356', profile_fields)\n  .then(res =\u003e {\n    console.log(res);\n    // {\n    //   \"first_name\": \"Peter\",\n    //   \"last_name\": \"Chang\",\n    //   \"profile_pic\": \"https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpf1/v/t1.0-1/p200x200/13055603_10105219398495383_8237637584159975445_n.jpg?oh=1d241d4b6d4dac50eaf9bb73288ea192\u0026oe=57AF5C03\u0026__gda__=1470213755_ab17c8c8e3a0a447fed3f272fa2179ce\",\n    //   \"locale\": \"en_US\", \n    // }\n  });\n```\n\nReturns **[Promise][147]\u0026lt;[Object][143]\u003e** The API response\n\n## Webhook\n\nCreates and starts a webhook that emits all received webhook events.\n\n### Parameters\n\n-   `options` **[Object][143]** Configuration options for your webhook. All options may also be set as environment variables.\n    -   `options.verify_token` **[string][144]** May also be set as `MESSENGER_VERIFY_TOKEN` in environment variables.\n    -   `options.endpoint` **[string][144]** _Optional._ Defaults to `/webhook`. May also be set as `MESSENGER_APP_ENDPOINT` in environment variables.\n    -   `options.app_secret` **[string][144]** _Optional._ Your app secret. Required for `validateSignedRequest()`. May also be set as `MESSENGER_APP_SECRET` in environment variables.\n    -   `options.port` **[string][144]** _Optional._ Defaults to `1337`. May also be set as `MESSENGER_PORT` in environment variables.\n\n### Examples\n\n```javascript\nconst Messenger = require('messenger-node');\n\n// you can also set these as env vars\nlet options = {\n  'verify_token': 'my_voice_is_my_passport',\n  'app_secret': 'ih908wh084ggh423940hg934g358h0358hg3', //optional\n  'endpoint': '/awesomewebhook' //optional\n  'port': '9485' //optional\n}\nconst Webhook = new Messenger.Webhook(options);\n```\n\nReturns **[Webhook][166]** \n\n### on\n\nAdds an event listener. Implements Node.js EventEmitter's [`emitter.on`][167].\n\n#### Parameters\n\n-   `event_name` **[String][144]** The name of the event to listen for.\n-   `callback` **[Function][168]** The callback to execute when the event is received.\n\n#### Examples\n\n```javascript\nWebhook.on('messaging_postbacks', (event_type, sender_info, webhook_event) =\u003e {\n  // do something \n});\n```\n\n### once\n\nAdds a one-time event listener that will be removed after it is called once. Implements Node.js EventEmitter's [`emitter.once`][169].\n\n#### Parameters\n\n-   `event_name` **[String][144]** The name of the event to listen for.\n-   `callback` **[Function][168]** The callback to execute when the event is received.\n\n#### Examples\n\n```javascript\nWebhook.once('messaging_postbacks', (event_type, sender_info, webhook_event) =\u003e {\n  // do something \n});\n```\n\n### emit\n\nEmits an event from the Webhook instance. Event listeners can be set with `Webhook.on()` and `Webhook.once()`. Implements Node.js EventEmitter's [`emitter.once`][170].\n\n#### Parameters\n\n-   `eventName` **[Object][143]** The name of the event to emit.\n\n#### Examples\n\n```javascript\nWebhook.emit('my_event', arg1, arg2);\n```\n\nReturns **[Object][143]** ...args  _Optional._ Arguments to pass to the event listener.\n\n### getInstance\n\nRetrieves the current Webhook instance. This is the express.js [`app`][171] instance.\n\n#### Examples\n\n```javascript\nlet instance = Webhook.getInstance();\nconsole.log(instance) // express.js app instance\n```\n\nReturns **[Object][143]** The Webhook instance.\n\n### stopInstance\n\nStops the Webhook instance.\n\n#### Parameters\n\n-   `callback` **[Function][168]** A callback function to execute when the webhook is stopped.\n\n#### Examples\n\n```javascript\nWebhook.stopInstance(() =\u003e console.log('Webhook is stopped'));\n```\n\n### getPort\n\nRetrieves the port the webhook is running on.\n\n#### Examples\n\n```javascript\nlet port = Webhook.getPort();\nconsole.log(port) // '1337'\n```\n\nReturns **[String][144]** The current port number.\n\n### getEndpoint\n\nRetrieves the current endpoint of the webhook.\n\n#### Examples\n\n```javascript\nlet endpoint = Webhook.getEndpoint();\nconsole.log(endpoint) // '/webhook'\n```\n\nReturns **[String][144]** The current endpoint.\n\n### getVerifyToken\n\nRetrieves the current verify token of the webhook.\n\n#### Examples\n\n```javascript\nlet verifyToken = Webhook.getVerifyToken();\nconsole.log(verifyToken) // 'my_verify_token'\n```\n\nReturns **[String][144]** The current verify token.\n\n### setAppSecret\n\nSets the app secret used for validating signed requests.\n\n#### Parameters\n\n-   `app_secret` **[String][144]** The app secret to set.\n\n#### Examples\n\n```javascript\nlet app_secret = Webhook.setAppSecret('hgr0a8h30gh30agh');\nconsole.log(app_secret) // 'hgr0a8h30gh30agh'\n```\n\nReturns **[String][144]** The app secret that was successfully set.\n\n### validateSignedRequest\n\nVerifies a signed request received by calling [`getcontext()`][172] in the Messenger webview.\n\n#### Parameters\n\n-   `signed_request` **[Object][143]** The signed request.\n\n#### Examples\n\n```javascript\nlet request = {\n  'tid': '1254453049382119',\n  'thread_type': 'USER_TO_PAGE', \n  'psid': '1254413049321919',\n  'signed_request': 'QDTuYBidQ7pbpxIbPwgsb__nHty2...'\n};   \nlet decrypted_request = Webhook.validateSignedRequest(signed_request);\n\nconsole.log(decrypted_request)\n// {\n//   \"psid\": \"1254413049321919\", \n//   \"algorithm\": \"HMAC-SHA256\", \n//   \"thread_type\": \"GROUP\", \n//   \"tid\": \"1254453049382119\", \n//   \"issued_at\": 1491351619, \n//   \"page_id\": 239483560376726\n// }\n```\n\nReturns **[Object][143]** The decrypted signed request.\n\n[1]: #client\n\n[2]: #parameters\n\n[3]: #examples\n\n[4]: #uploadattachment\n\n[5]: #parameters-1\n\n[6]: #examples-1\n\n[7]: #setpagetoken\n\n[8]: #parameters-2\n\n[9]: #examples-2\n\n[10]: #getpagetoken\n\n[11]: #examples-3\n\n[12]: #setapptoken\n\n[13]: #parameters-3\n\n[14]: #examples-4\n\n[15]: #getapptoken\n\n[16]: #examples-5\n\n[17]: #setapiversion\n\n[18]: #parameters-4\n\n[19]: #examples-6\n\n[20]: #getapiversion\n\n[21]: #examples-7\n\n[22]: #passthreadcontrol\n\n[23]: #parameters-5\n\n[24]: #examples-8\n\n[25]: #takethreadcontrol\n\n[26]: #parameters-6\n\n[27]: #examples-9\n\n[28]: #requestthreadcontrol\n\n[29]: #parameters-7\n\n[30]: #examples-10\n\n[31]: #getthreadowner\n\n[32]: #parameters-8\n\n[33]: #examples-11\n\n[34]: #getsecondaryreceiverlist\n\n[35]: #parameters-9\n\n[36]: #examples-12\n\n[37]: #sendbroadcast\n\n[38]: #parameters-10\n\n[39]: #examples-13\n\n[40]: #startbroadcastreachestimation\n\n[41]: #parameters-11\n\n[42]: #examples-14\n\n[43]: #getbroadcastreachestimation\n\n[44]: #parameters-12\n\n[45]: #examples-15\n\n[46]: #createcustomlabel\n\n[47]: #parameters-13\n\n[48]: #examples-16\n\n[49]: #getcustomlabelbyid\n\n[50]: #parameters-14\n\n[51]: #examples-17\n\n[52]: #getcustomlabelsbypsid\n\n[53]: #parameters-15\n\n[54]: #examples-18\n\n[55]: #getallcustomlabels\n\n[56]: #examples-19\n\n[57]: #deletecustomlabel\n\n[58]: #parameters-16\n\n[59]: #examples-20\n\n[60]: #addpsidtocustomlabel\n\n[61]: #parameters-17\n\n[62]: #examples-21\n\n[63]: #removepsidfromcustomlabel\n\n[64]: #parameters-18\n\n[65]: #examples-22\n\n[66]: #createmessagecreative\n\n[67]: #parameters-19\n\n[68]: #examples-23\n\n[69]: #sendtext\n\n[70]: #parameters-20\n\n[71]: #examples-24\n\n[72]: #sendquickreplies\n\n[73]: #parameters-21\n\n[74]: #examples-25\n\n[75]: #sendattachment\n\n[76]: #parameters-22\n\n[77]: #examples-26\n\n[78]: #sendtemplate\n\n[79]: #parameters-23\n\n[80]: #examples-27\n\n[81]: #sendsenderaction\n\n[82]: #parameters-24\n\n[83]: #examples-28\n\n[84]: #sendsponsoredmessage\n\n[85]: #parameters-25\n\n[86]: #examples-29\n\n[87]: #getmessaginginsights\n\n[88]: #parameters-26\n\n[89]: #examples-30\n\n[90]: #generatemessengercode\n\n[91]: #parameters-27\n\n[92]: #examples-31\n\n[93]: #setmessengerprofile\n\n[94]: #parameters-28\n\n[95]: #examples-32\n\n[96]: #getmessengerprofile\n\n[97]: #parameters-29\n\n[98]: #examples-33\n\n[99]: #deletemessengerprofile\n\n[100]: #parameters-30\n\n[101]: #examples-34\n\n[102]: #setnlpconfigs\n\n[103]: #parameters-31\n\n[104]: #examples-35\n\n[105]: #getmatchingpsids\n\n[106]: #parameters-32\n\n[107]: #examples-36\n\n[108]: #getmatchingasids\n\n[109]: #parameters-33\n\n[110]: #examples-37\n\n[111]: #getuserprofile\n\n[112]: #parameters-34\n\n[113]: #examples-38\n\n[114]: #webhook\n\n[115]: #parameters-35\n\n[116]: #examples-39\n\n[117]: #on\n\n[118]: #parameters-36\n\n[119]: #examples-40\n\n[120]: #once\n\n[121]: #parameters-37\n\n[122]: #examples-41\n\n[123]: #emit\n\n[124]: #parameters-38\n\n[125]: #examples-42\n\n[126]: #getinstance\n\n[127]: #examples-43\n\n[128]: #stopinstance\n\n[129]: #parameters-39\n\n[130]: #examples-44\n\n[131]: #getport\n\n[132]: #examples-45\n\n[133]: #getendpoint\n\n[134]: #examples-46\n\n[135]: #getverifytoken\n\n[136]: #examples-47\n\n[137]: #setappsecret\n\n[138]: #parameters-40\n\n[139]: #examples-48\n\n[140]: #validatesignedrequest\n\n[141]: #parameters-41\n\n[142]: #examples-49\n\n[143]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object\n\n[144]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String\n\n[145]: #client\n\n[146]: https://developers.facebook.com/docs/messenger-platform/reference/attachment-upload-api\n\n[147]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise\n\n[148]: https://developers.facebook.com/docs/messenger-platform/handover-protocol/pass-thread-control\n\n[149]: https://developers.facebook.com/docs/messenger-platform/handover-protocol/take-thread-control\n\n[150]: https://developers.facebook.com/docs/messenger-platform/handover-protocol/request-thread-control\n\n[151]: https://developers.facebook.com/docs/messenger-platform/handover-protocol#secondary_receivers_list\n\n[152]: https://developers.facebook.com/docs/messenger-platform/reference/broadcast-api\n\n[153]: https://developers.facebook.com/docs/messenger-platform/reference/send-api\n\n[154]: https://developers.facebook.com/docs/messenger-platform/reference/sponsored-messages\n\n[155]: https://developers.facebook.com/docs/marketing-api/targeting-specs\n\n[156]: https://developers.facebook.com/docs/messenger-platform/reference/messaging-insights-api\n\n[157]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array\n\n[158]: https://developers.facebook.com/docs/messenger-platform/reference/messenger-code-api\n\n[159]: https://developers.facebook.com/docs/messenger-platform/reference/messenger-profile-api\n\n[160]: https://developers.facebook.com/docs/messenger-platform/built-in-nlp\n\n[161]: https://developers.facebook.com/docs/messenger-platform/built-in-nlp#api\n\n[162]: https://wit.ai/\n\n[163]: https://developers.facebook.com/docs/messenger-platform/identity/id-matching\n\n[164]: https://developers.facebook.com/docs/messenger-platform/identity/user-profile\n\n[165]: https://developers.facebook.com/docs/messenger-platform/identity/user-profile#fields\n\n[166]: #webhook\n\n[167]: https://nodejs.org/api/events.html#events_emitter_on_eventname_listener\n\n[168]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function\n\n[169]: https://nodejs.org/api/events.html#events_emitter_once_eventname_listener\n\n[170]: https://nodejs.org/api/events.html#events_emitter_emit_eventname_args\n\n[171]: http://expressjs.com/en/4x/api.html#app\n\n[172]: https://developers.facebook.com/docs/messenger-platform/webview/context\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famuramoto%2Fmessenger-node","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famuramoto%2Fmessenger-node","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famuramoto%2Fmessenger-node/lists"}