botframeworkweb-chat

How to capture user_country/city in botframework web channel?


I would like to capture end-user location (country/city) in Application insights form azure botframework chatbot running on web chat channel. App insights instrumentation is already in place page view and custom events being collected, but it seems that the client_City and client_CountryOrRegion are not populated correctly. Is there a specific channel or webservice configuration?


Solution

  • Geolocation data can be accessed using the below method. This will provide you with the longitude and latitude providing the user grants access to this data.

    You will then need another process or library to convert the location data to a physical location in the real world.

    navigator.geolocation.getCurrentPosition( async (position) => {
      const { latitude, longitude } = position.coords;
      console.log(latitude, longitude)
    })
    

    There are some NPM packages that can do some of this for you, as well.

    If you look at my posted answer here, you can see an example I provided on how to get location data for use in generating and displaying a map as a feature of the bot.

    Please note that, in the above answer, the bot is responding to a request to display a map, is sending the activity which is then picked up by Web Chat. Web Chat, upon receiving the request, is then getting the location data and displaying the map.

    In your case, you will want to send the data back to the bot for the bot to then do something with. You can refer to Web Chat's b.piggyback-on-outgoing-activities sample on how to do this.

    Hope of help!