Part 2: Build an Action to make Google Assistant find the food you are craving for!

Vijay Nandwani
Espresso Labs
Published in
3 min readSep 13, 2018

--

In Part 1, we prepared our intent and entity. Now it is time to create the custom webhook for our Find My Food Action.

Implementing a custom webhook

The webhook can be implemented in two ways:

  1. Through inline editor that is powered by cloud function (serverless architecture)
  2. By writing your own functions in the backend (requires a server)

For our case, we have chosen option 2 because we will be using Zomato’s API for finding restaurants for the particular food and in order to use any external API in cloud functions, one needs to pay $25. So let’s save ourselves some money and use our own server.

In the Fulfillment section, you can put the URL to your server and receive the post request with the parameters mentioned.

Defining custom webhook with Node.js

Let’s take a deep dive into the code. Let’s say the user said ‘I want to eat Manchurian’, Manchurian would be extracted and sent as a post request to the backend. Our primary aim is to find the current location of the user because a user in Delhi won’t be interested in Manchurian available in Mumbai.

if(request.body.originalDetectIntentRequest.payload.device)

The line above checks if we have the device information or not because that’s where our location is. In case we don’t have the information, we have to ask the user for the location and that’s what our else statement does (we invoke the getLocation() function). Note that line #6–7 in the above code is for extracting Lat and Long from the device location.

Let’s explore Line 16–21 in depth with which we are instructing the Assistant to ask the user for their exact location(in lat and long). We are invoking a system intent which is of type-permission and asks the user for DEVICE PRECISE LOCATION to serve the user better.

Now notice line #17

“intent” : “actions.intent.PERMISSION”

To better understand how this works, let’s understand what is Helper Intent. Webhooks can use helper intents to obtain values such as user information to personalize the responses (basically fulfillment calls a helper intent when it wants the Assistant to handle part of a conversation). This returns frequently-requested information such as the user’s name and location.

Create a new Intent as shown below and enable webhook call in the fulfillment section.

What’s next after getting location and dish from the user?

Now that we have the location of the user and the dish user wants to eat today, lets hit the Zomato API and find out the list of nearest restaurants that will serve the required dish!

Results are then converted to speech

That’s it, we are done! Let’s see how it looks in Assistant.

This application is live for Google Assistant and you can try it out on your phone. Here is the link to the application. Here is a demo video:

Demo of Find my food action

We can also provide an additional feature in the app of asking details for a particular restaurant from the suggestions given. It’s simple:

  • Add an entity named Restaurant
  • Provide 1–2 restaurant names and open it for automated expansion
  • Make relevant changes in your index.js and you are done. You can find the entire code here.

What next?

I believe that you were able to follow the tutorial till now and have the application running. In case something wasn’t clear or you need help with something, please post a comment down below or write to us at support@espressolabs.in.

Here are some features which you can try to add yourself to improve your understanding of the Assistant Ecosystem.

  • A user might as the action to tell more suggestions
  • A user might want to modify the radius of the search
  • A user might want to sort the results by cost, rating or distance
  • A user might want to see the menu of the place or get its contact details

--

--