Built-in NLP

Natural Language Processing (NLP) allows you to understand and extract meaningful information (called entities) out of the messages people send. You can then use these entities to identify intent, automate some of your replies, route the conversation to a human via livechat, and collect audience data.

If you are currently leveraging an NLP API, you have to make an extra call when you receive the user message, which adds latency and complexity (example: async, troubleshooting, etc.). With built-in NLP, entities are automatically detected in every text message that someone sends.

Contents

Default NLP

Once Messenger's built-in NLP is enabled for your Facebook Page, it automatically detects meaning and intent in every text message before it is sent to your bot. The message will be relayed to your bot as usual, along with any entities detected in the body. See Handling a Message With Entities.

Entities

By default, Messenger's built-in NLP detects the following entities:

  • Greetings (English only)
  • Thanks (English only)
  • Bye (English only)
  • Date/Time
  • Amount of money
  • Phone number
  • Email address
  • Distance
  • Duration
  • Quantity
  • Temperature
  • Volume

Date and time are automatically localized based on the locale sent in the user's profile.

For example, if someone sends the message, “tomorrow at 2pm” or “2 days before Xmas,” you will get the actual timestamp with the message.

Language Support

By default, built-in NLP supports the following languages. For other languages, check Customizing NLP via Wit.ai.

LanguageLanguage Code

Chinese (simplified & traditional)

zh, zh-Hans, zh-Hant

Croatian

hr

Danish

da

Dutch

nl

English

en

French

fr

German

de

Irish

ga

Italian

it

Hebrew (modern)

he

Hungarian

hu

Korean

ko

Norwegian Bokmål

nb

Polish

pl

Portuguese

pt

Romanian

ro

Spanish

es

Swedish

sv

Vietnamese

vi

Enabling Built-in NLP

To enable built-in NLP, do the following:

  1. Go to your Facebook app's 'Messenger Settings' page.
  2. Toggle the "on off" switch to enable/disable built-in NLP for your app.

Advanced Settings

Built-in NLP also supports the following advanced settings that let you further customize the nlp object included in messages webhook events. To enable/disable these settings, click the 'Advanced Settings` button:

  • Verbose mode: Returns extra information like the position of the detected entity in the query.
  • N-best: Returns the n-best trait entities as opposed to only the top one.

Enabling Built-in NLP with the Graph API

You can also use the graph API to enable built-in NLP programmatically:

curl -i -X POST \
  -d "access_token=$PAGE_APP_ACCESS_TOKEN" \
  "https://graph.facebook.com/v2.8/me/nlp_configs?nlp_enabled=$NLP_ENABLED"

You must append the nlp_enabled parameter which will either enable or disable NLP for that Page.

Handling a Message With Entities

Once built-in NLP is enabled, you will see an nlp key in the request sent to your message webhook.

For example, the message, "bye, see you tomorrow at 4pm" would include the following entities:

{...,
  entities: {
    "datetime": [
      {
        "confidence": 0.97249440664957,
        "values": ["..."],
        "value": "2017-05-10T14:00:00.000-07:00",
        "grain": "hour","type": "value"
        }
      ],
    "greetings": [
      {
        "confidence": 1,
        "value": "true"
      }
    ]
  }
}

For each message, the Messenger Platform will return a mapping of the entities that were captured alongside their structured data. The key pieces of information here are the confidence and the value for each entity.

confidence is a value between 0 and 1 that indicates the probability the parser thinks its recognition is correct.

value is the parser output. For example, 2pm can be converted to an ISO string you can use in your bot, like "2017-05-10T14:00:00.000-07:00".

You can learn more about the JSON structure of all the entities in the Wit.ai docs

Example Usage

In your message webhook, you can update the logic used to respond to messages by taking advantage of DefaultNLP. For example, if you have a handleMessage() function in your webhook that responds to each message received, you can use the greeting entity to send an appropriate response:

function firstEntity(nlp, name) {
  return nlp && nlp.entities && nlp.entities[name] && nlp.entities[name][0];
}

function handleMessage(message) {
  // check greeting is here and is confident
  const greeting = firstEntity(message.nlp, 'greeting');
  if (greeting && greeting.confidence > 0.8) {
    sendResponse('Hi there!');
  } else { 
    // default logic
  }
}

Replicate this logic for other entities, and you will be on your way to using built-in NLP!

Customizing NLP via Wit.ai

You can customize Messenger's built-in NLP to detect additional entities in English, as well as entities in the 57 languages supported by Wit.ai.

To customize NLP with Wit.ai, do the following:

  1. Go to your app's 'Messenger Settings' page.
  2. In the 'Built-in NLP' section, select one of your subscribed Pages.
  3. Add your Wit Server access token. You can find your access token in the Wit App settings.
  4. Save your settings.

Updating NLP Settings with the Graph API

You can also update your NLP settings programmatically with a POST request to the Graph API:

curl -i -X POST \
  -d "access_token=$PAGE_APP_ACCESS_TOKEN" \
  "https://graph.facebook.com/v2.8/me/nlp_configs?nlp_enabled=$NLP_ENABLED&&custom_token=$CUSTOM_TOKEN"

The following query parameters are supported:

PropertyTypeDescription

nlp_enabled

Boolean

Optional. Specifies whether NLP is enabled for the Page.

model

Enum

Optional. Specifies the NLP model to use. Either a language, or custom. Supported values are:

  • ENGLISH
  • CUSTOM
  • CHINESE
  • DUTCH
  • FRENCH_STANDARD
  • GERMAN_STANDARD
  • ITALIAN_STANDARD
  • POLISH
  • PORTUGUESE
  • ROMANIAN
  • SPANISH
  • VIETNAMESE

custom_token

String

Optional. Wit.ai server token for integrating a custom model.

verbose

Boolean

Optional. Specifies whether verbose mode if enabled, which returns extra information like the position of the detected entity in the query.

n_best

Integer

Optional. The number of entities to return, in descending order of confidence. Minimum 1. Maximum 8. Defaults to 1.