Some background on how NLP works (not required, but interesting):
Go to your settings, and then click "Built-In NLP". For language model, let's default to English, but also include support for Vietnamese.
Remember to set up ngrok again (and change your callback URL) so you can read messages. Now if we look carefully at our incoming messages, we'll see a new section: nlp
.
Let's dig more into this nlp section. Let's log it out to the screen, but let's use a little known method called console.dir
to print out all the different fields:
Send the message "Hello from Hanoi!" or similar, and you'll see output similar to this:
There are some amazing things in this data, that Messenger Platform (with wit.ai) have automatically understood for us.
Spend some time playing around with the bot and observing the output. Try the following scenarios:
In theory, you should receive values for any entity detected from the following list: https://developers.facebook.com/docs/messenger-platform/built-in-nlp/#entities_and_traits, which includes things like quantities, temperatures, volumes, and sentiment.
In this step, try to trigger all of them (in either Vietnamese or English). Here are some examples to get you started:
This built-in NLP is very cool, but a bit limited because as you can see above, only the entities
are being populated. Perhaps we could dig into the intent
. To do so, we'll have to create a custom model.
Click "Create a new Wit app".
After filing out the forms, it's a bit confusing where to go next, but in the bottom right, you'll see "Go to my Wit app"
If you have the option, in the upper right, click "Switch to the New Wit" to arrive at a page that looks like this:
Here, in the previous step, you should have tried many phrases - and not always been successful. Here you can help train the bot to understand what you said before.
For example here I've manually told the bot about 40 Degrees being a temperature and that ha noi is a location.
You can optionally go through and train your bot, but we'll be doing this in a more specific way in the next step.
Note that the following instructions will be for a Chatbot that understands Englihs, but you could do it in Vietnamese instead if you change the setting here:
General NLP is useful but let's specialize around a bot that has a job: to schedule a coffee delivery. To do so, first we'll create an intent. Click "intents" to the left, and then click "+ Intent".
Now, you'll see not much is set here. What we'll have to do is go back one page, and add some utterances.
Go to the bottom, and type in some phrases that someone might type into our coffee bot.
Create a new entity, let's call it drink_type, and have it belong to the coffee ordering intent.
Let's also add a milk coffee, or whatever types of coffee you'd like to offer in your store.
Now let's go back to our server, and test out a message.
My server responds like this:
Ah, this is not 100% correct. It seems that we did not get the time properly from this. That's okay, we can re-train our detected entities.
Actually, it's already been suggested. Just click "add", and click "train and validate".
Now update your bot to respond. First, let's go back and change our handleMessage
function:
And our handleCoffeeOrder
function looks like:
We'll just need to make a quick function called getEntityFromMessage
.
Congratulations! You now have a coffee bot.
One shortcoming of our bot is that it's not very good at asking for more information.
So we'lll need to build in some conversational logic. Doing this properly is beyond the scope of our lab today. Because we are running serverless, we won't be able to persist memory between requests, and it's recommended we use something like Redis to store our state.
The basic code to implement something like this is below: