How to create an ordering chatbot with simple NLP

Updated on
June 30, 2023
Table of content

A few weeks ago my team finished our first chatbot. It is a chatbot for a small restaurant of healthy food in Santa Monica, which allows users to order food directly from Facebook Messenger.

Because the chatbots are a quite new topic, you might think that creating a chatbot is some kind of rocket science. It can definitely be if you are going to create a chatbot that is able to talk on different topics, generate human-like messages and do other complicated NLP stuff. But is this kind of functionality necessary for such narrow purposes as ordering goods from a shop or a restaurant? Of course, you definitely need NLP to parse user’s requests which are related to your shop, e.g. search by products, getting recommendations or fetching some details about a product. I would like to share my experience and some practices that we used during the development.

Check out how to build your own AI chatbot

Model and order flow

At first, let us define a model of the chatbot. After a user starts a conversation the bot should introduce itself, tell the user about functionality and show the restaurant’s menu and motivate to make an order.

Bot introduction
Bot introduction

Then the customer selects a category in a menu and picks a dish from this category.

The user may also want to get more info about a dish before adding it to the cart. Or your product may have some options that the user can choose from. For example, if it is a pizza, you may create a custom pizza or add some extras to a standard one. There are two ways to provide these options: ask them in the chat (but it can be uncomfortable if there are a lot of options to choose) or use a Web view from Facebook API. Facebook Messenger provides Web view API to open a web page inside Messenger, you can use it to create a user-friendly interface to set up a dish. Do not forget to provide an ability to check the cart, change and remove items there.

create a chatbot

And the last step of the ordering flow is the payment part. Facebook Messenger provides an API to make payments via Messenger, so you don’t need to ask the user’s credit card info and do some other insecure things. You can connect Stripe or PayPal account to your Facebook Page, or just use tokenized customer’s card credentials and make payment on your own. But now Facebook Messenger payment API is in Beta and it is available only in the US. It can also request some user info that you may need, such as phone number, email and shipping address. But if the delivery price depends on the shipping area, you should probably ask for the shipping info before the checkout, and check manually if you deliver to this area and how much it costs.

Web view and Payment view
Web view and Payment view

Woohoo, the order is done! Show some confirmation message.

ordering chatbot

More details

Facebook has a very easy and clear API for Messenger, so I think there is nothing to explain about the coding part. Just keep your code clean, separate interactions with API from the core of your chatbot to let it be scalable, do not hardcode any text (move it to config), etc.

But the main problem is how to store your data to make search and recommendations better. At first, in the menu, besides the basic dish’s name, price, and description, you will probably need the ingredients and some tags that describe the dish (e.g. fried, spicy, vegetarian etc). It can help handle a more complex user’s request, like “I want some spicy with the chicken”. But the problem is to “parse” the sentence, and we will definitely need NLP for it.


Let’s add some intelligence to the chatbot. We want to understand the user’s intent (e.g. search in a menu or get recommendations), highlight the dish’s name, specifications and avoid some unnecessary words in the user’s message. And we need to use NLP for this. But is it necessary to implement NLP algorithms on our own? No. Actually, for our purposes probably the best way will be to use some NLP API. Here are a couple of examples:

All of them have almost the same functionality, but there is some difference in usage. Just read through the API docs and choose the one you like more. I chose because at that time had a limited number of requests per month, but now this limit is gone.

Before the magic NLP service starts working we should train it. Go to the “Train” or “Understanding” section and create as many different examples of sentences that a user may ask your chatbot, like: “Can you recommend me something sweet”, “Give me smoothie”, “I would like a drink with bananas” etc. And you should show to the service where are the entities in the sentence. Use synonyms, different sentence constructions to get better results. training interface training interface

Let’s talk more about entities. Entities are the useful info that you want to get from the message. The basic entity is an intent. Probably all kind of sentences should have an intent. It describes an action that should be done. But there are other informative entities. For example, in case of searching the menu, besides the intent (search in this case), you may need a search key, like a dish name or category name or dish description. If there are a lot of examples of some entity, like dish’s names in the menu, you can easily upload them via API of your NLP service.

API will only parse the message, but it is not enough to get a response for the user’s request. You need to do an action from the intent. In case of search, you need to find a dish or dishes that suit the search request the most. The user can write the dish’s name not as it is in the menu, make some typos, etc. Anyway, you should provide some good response to the user. So for this purpose, the simplest what we can do is to use a string distance algorithm (find similar strings). I have tested the ones that should work the best. They were Jaro-Winkler, Ratcliff-Obershelp, QuickSilver, Damerau-Levenshtein. And Ratcliff-Obershelp showed itself the best for my purposes. Because it puts closer the strings which have more common subsequences, unlike the other algorithms where distance is based on the number of changes that need to be done to obtain one string from another. For example, if the dish is called “Super organic healthy smoothie” and the user writes just “Smoothie”, Ratcliff-Obershelp puts them closer than other ones. Also, a good idea might be to count the distance between words and then use the mean or max or any other metric that suits you. So this way you have found the most relevant items in the menu, now just send them to the user. But it is one of the simplest ways, in a more complex situation you may need some NLP algorithm to parse user’s messages.

UX advice

Besides the technical side, making your bot user-friendly really matters. Here is some advice:

  1. Do not try to convince the user that he is talking to a real human. Let him understand that there is a machine on the other side. Do not try to lie.
  2. Do not leave the user alone. Always give him some next steps.
  3. Keep it clear. Make it clear for the user what he should do on each step.
  4. You can use pictures or gifs. It is fun!
  5. Handle the cases when the bot does not know what to answer, provide some Help. Feel free to ask the user to rephrase or just offer him some default actions.

I hope this article was useful for you and you got a better understanding of the chatbots!