hjelga
(Hjelga)
December 4, 2019, 1:45pm
1
Can someone tell me the best solution on getting user’s location from website running in socket.io
Would be best doing it from website asking for permission on getting location.
We have done this by getting the location by react-geolocator but are stuck on how to get the information to the running action server
btotharye
(Brian Hopkins)
December 10, 2019, 12:10pm
2
I haven’t done it myself by first thoughts are maybe the google location/maps API or some javascript package you can run in your chat widget/etc and get the location details and then send to your action server doing lookups based on it, etc.
I’m not the best at it but that is how I would probably approach it.
sterpapi
(Ster Papi)
March 8, 2020, 12:48am
3
Have you done this finally? Or anyone else maybe?
mlevin2
(Marshall Levin)
March 15, 2020, 7:15pm
4
I found an example at tutorial-rasa-google-assistant/actions.py at master · RasaHQ/tutorial-rasa-google-assistant · GitHub but it seems like this would fetch the location of the Rasa server, not the client, since the action code would run on the server. Has anyone found a good way to get the client’s location when using the embedded web client?
1 Like
hey @sterpapi , I had done in my chatbot widget you can check the code here:
//====================================== Get User Location ==================================================
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(getUserPosition, handleLocationAccessError);
} else {
response = "Geolocation is not supported by this browser.";
}
}
function getUserPosition(position) {
response = "Latitude: " + position.coords.latitude + " Longitude: " + position.coords.longitude;
console.log("location: ", response);
//here you add the intent which you want to trigger
response = '/inform{"latitude":' + position.coords.latitude + ',"longitude":' + position.coords.longitude + '}';
$("#userInput").prop('disabled', false);
send(response);
showBotTyping();
}
1 Like
mlevin2
(Marshall Levin)
March 16, 2020, 6:24pm
6
@JiteshGaikwad thanks! So this code could just be run as the first thing when the user launches the chatbot?
I’m assuming that if I wanted to use Google’s geolocation API I could just swap that in for navigator.geolocation.getCurrentPosition
.
Yes, but you need to send a custom payload from your Rasa Bot which will prompt user for the location access.
Sample Code:
## if we latitude & longitude entities are found, set it to slot
if(latEntity and lonEntity):
lat=latEntity
lon=lonEntity
## if user wants to search the best restaurants in his current location
if(user_locationEntity or (latEntity and lonEntity) ):
##check if we already have the user location coordinates stoed in slots
if(lat==None and lon==None):
dispatcher.utter_message("Sure, please allow me to access your location 🧐")
dispatcher.utter_custom_json({"payload":"location"})
return []
else:
locationEntities=zomatoApi.getLocationDetailsbyCoordinates(lat,lon)
location=locationEntities["title"]
city_id=locationEntities["city_id"]
entity_id=locationEntities["entity_id"]
entity_type=locationEntities["entity_type"]
## store the user provided details to slot
SlotSet("location", locationEntities["title"])
I hope this helps
peterkahan
(peter kahan)
October 12, 2023, 3:29pm
8
To get the user’s location from a website running on Socket.io , it’s best to request permission from the website using a geolocation API (like the browser’s built-in geolocation or a library like react-geolocator) and then send the location data to your action server via a Socket.io event.