In order to complete a conversation it is usually best to ask the user to confirm their input, and once they are happy with the result, push the data to your backend or trigger some other process outside of the chatbot domain.
Entities confirmation is a crucial step that brings in a lot of value to the conversation:
There is a special type of a step, entities-confirmation
, which handles all of the above functionality in a simple, but elegant fashion.
Add a confirmation step to the rent-car
conversation, which should ask the user to confirm the startDate, endDate, city, car and email entities. Like this:
rent-car
conversationentities-confirmation
step => start typing sten and select step-entities-confirmation
. You should get the following code
{
"type": "entities-confirmation",
"entity": "result-entity-name",
"entities": [
"entity-name"
],
"messages": [
"Confirm entities?"
]
}
entities
arraymessages
to display a message in the following three lines:
The whole step should look like this:
{
"type": "entities-confirmation",
"entity": "result-entity-name",
"entities": [
"startDate",
"endDate",
"city",
"car",
"email"
],
"messages": [
[
"Are these the correct details?",
"Press on any of the items you wish to update.",
"Press \"Confirm\" to complete the transaction."
]
]
}
Now you should be able to test the confirmation step in the rent-car
conversation.
Try the following steps:
Once the user completes the conversation and confirms their choice, all you are left with is to take the data collected in the conversation and send it to your backend.
This would usually mean triggering a process to execute on the agreed transaction. For example:
In order to send the data to your backend, you can use a webhook
step. Its purpose is simple, execute an HTTP command described in the data-source
property and display the provided message once that is complete.
Here is what the step-webhook
code snippet generates:
{
"type": "webhook",
"data-source": {
"endpoint": "https://",
"method": "POST",
"headers": {
"header name": "header value"
},
"payload": {
"key": "value"
}
}
}
A Webhook Step behaves just like a Message Step in the sense that the Conversation Flow Algorithm will execute it every time it steps over it. So, if you follow the pizza order webhook step with a satisfaction question, your customer might get charged twice and will receive two pizza orders.
This can be easily avoided. You can add an entity
property, which is used to store the result returned from executing the webhook. You can use the entity content to display it to the user (more on that in the bonus section below). But in this case, the Conversation Flow Algorithm will see that the webhook already contains a result, and therefore it will not execute it again.
To finalize the car renting conversation, you need to send the Order to the backend, and then provide the user with a confirmation message.
For the purposes of the Car Rental service, there is a Cloud Function called book-car, that allows you to complete the booking process.
book-car can be reached with the following parameters:
"endpoint": "https://api.demo.nativechat.com/v1/car-rental/book-car",
"method": "POST",
"headers": {
"Authorization": "Basic ZGVtby11c2VyOmRlbW8tcGFzc3dvcmQ="
},
The booking details should be provided as the request body (payload
in the chatbot configuration). For example:
{
"pickUpDate": "24 Mar 2019",
"dropOffDate": "30 Mar 2019",
"car": "Smart",
"price": "200",
"city": "Berlin",
"email": "me@email.com"
}
In the real-world application, the book-car function should perform some extra validations, save the new order and trigger additional processes (like an email confirmation, etc.).
In the context of this tutorial, book-car only returns a unique booking reference, i.e. { bookingRef: BER01234 }
In order to complete the transaction with a call to the book-car cloud function, follow these steps:
rent-car
conversationwebhook
step => start typing stw
data-source
to call book-car
(see the configuration above),payload
, use the following fields
pickUpDate
-> startDate
dropOffDate
-> endDate
car
-> car
price
-> car.price
city
-> city
email
-> email
(although you can ignore this field, as it is not used)
Additionally, you should format the dates, so that they are passed to the backend in a specific format. This can be done with the $date
formatter, like this: {{$date startDate 'D MMM YYYY'}}
.
data-source
should look like this:
"data-source": {
"endpoint": "https://api.demo.nativechat.com/v1/car-rental/book-car",
"method": "POST",
"headers": {
"Authorization": "Basic ZGVtby11c2VyOmRlbW8tcGFzc3dvcmQ="
},
"payload": {
"pickUpDate": "{{$date startDate 'D MMM YYYY'}}",
"dropOffDate": "{{$date endDate 'D MMM YYYY'}}",
"car": "{{car}}",
"price": "{{car.price}}",
"city": "{{city}}",
"email": "{{email}}"
}
},
entity
property (preferably, just before the data-source
configuration) => start typing en and select entity
and call
it order-response
. This will allow you to capture the response returned by book-car.messages
array (preferably, at the end of the step configuration) => start typing me and select messages
.Solution
The whole step should look like this:
{
"type": "webhook",
"entity": "order-response",
"data-source": {
"endpoint": "https://api.demo.nativechat.com/v1/car-rental/book-car",
"method": "POST",
"headers": {
"Authorization": "Basic ZGVtby11c2VyOmRlbW8tcGFzc3dvcmQ="
},
"payload": {
"pickUpDate": "{{$date startDate 'D MMM YYYY'}}",
"dropOffDate": "{{$date endDate 'D MMM YYYY'}}",
"car": "{{car}}",
"price": "{{car.price}}",
"city": "{{city}}",
"email": "{{email}}"
}
},
"messages": [
"Your car is booked. Your booking reference is {{order-response.bookingRef}}"
]
}
Test
Now you should be able to test the full conversation rent-car
conversation.
Try the following steps:
And just like that, you have created a fully functioning chatbot. It can communicate using Natural Language Processing, communicate with the backend to display data, validate user input and save the transaction to the backend.
There is still more to master. You can learn more from the NativeChat documentation.
As a bonus, here is the final homework for you, which should use a lot of the knowledge that you’ve learned in this tutorial.
Before booking the car, you should:
To get a quote you should call the get-quote cloud function.
get-quote can be reached with the following parameters:
"endpoint": "https://api.demo.nativechat.com/v1/car-rental/get-quote",
"method": "POST",
"headers": {
"Authorization": "Basic ZGVtby11c2VyOmRlbW8tcGFzc3dvcmQ="
},
It expects the body to be like:
{
"car": "Smart"
"pickUpDate": "2023-01-22T00:00:00Z",
"dropOffDate": "2023-01-27T00:00:00Z"
}
As a result, it returns something like this:
{
"days": 5
"price": 200
}
Here is a high-level guide on how to complete this challenge.
Note, that you should be adding the solution just after the entity-confirmation step, and before the book-car webhook step.
Before you display the quote. You need to add a webhook step, that we'll call get-quote.
Make sure to add the entity
property (you can call it quote
), which you will use to display the result later. Like: {{quote.days}}
.
For a message, you could add something like: Preparing a quote, but that is not necessary.
After that, add a confirmation type step.
Make sure to call the entity
property something meaningful like wantsToBook
.
It should display a message like:
Here is the summary of your order:
Car: Smart
Price per day: $50
5 day(s): from 10-Mar-2019 to 15-Mar-2019
Total price $250
Would you like to go ahead and book it?
The number of days you can get from quote.days
(or whatever you called the webhook entity), and the total price from quote.price
.
Also, note that you should use $currency and $date formatters to display the data in the desired format.
Add a message step with a condition checking the entity form the confirmation step is false. (i.e. check if wantsToBook
equals false or is not true).
If you don’t remember how, you can go back to the comparison chapter, or check the docs.
The message should be something like: I understand. I will cancel the quote now.
Update the book-car Webhook step. Add a condition, so that it executes when the confirmation entity is true (i.e. check wantsToBook
is true)
Test it, you know the drill.
Solution
I hope that you completed the solution all by yourself, and you are here to just compare the results. Either way, here are the three new steps, and the updated book-car step.
GetQuote Step
{
"type": "webhook",
"entity": "quote",
"data-source": {
"endpoint": "https://api.demo.nativechat.com/v1/car-rental/get-quote",
"method": "POST",
"headers": {
"Authorization": "Basic ZGVtby11c2VyOmRlbW8tcGFzc3dvcmQ="
},
"payload": {
"car": "{{car}}",
"pickUpDate": "{{$date startDate 'D MMM YYYY'}}",
"dropOffDate": "{{$date endDate 'D MMM YYYY'}}"
}
}
},
Confirmation Step
{
"type": "confirmation",
"entity": "wantsToBook",
"messages": [
[
"Here is the summary of your order:",
"Car: {{car}}",
"Price per day: {{$currency car.price 'USD'}}",
"{{ quote.days }} day(s): from {{$date startDate 'D MMM YYYY'}} to {{$date endDate 'D MMM YYYY'}}",
"Total price {{$currency quote.price 'USD'}}.",
"Would you like to go ahead and book it?"
]
]
},
Message Step
{
"type": "message",
"messages": [
"I understand. I will cancel the quote now."
],
"conditions": [
"{{$not wantsToBook}}"
]
},
Webhook BookCar step
{
"type": "webhook",
"entity": "order-response",
"data-source": {
"endpoint": "https://api.demo.nativechat.com/v1/car-rental/book-car",
"method": "POST",
"headers": {
"Authorization": "Basic ZGVtby11c2VyOmRlbW8tcGFzc3dvcmQ="
},
"payload": {
"pickUpDate": "{{$date startDate 'YYYY-MM-DDTHH:mm:ssZ'}}",
"dropOffDate": "{{$date endDate 'YYYY-MM-DDTHH:mm:ssZ'}}",
"car": "{{car}}",
"price": "{{car.price}}",
"city": "{{city}}",
"email": "{{email}}"
}
},
"messages": [
"Your car is booked. Your booking reference is {{order-response.bookingRef}}"
],
"conditions": [
"{{wantsToBook}}"
]
}
Congratulations on reaching the end of the tutorial and building your first chatbot with NativeChat! Even though this is just the beginning, the foundational knowledge you have gained will help you along your journey. You can further your knowledge by checking out the official NativeChat documentation. Moreover, you’re welcome to take a close look at the NativeChat Accelerators, which can help you get started even faster!
Get started today