Getting user location from Facebook

If this is your first visit, be sure to check out the FAQ by clicking the link (Sorry, coming soon!). You may have to register before you can post: click the register link to proceed.

To start viewing and posting messages, select the forum that you want to visit from the selection below.  Welcome to the forums!

7 posts / 0 new
Last post

Getting user location from Facebook

This quick tutorial will show you how to get user location data from a Facebook User AND assign a street address from Google Maps

First off, when a user sends data to the chat bot containing a location from Facebook, you will receive JSON data. In order for the user to send location data, they have to click on that cool little pin (you know, the drop pin icon). The data will look a lot similar to the following when it hits your webhook:

array(2) {
  ["object"]=>
  string(4) "page"
  ["entry"]=>
  array(1) {
    [0]=>
    array(3) {
      ["id"]=>
      int(1151635421536343)
      ["time"]=>
      int(1463265031987)
      ["messaging"]=>
      array(1) {
        [0]=>
        array(4) {
          ["sender"]=>
          array(1) {
            ["id"]=>
            int(1159277174102575)
          }
          ["recipient"]=>
          array(1) {
            ["id"]=>
            int(1151635421536343)
          }
          ["timestamp"]=>
          int(1463265031923)
          ["message"]=>
          array(3) {
            ["mid"]=>
            string(36) "mid.1463265031752:07d973ce3f3f276421"
            ["seq"]=>
            int(3531)
            ["attachments"]=>
            array(1) {
              [0]=>
              array(4) {
                ["title"]=>
                string(24) "What Sweet Hell is This?"
                ["url"]=>
                string(322) "https://www.facebook.com/l.php?u=https%3A%2F%2Fwww.bing.com%2Fmaps%2Fdefault.aspx%3Fv%3D2%26pc%3DFACEBK%26mid%3D8100%26where1%3DSouth%2BBurlington%252C%2BVermont%26FORM%3DFBKPL1%26mkt%3Den-US&h=0AQHaPVgX&s=1&enc=AZPoRntntMTJ6k2Iq9g9uBLH3IFFNySg98jLJEy81cg7zr_ttunOt4DSO9CNdowHocRGLViscMsczdSyYJqtedi8_PT2aHMZgP9FLfAIWDtkuQ"
                ["type"]=>
                string(8) "location"
                ["payload"]=>
                array(1) {
                  ["coordinates"]=>
                  array(2) {
                    ["lat"]=>
                    float(44.4493674)
                    ["long"]=>
                    float(-73.1594363)
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}


With this data, we can then grab the longitude and latitude of the current user by grabbing the data from the returned JSON.
For example, to get our longitude and latitude from this returned data, we can do something like this:

<?php
//Read Input from Facebook
$input = json_decode(file_get_contents('php://input'), true);
//If input contains coordinates data, do something with it
if ($input['entry'][0]['messaging'][0]['message']['attachments'][0]['payload']['coordinates'])
{
//The name of the location sent
$userLocationTitle = $input['entry'][0]['messaging'][0]['message']['attachments'][0]['title'];
//The Latitude of the location sent
$userLat = $input['entry'][0]['messaging'][0]['message']['attachments'][0]['payload']['coordinates']['lat'];
//The Longitude of the location sent
$userLong = $input['entry'][0]['messaging'][0]['message']['attachments'][0]['payload']['coordinates']['long'];
}
?>


So now that we have latitude and longitude data, wouldn’t it be nice to get a bit more accurate with an address, or a city and state, or anything besides numbers? Well, we can, using Google Maps API.

<?php
$url
= 'https://maps.googleapis.com/maps/api/geocode/json?&latlng=' . $userLat . ',' . $userLong;
$content = file_get_contents($url);
$json = json_decode($content, true);
?>


Then to get the name of the city for example, we can do the following:

<?php
$userCity
= $json['results'][0]['address_components'][2]['long_name'];
//To get a complete list of address components returned, try this:
var_dump($json);
?>


Hope this is helpful when trying to get your location information from the user in Facebook Messenger. The last thing you want to do is have the user leave the experience just to provide a location. The address information is relatively accurate, and might fall apart in a big city only because of street numbers not being 100% accurate; but this is a great starting point to getting your location information from your users. So next time you need location data, make sure to ask them to hit the location button then press send; presto, you have location data!

Vote up!
Vote down!

Points: 0

You voted ‘up’

Thats actually a cool tutorial. Really very informative.

So, is there any possible way by which inspite of ' user is typing ' we can detect and view the keys typed when the user is typing the message ?

Vote up!
Vote down!

Points: 0

You voted ‘up’

At the moment, when a user is typing (at least with Facebook) the API allows for programmers to see when a message was sent, as well as read. They lack the ability to see real time character typing though, and I can imagine the type of load it would put on the Facebook servers if every single message was sent in realtime character by character. Maybe some day Facebook may open up the API a bit more, but until then there are limited options for this behavior

Vote up!
Vote down!

Points: 0

You voted ‘up’

it only shows location from the server provider. Like spain everyone is in Madrid or Barcelona....So you never know where somebody realy is???

Vote up!
Vote down!

Points: 0

You voted ‘up’

No, this returns the data which you explicitly provide at the time of request for location. It's up to the end user to provide truthful location data, which ultimately provides a better user experience.

In my own testing, I found between the returned latitude and longitude data, along with Google Maps API, I was able to pinpoint locations with the correct address about 80% of the time. The other 20% it returned the address of someone within 10 feet of the location provided. So it's highly accurate, not simply based on IP addresses.

Vote up!
Vote down!

Points: 1

You voted ‘up’

Nice

Vote up!
Vote down!

Points: 0

You voted ‘up’

it seems very fascinating but only it tells the place or the exact location if it tells the exact location then it would be amazing

Recent Posts

Video Marketing for Business: Here is what the experts are saying If you are an entrepreneur in this era, you... Read more
Brand Yourself for Web Marketing It's time to talk about the feature that will set you apart from your competition... Read more
Time to Make your Presence on the Web Felt: How Much you Should invest? You know what you want to... Read more

Need Support?


Every video, every website, every chatbot, every client - 100% custom products and solutions to make your business run successfully

145 Pine Haven Shores Road #1205 Shelburne, VT

Recent Tweets

  • This needs to end... Senseless violence all for what? https://t.co/mNMP0uMnJN 2 weeks 10 hours ago
  • RT : My warmest condolences and sympathies to the victims and families of the terrible Las Vegas shooting. God bless you! 2 weeks 10 hours ago
  • RT : Michelle & I are praying for the victims in Las Vegas. Our thoughts are with their families & everyone enduring another senseless tragedy. 2 weeks 10 hours ago
  • RT : Las Vegas update: - At least 400 wounded taken to hospitals, per police - Gunman found dead in hotel room https://t.co/5EOndn2tkj 2 weeks 10 hours ago
  • RT : Keep us in your thoughts - its a very difficult and tragic evening for a lot of people ,… https://t.co/mf7x29rD9Y 2 weeks 10 hours ago

Socialize with us

               


Go to top