Creating a chatbot which provides information like movie details, news, flight details, weather information requires some external API’s to collect the data. This is achieved through Webhooks.
Let’s see how we can create a webhook to get the Movie details. I assume you have created intents and entities for your chatbot. The first step is to create new nodejs project. After creating the project, you should see the following folder structure.
Now go to the root directory and install the following packages.
Once done, lets create the sample API to test the app.
here /getName is the URL for the API. Let’s call the API from the postman.
As the app is working without any errors, let’s create the container API which will be called by the Dialog flow for every request. Every request has a parameter associated with it. Depending upon the parameter we can call the different API’s. Let’s call the Tmdb API to get the top rated movies.
/getMovies is the root level API which Dialog flow will invoke upon receiving a request from the user. I am using Unirest library to handle the HTTP calls. You can use any library which is suitable for you. If-else statements are used to handle the error. If the request is successful and API has returned the response, we can send back the customized response to the user through speech and displayText properties.
Parameter detection is all depends upon how well you have trained your chatbot. For example, to detect the “top-rated” parameter from user input, I have trained my agent with following user expressions.
We are almost done. Now enable the webhook in fulfillment section in dialogflow console. Also don’t forget to enable the webhook call for each intent. To enter the URL for webhook, you need to host the app on the server. I have hosted my app on Heroku. After hosting the app, append“/getMovies” at the end of URL. For example,