Webhook is simple HTTP callback. You can think of it as an event-trigger based system. It will perform some actions if certain thing are changed.
npm install express
npm install unirest
server.get('/getName',function (req,res){
res.send('Swarup Bam');
});
here /getName is the URL for the API. Let’s call the API from the postman.
Your Content Goes Here
server.post(‘/getMovies’,function (request,response) {
if(request.body.result.parameters[‘top-rated’]) {
var req = unirest(“GET”, “https://api.themoviedb.org/3/movie/top_rated”);
req.query({
“page”: “1”,
“language”: “en-US”,
“api_key”: “”
});
req.send(“{}”);
req.end(function(res) {
if(res.error) {
response.setHeader(‘Content-Type’, ‘application/json’);
response.send(JSON.stringify({
“speech” : “Error. Can you try it again ? “,
“displayText” : “Error. Can you try it again ? “
}));
} else if(res.body.results.length > 0) {
let result = res.body.results;
let output = ”;
for(let i = 0; i<result.length;i++) {
output += result[i].title;
output+=”\n”
}
response.setHeader(‘Content-Type’, ‘application/json’);
response.send(JSON.stringify({
“speech” : output,
“displayText” : output
}));
}
});
}
}
Note- Every response should contain the speech and displayText property.
We are almost done. Now enable the webhook in fulfillment section in dialogflow console. Also dont 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.