Retrieve User ID of Facebook App Invitor - facebook

In the context of a given Facebook app, suppose User A invited user B to start using it. Once User B accepts to use the app, is there any way to retrieve the ID of User A programmatically (via either PHP/JS SDK) ? This doesn't seem quite documented.
For what it's worth, A/B users are friends, if it's any use.

when user comes following the app request, you can get request id's using
$_GET['request_ids']
then retrieve all the request ids with which you can call graph api to get the corresponding request details like below:
if(isset($_GET['request_ids']))
{
$request_ids = $_GET['request_ids'];
}
$request_ids = explode(",", $request_ids);
foreach($request_ids as $request_id)
{
$request_object = $facebook->api($request_id);
//this $request_object have sender facebook id in the field uid_from
}

If you look here:
http://developers.facebook.com/docs/reference/dialogs/requests/
You can see the object layout. Of note is the data property:
Optional, additional data you may pass for tracking. This will be
stored as part of the request objects created. The maximum length is
255 characters.
In this object you can add your referring UserId and then when the request is claimed, you can then process it on your end.
Hope this helps.

Related

Facebook Server-Side API - push custom events to create Custom Audience

We try to use Server-Side API to push custom events to our pixel in order to create event-based Custom Audiences in Facebook Ads (https://developers.facebook.com/docs/marketing-api/facebook-pixel/server-side-api/)
We use _fbp cookie value to match users (it's a first party cookie created on our website by FB pixel).
For example (Python):
from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adspixel import AdsPixel
my_app_id = 'X'
my_app_secret = 'X'
my_access_token = 'X'
my_pixel_id = 'X'
FacebookAdsApi.init(access_token=my_access_token, app_id=my_app_id, app_secret=my_app_secret)
fields = []
params = {
'data': [{'event_name': 'icrm_test_20191113_fbp_1m', 'event_time': 1573230217, 'user_data':{'fbp': 'fb.1.1558571054389.1098115397'}}]
}
print(AdsPixel(my_pixel_id).create_event(fields=fields, params=params))
The problem is, when we create a Custom Audience in Facebook Ads, the size of the list is always < 1000, even if we push hundreds of thousands of cookie IDs, which means Facebook matched a very low % of cookies, which were sent.
Custom Audience definition based on server-side event:
List size is always <1000, no matter how many fbp cookies are sent:
It seems like there is some kind of an issue matching _fbp cookies to Facebook user profiles. Is there any known way of improving/fixing matching results? We can't use hashes of sensitive data.
External_id matching (https://developers.facebook.com/docs/marketing-api/facebook-pixel/server-side-api/parameters) also gave us similar results.
The event time you are using there translates to 11/08/2019 # 4:23pm (UTC).
So they're not gonna get included in your 30 day window.
Try importing time
import time
Then set
'event_time': int(time.time())

Getting list of friends with koala and fb api

Im trying to return the friend list with uid of an authenticated user. however I only get a partial return value, some part of the friends are just left out:
graph = Koala::Facebook::API.new(fb_token)
friends = graph.get_connections("me", "friends")
#some friend action
when I type in the
friends.paging["next"]
in the browser it also returns an empty json array
"data": [ ]
How am I doing it wrong and what is the correct practice?
You need to get the permission 'user_friends' to get friendlist, otherwise you'll receive a empty data:
The field 'friends' is only accessible on the User object after the user grants the 'user_friends' permission.
Try yourself:
https://developers.facebook.com/tools/explorer?method=GET&path=me%2Ffriends&version=v2.5
So, with correct permission you can do:
graph = Koala::Facebook::API.new(fb_token)
result = graph.get_connections('me', 'friends')
to paginate:
next_page = result.next_page
LAST BUT NOT LEAST:
Only friends who installed this app are returned in API v2.0 and
higher. total_count in summary represents the total number of friends,
including those who haven't installed the app.Learn More
You just try this..
This is very simple to you can get all friend count in a single line :)
#graph = Koala::Facebook::API.new(facebook_token)
#friends = #graph.get_connection("me", "friends",api_version:"v2.0").raw_response["summary"]["total_count"]

Facebook Private Messaging

It is said, that it is not possible to initiate new conversation through the API alone, except using Facebook's own Form integrated in the app. Is this correct, or is there some new API, which enables me to initiate a new conversation?
To reply to an existing conversation, I retrieved the conversations id using the following FQL Query "SELECT thread_id, . WHERE viewer_id={0} AND folder_id=0". Afterwards I retrieved the PageAccessToken for my app page using my user Access token, and tried to use this call:
*You can reply to a user's message by issuing an HTTP POST to /CONVERSATION_ID/messages with the following parameters [conversation id, message]. A conversation ID look like t_id.216477638451347.*
My POST Call looked like this (this is not a valid thread id): /t_id.2319203912/messages with message parameter filled. But it always said "Unknown method". Can you help me out with this one? Is there a parameter missing? I passed in the page's Access Token to call this one.
Is there some API out (except Facebook's Chat API), that I am missing, which can send private messages to users?
Edit:
What I wonder about is, that the code below only returns a single page, the application's page. Is this correct, or is there another page token required? This is what bugged me the most about the returned page.
The FacebookClient uses my UserToken to perform the next following task.
This is the code to retrieve my Page Access Token:
dynamic pageService = FacebookContext.FacebookClient.GetTaskAsync("/"+UserId+"/accounts").Result;
dynamic pageResult = pageService.data[0];
_pageId = pageResult["id"].ToString();
return pageResult["access_token"].ToString();
Now the code to retrieve my ConversationÍd:
dynamic parameters = new ExpandoObject();
parameters.q = string.Format("SELECT thread_id, folder_id, subject, recipients, updated_time, parent_message_id, parent_thread_id, message_count, snippet, snippet_author, object_id, unread, viewer_id FROM thread WHERE viewer_id={0} AND folder_id=0", FacebookContext.UserId);
dynamic conversations = FacebookContext.FacebookClient.GetTaskAsync("/fql",parameters).Result;
The following code is executed using the access token retrieved from the code above (page access token request).
Now the Code used to send the reply:
dynamic parameters = new ExpandoObject();
parameters.message = CurrentAnswer;
string taskString = "/t_id." + _conversationId + "/messages";
dynamic result = FacebookContext.FacebookClient.PostTaskAsync(taskString,parameters).Result;
return true;
I also tried it with facebook's graph API Debugger using the token, which is returned by my first part of code. But with the same error message.

Facebook Request Dialog with data

I read this article.
So, I tried it and I put a number in the data property.
FB.ui({
method: 'apprequests',
message: 'Come join me and play at MyWebSite!',
data: '12345',
redirect_uri: 'myWebSite'
});
I get the request_ids, but how do I get the data part (the 12345 number)?.
on server side, you can do something like:(using php here)
$request_ids = $_GET['request_ids'];
$request_ids = explode(",", $request_ids);
foreach($request_ids as $request_id)
{
$request_object = $facebook->api($request_id);
if(isset($request_object['data'])) $req_data = $request_object['data']; //$req_data will be '12345' as per your request data set.
// after getting the data, you may like to delete the request.
$full_request_id = $request_id."_".$fbid; //$fbid is current user facebook id
$facebook->api("$full_request_id","DELETE");
}
Did you try Facebook's documentation too?
https://developers.facebook.com/docs/requests/ has more documentation; if a data parameter was added in the call to the requests dialog, the same value should also be there when requesting the Request details via the API (i.e. a call to /REQUEST_ID)
See the facebook developer site documentation for more details
http://developers.facebook.com/docs/reference/dialogs/requests/
Note:
data:Optional, additional data you may pass for tracking. This will be stored as part of the request objects created. The maximum length is 255 characters.

How can I construct a link to view a message on facebook.com if I have the message id

I am retrieving the messaging inbox with the following graph api call:
https://graph.facebook.com/me/inbox?access_token=...
It returns an array of messages that each have an identifying "id" (same as message id as returned by FQL)
I would like to be able to provide a link for the user to view a message directly on Facebook.com.
Something like: https://www.facebook.com/messages/?action=read&tid=....
That is the link you get to if you you browse to a message from facebook itself.
However I can't seem to find a way to discover the correct tid from either the Graph API or FQL.
I haven't had any luck in figuring out an alternative URL either.
This url used to work, but is broken for me now: http://facebook.com/?sk=messages&tid=...
It just redirects to the top level messaging page: https://www.facebook.com/messages/
ANY IDEAS?
Thanks so much
From graph API get to me/inbox the result set gives id as part of each object returned. This is also thread_id.
If the id you have is a string object (probably a guid), this is from Facebook's older message system storage structure. Now they've updated to a new storage structure that requires the old ones to be migrated into the new
So you have a fairly easy check:
If thread id is a long (Int64/BigInt), then you have a new thread and can use
http://www.facebook.com/messages/?action=read&tid=id.THREAD_ID
If thread id is a string then you have a older thread and can use
http://www.facebook.com/messages/?action=read&tid=THREAD_ID
many programming languages have their own form of checking the type of a value.
var threadId = (string)data.thread_id;
var longVal = 0L;
var isLong = Int64.TryParse(threadId, out longVal);
var threadUrl = (isLong) ?
"http://www.facebook.com/messages/?action=read&tid=id." + threadId :
"http://www.facebook.com/messages/?action=read&tid=" + threadId;