Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Find Device Location in an Alexa Skill and a Google Home Action

DZone's Guide to

Find Device Location in an Alexa Skill and a Google Home Action

Learn how to use the device location in an Alexa Skill, a Google Home action, or a Facebook Messenger bot and use it to locate the closest restaurant using an API.

· IoT Zone ·
Free Resource

In this tutorial, we will learn how to use the device location in an Alexa Skill, a Google Home action, or a Facebook Messenger bot, and use it to locate the nearest restaurant using Yelp Fusion API.

If you follow the steps as they’re outlined, you will have a deployed, working “Find Nearby Resturants” app.

Create a Yelp Fusion Account

The Yelp Fusion API allows you to get the best local content and user reviews from millions of businesses across 32 countries.

  1. If you don’t already have a Yelp developer account, sign up. If you have an account, log in.
  2. Create a new app at https://www.yelp.com/developers/v3/manage_app. Save the “API Key” we will need it later.

Create a Conversational App

A Conversation.one application represents your conversational interface through Amazon Alexa, Google Home, or Facebook Messenger bot. To create an app:

  1. If you don’t already have a Conversation.one account, sign up. If you have an account, log in.
  2. Click on Create New App in the navigation. If it’s your first application, you will be redirected to the Create New App wizard automatically.
  3. Select your industry and enter your company name. For the purpose of this tutorial, select “Other” for the industry and enter “Find Restaurants” as your company name. Click Next to continue.
  4. Select the devices you want to support with your app. For this demo, select Messenger, Amazon Alexa, Google Home, and Chatbot. Click Next to continue.
  5. When asked “What will the user ask your bot?” enter “Find Restaurant” and click Next to continue.
  6. Select Code response, and enter the code:
var apiKey = "YourYelpAPIKey";
const yelp = require('yelp-fusion');
const client = yelp.client(apiKey);

return sdk.GetDeviceAddress(context, req, res).then(function(address) {
    var searchOptions = {
        term: 'restaurant'
    }
    if (address.long != "" && address.lat != "") {
        searchOptions.longitude = address.long;
        searchOptions.latitude = address.lat;
    } else if (address.FullAddress != "") {
        searchOptions.location = address.FullAddress;
    } else if (address.Street != "" && address.City != "") {
        searchOptions.location = address.Street + "," + address.City;
    } else {
        res.say("Soory, I could not locate your address. Repeat your question using a full address.");
        return;
    }
    return client.search(searchOptions).then(response => {
        var text = "I found " + response.jsonBody.businesses[0].name + ", located at ";
        text += response.jsonBody.businesses[0].location.address1 + "," + response.jsonBody.businesses[0].location.city + ".";
        res.say(text);
        var text = response.jsonBody.businesses[0].location.address1 + "," + response.jsonBody.businesses[0].location.city + ".";
        text += "
";
        text += "Phone: " + response.jsonBody.businesses[0].phone;
        res.card({
            type: "Standard",
            title: response.jsonBody.businesses[0].name,
            text: text,
            image: {
                smallImageUrl: response.jsonBody.businesses[0].image_url,
                largeImageUrl: response.jsonBody.businesses[0].image_url,
            }
        });
    });
}).catch(e => {
    console.log(e);
});

The code above is running the following steps:

  1. Find the user’s address using the functionGetDeviceAddress.

  2. Use the best option for the location search — GPS (log/lat), full address, or street and city.

  3. Call the Yelp Fusion API using yelp-fusion client

  4. Return the result both as text and as a card.

Your new application is ready. Click Customize to complete your app.

Add Entities

Entities are powerful tools used for extracting parameter values from natural language inputs. We will use entities in this tutorial to allow the user to provide a specific address, instead of the device configured address.

  1. Open the FindRestaurant intent.
  2. Add a new entity named “Address” with type “Address.”

Add More Samples

  1. Replace the existing “Users sais” sample with “(where is|what is|what’s) the (closest|nearest|nighest) (restaurant) ( | to {Address})”
  2. Click Save

Try it Out

Now that your app can return the nearest restaurant to the user, try out what you have so far.

In the chat console on the right, type in a request. The request can be a little different than the examples you provided in the User Says section. This can be something like “where is the closest restaurant “. After you type the request, hit “Send”.

The chatbot will answer with the details of the closest restaurant. For example “I found DishDash, located at 190 S Murphy Ave, Sunnyvale.”

Try now with a specific address. For example “where is the closest restaurant to 1 Market St, San Francisco?” The chatbot will answer with the details of the closest restaurant. For example “I found Hog Island Oyster Co, located at 1 Ferry Bldg, San Francisco.”

What’s Next?

You can use the Amazon Alexa and Google Home deployment wizard to deploy your new application on the different platforms.

Topics:
alexa skills development ,google home actions ,iot ,iot development

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}