Back to Blog

Create a location-aware chatbot and find nearest store locations

location-aware chatbot that finds nearest location

Is your chatbot able to handle tasks based on your customer or client’s location? No? Then it’s time to take your bot to the next level. Imagine a virtual assistant on WhatsApp or Facebook Messenger that is able to tell your customer where the closest repair service, servicepoint or collectionpoint is.

In this knowledge base article we will demonstrate how to use the location of the user to find the nearest store. To find a store or location that is closeby, we will need two things:

  1. Location or Address of the User
  2. Store Locator API

1. Location or Address of the User

Within you can use the Location trigger and an Any Text trigger to obtain input that we can use. Even though sharing a location is cool, some users prefer entering an address as they don’t want to share their current location. Therefore, it is recommended to use both options.

As most store locators work with the latitude and longitude as input, we need to convert a shared location or an address to a long and lat value.

Use location or address to find a store


When using the Location trigger you will receive the lat and long values. Read more in our location docs or use the code below to create an Action and convert the location of the user to a lat, long value.

async payload => {
  var lat = "-"
  var long = "-"
  // If the user shares location
  if(Array.isArray(payload.params.location)) {
    lat = payload.params.location[0]
    long = payload.params.location[0].value.long


A second option to obtain the long and lat value is use an address entered by the user. But how do we convert that into a long and lat value?

A fairly simple solution is to use the Google Maps Geocoding API. Have a look at our article about using the Google Maps Geocoding API or use the code below and add it to your existing Action.

  if(Array.isArray(payload.params.address)) {
    try {
    const {
    } = payload

    const { address } = params

    const lastAddress = address[address.length - 1].value

    // For this example we show the API key in code
    // but best practice is to use Configuration
    const key='YOUR SECRET API KEY'

    // Construct a Google API URL
    const url=encodeURI(`${lastAddress}&key=${key}&language=${user.profile.locale || 'en'}`)

    // Call the API
    const { data } = await request(url)
    const { status, results } = data

    lat = results[0]
    long = results[0].geometry.location.lng

  } catch(err) {
    // This is not good..
    reply(new Message('Here you will find the closest stores:'))

2. Locate the nearest store

Now, we’ve got our long- and latitude values, and these values can be used to find a location near the user. At this point you’ve got 2 options: use an existing API of your company or calculate the distance to the location yourself.

Some things to keep in mind during your design:

Store Locator API

Some retailers already have a store locator API in place. If that’s the case you should be able to make an API call to that service and include the user lat- and longitude. A store locator API usually returns an array of available locations that need to be mapped to a text reply.

Calculate distance within

Not all companies have a Store locator API in place. No worries we can calculate the distance in the same Action as it allows you to create advanced logic.

To calculate the distance between 2 objects you will need the lat- and longitude of both objects. A simple database that contains locations as rows would be sufficient here. We can retrieve locations with their long- and latitude and calculate the distance between the user and the location by using the function below. For reference have look at geodatasrouce.

    // Function to calculate distance
  function distance(lat1, lon1, lat2, lon2, unit) {
    var radlat1 = Math.PI * lat1/180
    var radlat2 = Math.PI * lat2/180
    var theta = lon1-lon2
    var radtheta = Math.PI * theta/180
    var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
    if (dist > 1) {
        dist = 1;
    dist = Math.acos(dist)
    dist = dist * 180/Math.PI
    dist = dist * 60 * 1.1515
    if (unit=="K") { dist = dist * 1.609344 }
    if (unit=="N") { dist = dist * 0.8684 }
    return dist

Map result to text reply

After having an array of location sorted on distance to the user, we can map these results to text reply.

  // Map sorted array of objects to reply

  // Fallback default
  var message = (`To find a location have a look at`)
  // Bij gevonden locaties
  if (arrOfObj.length > 0) {
      message = (`These are the closest locations:`)
      for (var i in arrOfObj) {
        message += `\n ${arrOfObj[i]["City"]}, ${arrOfObj[i]["address"]}`

  reply(new Message(message))

Covering any edge cases

location-aware chatbot that finds nearest location

Your API, both the Google API and your Store Locator API might work while testing but what do you do:

In the example code above we simply return a text message with a link. With that link the user is still able to find the nearest store him or herself.

Read more

Request demo