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. We’ll cover the following steps:

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. Store Locator API

Now, we’ve got our long and lat value and these values can be used for you own Store Locator API. Have a look at your own API docs to implement this one. Some things to keep in mind during your design:

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

Get StartedContact us