Links

Find Nearest Location

With this template, your customers can easily find your business' nearest branch by simply asking your chatbot! After asking the user for their address, the bot will look through an external database (Airtable) which contains the data for all your branch locations. Then it will calculate which location is closest to the user’s address by using the Google Maps API. Finally, the bot will show the result on Google Maps, within an iFrame.
Keep reading to learn how you can customise this template for your own organisation.

How to customise this template

Make sure to always update the NLP after loading a template!
This template uses two third party services:
  1. 1.
    Airtable, as a database for branch locations
  2. 2.
    Google Maps API, for finding the user address
To let your bot interact with these services you will need an API key for each and configure these in the Action dialogs where the bot makes the API calls.`

Setting up your own Airtable base

If you have not worked with Airtable before, you can sign up for free here and learn the basics here.
For each of your organisation's locations, your Airtable will need fields with the following information:
  1. 1.
    Name of the branch
  2. 2.
    Address
  3. 3.
    Latitude
  4. 4.
    Longitude
When you’ve newly imported the 'Nearest Location' template, it will automatically link to this Airtable. Feel free to use it for testing, but make sure to replace it once you start using the bot for your own organisation!
For more information on how to integrate Airtable into your bot, check out this tutorial.
Managing your branch locations in another database? You can link any type of database to this bot, as long as it has an API. Read more about our API integrations here.
​
Below is an overview of the key dialogs in each flow of the bot. Here we will walk you through how to configure the connection with the Airtable and Google Maps APIs, as well as some other tips to customise this template for your business.

Flow: General

Dialog: Ask address

In this dialog, the bot asks the user for their address and saves the response as a variable: user_address.

Dialog: Locator

To make this template work, your bot needs to be able to access the location data of your branch locations (in Airtable) and look up the user’s address (using the Google Maps API).
This dialog uses a Code Action to call both the Airtable API and the Google Maps API with the Javascript code shown below. This code may look a bit intimidating, but don’t worry, you only have to change a few pieces of information!
const allStations = await fetch(`https://api.airtable.com/v0/(YOUR_BASE_ID)/(YOUR_TABLE_NAME)`, {
headers: {
Authorization: `Bearer (YOUR_AIRTABLE_API_KEY)`
}
}).then(r => r.json());
let re = / /g;
​
const currLoc = args.user_address.replace(re, '+');
let minDistance = Number.MAX_SAFE_INTEGER;
let nearestShop;
const chatlayer = ChatlayerResponseBuilder(); chatlayer.addSessionVariable('currLoc', currLoc);
const addressFromMaps = await fetch(`https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=${currLoc}&inputtype=textquery&fields=geometry&key=(YOUR_GOOGLEMAPS_API_KEY)`).then(r=> r.json());
const geoLocation2 = {
Latitude: addressFromMaps.candidates[0].geometry.location.lat,
Longitude: addressFromMaps.candidates[0].geometry.location.lng,
}
//const geoLocation2 = {Latitude : "52.5001278", Longitude : "13.3121555"}; allStations.records.forEach(station=>{
const geoLocation1 = station.fields;
const lat = geoLocation1.Latitude - geoLocation2.Latitude;
const lon = geoLocation1.Longitude - geoLocation2.Longitude;
const val = Math.sqrt(Math.pow(lat, 2) + Math.pow(lon, 2)); if(minDistance>val){
minDistance = val;
closestLocation = geoLocation1;
}
});
​
closestLocation.Address = closestLocation.Address.replace(/ /g, ' ');
chatlayer.addSessionVariable('closestLocation', closestLocation);
chatlayer.addSessionVariable('addressFromMaps', addressFromMaps); chatlayer.send();----------------------------------
In this piece of code, you should customise the following three things:
  1. 1.
    The Airtable link In the url on line 1 of the code block, replace (YOUR_BASE_ID) with the ID of your own Airtable base and (YOUR_TABLE_NAME) with the name or ID of the table that contains your location data. You can find your Airtable's base and table IDs here, or in the URL of your table view.
  2. 2.
    The Airtable API key In the url on line 3, replace (YOUR_AIRTABLE_API_KEY) with your own Airtable API key. You can find the API key on your Airtable account page.
  3. 3.
    The Google Maps API key On line 13, replace (YOUR_GOOGLEMAPS_API_KEY) with your own Google API key.

Dialog: Give address

In this dialog, the bot shows the name and address of the closest location found in the database. The name is stored in the closestLocation.Name variable and the address in closestLocation.Address.

Dialog: Show location on map

In this dialog the bot shows the closest branch location on a map, within an iFrame. In the Source Field of the iFrame action, also replace (YOUR_GOOGLEMAPS_API_KEY) with your own Google API key.
The Show location on map dialog with the part of the source URL to change highlighted in red

Dialog: Clear user address

Finally, in this dialog the bot clears the variable user_address so the user can start the flow again and use another address.

Connect the flow to other flows

Now that you've added this new flow, make sure it connects to other flows. For example, if this is an important use case for your bot, make sure to mention it in the introduction.

​

That's it, now your bot can find locations nearest to the user's address! 👏