Location Data in Facebook Messenger for your PHP Chatbot

Blaine's picture
Location Data is sent from Facebook using JSON. You can get a user location in your Chatbot by reading this JSON response and referencing the location data.

For the purposes of this blog, we are going to assume you are using a PHP chat bot of some sort. We make this assumption only because this is what we are using. If you are using a different programming language, please feel free to adapt the concept to your purposes. 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
(
    [object] = page
    [entry] = Array
        (
            [0] = Array
                (
                    [id] = 1277646242287507
                    [time] = 1486167927861
                    [messaging] = Array
                        (
                            [0] = Array
                                (
                                    [sender] = Array
                                        (
                                            [id] = 1172503962826652
                                        )

                                    [recipient] =Array
                                        (
                                            [id] =1277646242287507
                                        )

                                    [timestamp] =1486167927798
                                    [message] = Array
                                        (
                                            [mid] = mid.1486167927798:f83acfdf63
                                            [seq] = 88403
                                            [attachments] = Array
                                                (
                                                    [0] = Array
                                                        (
                                                            [title] = What Sweet Hell is This?
                                                            [url] = https://l.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%2B05403%26FORM%3DFBKPL1%26mkt%3Den-US&h=ATNV1arshSGYNFIcHNC5dqpS4YV6hpUVxwgDli-3dlYct8EfNi4UCnTZioY89d0nGgMZQDIQoVb15675icbUO6s2iOYrrBL0sHJW43ZAN3x3AcIi-sB2bUCS_6Le5KY1lI8pAWU&s=1&enc=AZO19aNb9ivsVlHvtwrR3UUVQjEinerLag8E15DDl2KK-FtU5Byk1s9NWYylfiywMIo_F2DQ5YVmKN38Vp6w06kR
                                                            [type] = location
                                                            [payload] = Array
                                                                (
                                                                    [coordinates] = Array
                                                                        (
                                                                            [lat] = 44.449223662
                                                                            [long] = -73.159705385
                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

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:

//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.

$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:

$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!

Recent Posts

Your Brand is Out: It’s You Vs. The World Now! Your brand site is up, and you are aware of... Read more
It’s been a long road, but we are almost set. We have made some tough decisions, but we have finally... Read more
Going Social with Your Brand: Building Online Presence Facebook, Twitter, Instagram, Tumblr, LinkedIn, YouTube, Reedit, Pinterest, Google+, Ask.fm. You probably... 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

Socialize with us

               


Go to top