Real-time Update - facebook

I really hope this is not a naive question but does Facebook real time updates work on user's status updates? if so can someone point me in the right direction because I can't seem to confirm this anywhere.

You can't subscribe to these user connections yet: home, tagged, posts, photos, albums, videos, groups, notes, events, inbox, outbox, updates, accounts
http://developers.facebook.com/docs/reference/api/realtime/

It's called long-polling. Look up node.js and long-polling ajax concepts.
Essentially the client makes an initial ajax request and waits for a response with no timeout set. Server-side JS will trigger an event when the user updates his/her status and send the response to any JS clients awaiting said response. The information gets updated, and immediately the client sends another request and waits for the next event.

You can subscribe to the 'feed' connection of your app users, which then also triggers on status updates.
URL = 'https://graph.facebook.com/%s/subscriptions' % appid
data = {'object': 'user',
'fields': 'feed',
'callback_url': callback,
'verify_token': verify_token,
'access_token': access_token,
}
res = requests.post(URL, params=data)
print res.content
print res.ok
The access_token must be an application access token, not a user access token. The above request will subscribe you to updates of all of your users! The message sent by the Facebook server will only include the user ID of the user, though, and no details about what happened. You have to figure that out yourself.

Related

Facebook Graph API - message a user that has blocked a page

What (if any) will be the result of the following use-case:
I have a FB page and I'm sending (private) messages back and forth with another FB user.
The user blocks my page.
(1) What will happen if I send a message to the user ? I guess the API will return a message with a certain HTTP code ?
Here it states that
https://developers.facebook.com/docs/messenger-platform/error-codes
You might also receive this error if a customer has blocked your business from messaging him or her.
(2) What happens if I keep sending messages to the user which has my page blocked ? Will FB see this as some kind of violation, or just send back same response ?
The FB graph API is as usual very hard to navigate.
Looking at various FB API pages, still not clear what could happen.
https://developers.facebook.com/docs/graph-api/reference/page/blocked
https://developers.facebook.com/docs/messenger-platform/reference/send-api
https://developers.facebook.com/docs/messenger-platform/error-codes

Facebook Messenger Platform - Webhook Subscription

I have followed the steps to setup the Facebook Messenger platform. The verification GET web hook request work perfectly, as does the subscribe but when I submit the chat I keep getting the follow Developer Alert:
Hi Norah,
We've noticed that your Webhooks subscription for callback URL https://{domain}/v1/webhook has not been accepting updates for at least 16 minutes. Please verify that your callback server is functioning so you may continue to receive updates. If you need to update your callback URL, see https://developers.facebook.com/docs/messenger-platform/webhook-reference#webhook_setup
If your callback URL continues to fail to accept updates for 8 hours straight, we will disable your subscription. To reactivate the subscription, make a POST request with the same parameters, and it will be reactivated.
My post request works through POSTMAN.
Please can someone help me! This is driving me nuts!
Do you have logs on your server for that post requests?
Facebook requires you to return status code 200 for the post request, so they know that you successfully received it. When they havent, they try it again and if that still fails after several times, they will give you this alert.
Maybe facebook uses another content-type or message content than you used with postman.
Your server logs should give you more insights about that.
Depending on what Webhook events that you have subscribed for a page, there will be callbacks for those events, and more, on the url you have specified in the Web Hook set up.
If you had subscribed to the message_deliveries event, every time a message is sent, whether from a user to your page or from your page to a user, there is a, maybe more, calllback with a Message Delivered json object. The Webhook Reference has an example of the Message Delivered json object, but no specification or explanation on what the fields mean.
Occasionally I find that an undocumented Read callback is received, sometimes. The undocumented json data for this is like:
{"object":"page",
"entry":[
{
"id":"1722858134648129",
"time":1465407550812,
"messaging":[
{
"sender":{"id":"1131485883560113"},
"recipient":{"id":"1722858134642218"},
"timestamp":1465407550868,
"read":
{
"watermark":1465407548057,
"seq":428
}}]}]}
Essentially, you must code your callback to handle ALL types of json data gracefully, including unknowns, even though you may not be ready to process them further. For those that you are not ready to handle or uninterested in, return nothing with Http status code 204 (in fact every callback should return 204 as the type is void).
If you handle only those types of json data you are interested in, any unexpected json data will most likely raise an exception in whatever language your web callback code is written in and result in a 500 server error returned to Facebook. It is this 500 error that is causing Facebook to make that complaint in your question.

"Ask for gift" Facebook request without server

I want to implement "ask for help" feature in my game (Facebook Unity SDK). If user A asks user B for help, and user B accepts, user A will get a gift.
My game is a single player experience right now so we have not created a server. Is there anyway to know if user B has accepted the request when User A logs in again next?
The solution here involves storing information in a database:
send Facebook request and get a gift FB API
Not really, you must delete your request on accepting, so request can only be in pending state.
You can remember ID when you send request to a player, and check what pending requests exist, and if you don't find your ID there, you know request was either accepted or rejected, but that probably doesn't help.
You will need to store request data somewhere. You can use service like parse.com, where you can just push data from client without having to do much work server-side. Parse is free until your game gets big and after that 200$ should not be too much.

Is there a reliable way to record user initiated facebook apprequests?

It seems like the only way to do apprequests from user to user is via a dialogue. It also seem like I would need to do a ajax post with the invite ids to my server to record them happening. It's a bit unreliable.
Is there a way where I can get a list of the requests without needing to do a post?
You can only list pending requests by calling /me/apprequests on the user. Any other requests that were processed, you need to save them at the initial point of the request.

get all outstanding user to user requests with only sender user_id and access token?

I tried $facebook->api('/me/apprequests/'); and that was blank even with 2 outstanding requests sent to 2 different friends. Nothing shows on graph explorer either.
What I need is a way to get this information without knowing the request ID, because I need to add this information to my database after they send a user to user request, to prevent them from sending more than one request to a friend per day. I know other apps do this, but I guess they aren't using FB.ui and probably record that info while sending the request?
There is no way to get requests that user sent via apprequests connection of user object, only those user received.
You can read the apprequests sent to a user by your app by issuing an HTTP GET request to /USER_ID/apprequests with user access_token.
You should store information about requests sent by application on your end if you need that info.