Facebook Private Messaging - facebook

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.

Related

Facebook private replies response doesn't give user_id

I use the Private Replies API call to send a message in Messenger to someone commenting on a Facebook post. If the doc says I should get back the message id and the user_id, I only get the message id in response:
body:
'{"id":"m_ZpLkUdJGAoJ9WCpeROAvWJaHycesSPGVPmoNH0SXTUB8WYqRqHl0ru0y3mniMP3q7YL9rl1lfuQr0x9fyNP
Here is my API call code in ruby:
url = "https://graph.facebook.com/v3.1/" + #comment_id + "/private_replies?message=" + #trigger_message + "&access_token=" + #page_access_token
uri = URI.parse(url)
https = Net::HTTP.new(uri.host,uri.port)
https.use_ssl = true
request = Net::HTTP::Post.new(uri, initheader = {'Content-Type' =>'application/json'})
resp = https.request(request)
Why don't I get the user_id?
Facebook private replies API was supposed to return App scoped ID or ASID, though post the new app verification or around that time, they have stopped giving ASID into private reply API - most probably because they are heading towards returning Page scoped ID or PSID (PSID Migration)
Though you can still get ASID from either :
a. The webhook that could be providing comment ID, it should be providing ASID as sender_id
b. from field with parsing comment ID. This is used to parse single comment node, you can find more info here
Do keep in mind ASID to PSID migration...

Get data of a post using ID provided by facebook realtime-api

I am using Facebook realtime api, as I get only the updates and have to fetch the whole data by hitting the server again.
I have a page, my app added to that hence I am getting page feed(like, comment, post, all).
When any user posts on the page, we get the update from Facebook realtime update api. But when I try to fetch post data using the Koala gem it gives me error, note that error is not in case of Update from Page itself(page admin) but when some other user posts on it.
Following is the code for help :-
Trying to fetch using long lived page-token, and without that too, failing both ways
##graph = Koala::Facebook::API.new ACCESS_TOKENS["facebook"]["page_token"]
##public_graph = Koala::Facebook::API.new
JSON response from facebook :-
{"object"=>"page",
"entry"=>
[{"id"=>"123412341234234",
"time"=>1412341234,
"changes"=>
[{"field"=>"feed",
"value"=>
{"item"=>"post",
"verb"=>"add",
"post_id"=>123412341234123,
"sender_id"=>1234123412}}]}]}}
##public_graph.get_object("123412341234123")
*** Koala::Facebook::ClientError Exception: type: GraphMethodException, code: 100, message: Unsupported get request. [HTTP 400]
##graph.get_object("123412341234123")
*** Koala::Facebook::ClientError Exception: type: GraphMethodException, code: 100, message: Unsupported get request. [HTTP 400]
Please help me out to understand how to fetch the public post data using the post_id provided by the realtime-updates api of facebook.
Q: how to fetch the public post data using the post_id provided by the realtime-updates api of facebook.
For fetching public data of the post from the page, you will need to specify both the IDs (page id as well as post id) you are getting in the RT hit form fb.
You will need to pass id as <page_id>_<post_id>. In your case, it will be:
rt_hit = {"object"=>"page",
"entry"=>
[{"id"=>"123412341234234",
"time"=>1412341234,
"changes"=>
[{"field"=>"feed",
"value"=>
{"item"=>"post",
"verb"=>"add",
"post_id"=>123412341234123,
"sender_id"=>1234123412}}]}]}}
entry = rt_hit["entry"].first // you may want to have loop instead of `first`
public_id = "#{entry['id']}_#{entry['changes'].first['value']['post_id']}"
##public_graph.get_object(public_id) // fetch object

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.

Retrieve User ID of Facebook App Invitor

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.

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;