AI Challenge : Facebook location and APIAI - How to get user location from facebook?

Getting location from facebook is not that easy as it used to be back in 2013 or 2014.
Facebook has improved on the privacy of users and it is not possible to get user location without user confirmation.
In regards to fb bots, there will be several cases where you might need user location.
Some business cases
1. You want to sell some products or services which are relevant for the user location, to have the best user experience.
2. You want to know if your user is travelling to some new place and to propose him something for the new place or provide some offers/rewards/promos based on his location.

Here are the steps  and the video for the same
You can choose dailymotion or youtube to view the video below

AI Bots -Get User Location from Messenger using... by himantgupta
1. First , you cannot user location until user confirms and shares the location himself.
2. FB has a way via bot to request user to share his location via Quick replies, and content type location (you need to send the JSON with the relevant text from your server webhook to FB page, using the format for facebook send message Graph API.) If you are using direct integration (without webhook), you need to send custom payload from your AI platform
e.g. APIAI can send direct custom payload. You would need to send something like this.


3. Once you define this custom payload, and user will see send location.


4. Once a person uploads location or selects location in FB, it will come as attachment object in your webhook.

5. Check the attachment, if it is not a URL, it means it is a location. For the images and other attachments, FB generates a URL from Fb, so if there is no URL in attachment it is a location.
Note, to use APIAI code from GitHub webhook and modify it as :
Alternately you can use the updated API.AI code (they have updated it at https://github.com/dialogflow/dialogflow-nodejs-client/tree/master/samples/facebook/src


for(i=0; i<messagingEvent.message.attachments.length; i++)
  
{   
   console.log("Attachment inside: " + JSON.stringify(messagingEvent.message.attachments[i]));    

       
                          var text =             messagingEvent.message.attachments[i].payload.url;


                          //If no URL, then it is a location

                          if(text == undefined || text == "")

                         {
                            text =  "latitude:"

                                      +messagingEvent.message.attachments [i].payload.coordinates.lat
                                      +",longitude:"
                                   +messagingEvent.message.attachments[i].payload.coordinates.long;

                          }



6. Then you can send this text to APIAI client to handle the text.

7. If you want you can make another intent to parse this text and show the latitude and longitude.
e.g. what I am doing is, I am sending the text to APIAI and the following intent is handling it and sending message back to user


Basically until step 6, you will get the coordinates in your webhook.
You can then use them for any further processing.

For more code sample and more info, do not forget to consult us.
We have working code for all the blog posts.

Comments

  1. Hello , thank you for this article. How can i catch the longitude and latitude please ?

    ReplyDelete
    Replies
    1. Hi, I have added steps after 5. Please check for your questions.

      Delete
    2. where does the messagingEvent came from?

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Hi,

      API.AI also added the code recently for it. You can check here https://github.com/api-ai/api-ai-facebook/blob/master/src/app.js
      And check for location. You will understand.

      Delete
  3. Hi, Github link is 404, please fix it. Thank you!

    ReplyDelete
    Replies
    1. Updated. You have to search in repositories. In case you need code and services from us, please let us know

      Delete
    2. https://github.com/dialogflow/dialogflow-nodejs-client/tree/master/samples/facebook/src

      Delete
  4. Hi, can I use [Custom Payload] for [Prompt] of [Required parameter]?
    and how to do that
    Thanks.

    ReplyDelete
  5. Hello,
    1) I am passing custom payload from web hook and deploying in a firebase with dialog flow
    2) Added FACEBOOK_LOCATION event to my intent, but it's not working my intent was not triggering even it's not logging any error message
    Thanks,

    ReplyDelete
  6. Everybody share their experience here and this is truly pleasant to peruse distinctive sort of speculations identified with same theme. Everybody setting their distinctive feeling and it demonstrates differing qualities. Value this stage.
    chatbots

    ReplyDelete
  7. somebody can tell me how to use the app.js on this link https://github.com/dialogflow/dialogflow-nodejs-client/tree/master/samples/facebook/src

    i'm really confuse

    ReplyDelete
  8. AI APIs are game-changers, offering developers powerful tools to integrate artificial intelligence capabilities seamlessly into their applications. From natural language processing to image recognition and sentiment analysis, AI API provide access to sophisticated algorithms that drive intelligent decision-making. Whether automating tasks, personalizing user experiences, or enhancing data analysis, AI APIs empower developers to harness the potential of AI without the need for extensive expertise. With their versatility and accessibility, AI APIs pave the way for innovative solutions that revolutionize industries across the board.

    ReplyDelete

Post a Comment