Skip to content

Commit be60a6b

Browse files
committed
initial commit
0 parents  commit be60a6b

File tree

9 files changed

+186
-0
lines changed

9 files changed

+186
-0
lines changed

CONTRIBUTING

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Contributing to real-time-updates-samples
2+
We want to make contributing to this project as easy and transparent as
3+
possible.
4+
5+
## Pull Requests
6+
We actively welcome your pull requests.
7+
8+
1. Fork the repo and create your branch from `master`.
9+
2. If you've changed APIs, update the documentation.
10+
3. If you haven't already, complete the Contributor License Agreement ("CLA").
11+
12+
## Contributor License Agreement ("CLA")
13+
In order to accept your pull request, we need you to submit a CLA. You only need
14+
to do this once to work on any of Facebook's open source projects.
15+
16+
Complete your CLA here: <https://code.facebook.com/cla>
17+
18+
## Issues
19+
We use GitHub issues to track public bugs. Please ensure your description is
20+
clear and has sufficient instructions to be able to reproduce the issue.
21+
22+
Facebook has a [bounty program](https://www.facebook.com/whitehat/) for the safe
23+
disclosure of security bugs. In those cases, please go through the process
24+
outlined on that page and do not file a public issue.

LICENSE

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Copyright (c) 2015-present, Facebook, Inc. All rights reserved.
2+
3+
You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
4+
copy, modify, and distribute this software in source code or binary form for use
5+
in connection with the web services and APIs provided by Facebook.
6+
7+
As with any software that integrates with the Facebook platform, your use of
8+
this software is subject to the Facebook Developer Principles and Policies
9+
[http://developers.facebook.com/policy/]. This copyright notice shall be
10+
included in all copies or substantial portions of the software.
11+
12+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
14+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
15+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
16+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
17+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Real Time Updates Samples
2+
3+
These are sample clients for [Facebook's Real Time Updates API](https://developers.facebook.com/…/graph-…/real-time-updates/) and [Instagram's Real-time Photo Updates API](https://instagram.com/developer/realtime/).

heroku/README.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Real Time Updates Heroku Sample
2+
3+
This example is a [Hubot](https://hubot.github.com/) script that messages a chat room when a Facebook Page post is published using Facebook's [Real Time Updates API](https://developers.facebook.com/docs/graph-api/real-time-updates/). The message includes a link to the Facebook post. The bot will post another message with the number of likes on the post after a configurable number of minutes.
4+
5+
These instructions assume you have already set up [Hubot on Heroku](https://hubot.github.com/docs/deploying/heroku/).
6+
7+
## Setup
8+
9+
1. Download `real-time-updates.coffee`.
10+
1. Add a line to `hubot-scripts.json` with `real-time-updates`.
11+
1. Create a new [Facebook application](https://developers.facebook.com/apps) and/or register an [Instagram API client](https://instagram.com/developer/clients/manage/).
12+
1. Using `token` as the verify_token, set up your Facebook application's [Real Time Updates subscription](https://developers.facebook.com/docs/graph-api/real-time-updates/#setup) using `https://<your-subdomain>.herokuapp.com/facebook` as the callback URL, and/or your Instagram client's [Real-time Photo Updates subscription](https://instagram.com/developer/realtime/) using your `https://<your-subdomain>.herokuapp.com/instagram` as the callback URL.
13+
1. Set the Heroku configuration values defined at the top of `real-time-updates.coffee` before deploying.
14+
- `FACEBOOK_APP_ACCESS_TOKEN` - [access token](https://developers.facebook.com/docs/facebook-login/access-tokens#apptokens) for your Facebook app
15+
- `REAL_TIME_ROOM` - chat room for Hubot to post in
16+
- `WAIT_MINUTES` - number of minutes to wait before retrieving the number of likes on the post
17+
1. Install the Facebook app on your Facebook Page using the [Page subscribed apps endpoint](https://developers.facebook.com/docs/graph-api/reference/page/subscribed_apps).

heroku/real-time-updates.coffee

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Description:
2+
# Real Time Updates router for Facebook and Instagram.
3+
#
4+
# Configuration:
5+
# FACEBOOK_APP_ACCESS_TOKEN
6+
# REAL_TIME_ROOM
7+
# WAIT_MINUTES
8+
#
9+
# Notes:
10+
# Set up your Real-Time updates as descried here: https://developers.facebook.com/docs/graph-api/real-time-updates/
11+
# And here: https://instagram.com/developer/realtime/
12+
#
13+
# The `verify_token` is "token".
14+
#
15+
# Author:
16+
# adamgross42
17+
18+
module.exports = (robot) ->
19+
20+
robot.router.get ['/facebook', '/instagram'], (req, res) ->
21+
if req.param('hub.mode') == 'subscribe' and req.param('hub.verify_token') == 'token'
22+
res.send req.param('hub.challenge')
23+
else
24+
res.send 400
25+
26+
robot.router.post '/facebook', (req, res) ->
27+
if req.body.entry[0].changes[0].value.verb == 'add' and req.body.entry[0].changes[0].value.item == 'status'
28+
res.send 200
29+
pageId = req.body.entry[0].id
30+
postId = req.body.entry[0].changes[0].value.post_id
31+
robot.http("https://graph.facebook.com/#{pageId}?access_token=" + process.env.FACEBOOK_APP_ACCESS_TOKEN)
32+
.header('Accept', 'application/json')
33+
.get() (err, res, body) ->
34+
body = JSON.parse body
35+
robot.messageRoom "#{process.env.REAL_TIME_ROOM}", "New post on #{body.name} Page: https://www.facebook.com/#{postId.split('_')[1]}."
36+
# Wait some time before pulling post stats
37+
setTimeout () ->
38+
robot.http("https://graph.facebook.com/#{postId.split('_')[1]}/likes?summary=true&access_token=" + process.env.FACEBOOK_APP_ACCESS_TOKEN)
39+
.header('Accept', 'application/json')
40+
.get() (err, res, body) ->
41+
body = JSON.parse body
42+
if body.summary
43+
likes = body.summary.total_count
44+
else
45+
likes = 0
46+
robot.messageRoom "#{process.env.REAL_TIME_ROOM}", "After #{process.env.WAIT_MINUTES} minutes, the Facebook post https://www.facebook.com/#{postId.split('_')[1]} has #{likes} likes."
47+
, process.env.WAIT_MINUTES * 60000
48+
49+
robot.router.post '/instagram', (req, res) ->
50+
robot.messageRoom "#{process.env.REAL_TIME_ROOM}", "New post on Instagram."
51+
res.send 200

parse/.parse.local

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"applications": {
3+
"Real Time Updates Test App": {
4+
"applicationId": "EzGxZBdh063cKPWz0QO6ey2ic5Uts4aWQy0ph9UJ"
5+
},
6+
"_default": {
7+
"link": "Real Time Updates Test App"
8+
}
9+
}
10+
}

parse/.parse.project

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"project_type": 1,
3+
"parse": {
4+
"jssdk": "latest"
5+
},
6+
"email": "[email protected]"
7+
}

parse/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Real Time Updates Parse Sample
2+
3+
This is a sample client for [Facebook's Real Time Updates API](https://developers.facebook.com/docs/graph-api/real-time-updates/) and [Instagram's Real-time Photo Updates API](https://instagram.com/developer/realtime/), powered by [Parse Cloud Code](https://parse.com/docs/js/guide#cloud-code).
4+
5+
## Setup
6+
7+
### Parse
8+
1. Download the files in this directory.
9+
1. Create a new [Parse application](https://parse.com/apps).
10+
1. Set your Parse application's [subdomain name](https://parse.com/docs/js/guide#hosting-choosing-a-subdomain-name).
11+
1. Install the [Parse command line tool](https://parse.com/docs/js/guide#command-line).
12+
1. Run the `parse new` command from the root directory of this repository to initialize the cloud code with your Parse app keys (enter `cloud` for the directory name to use this sample code).
13+
1. Navigate to the `/cloud` directory and run the `parse deploy` command to deploy the Cloud Code.
14+
1. Test your deployment with `curl https://<your-subdomain>.parseapp.com` - you should see "It works!".
15+
16+
17+
### Facebook
18+
1. Create a new [Facebook application](https://developers.facebook.com/apps).
19+
1. Set up your Facebook application's [Real Time Updates subscription](https://developers.facebook.com/docs/graph-api/real-time-updates/#setup) using `https://<your-subdomain>.parseapp.com/facebook` as the callback URL and `token` as the verify_token.
20+
21+
### Instagram
22+
1. Register an [Instagram API client](https://instagram.com/developer/clients/manage/).
23+
1. Set up your client's [Real-time Photo Updates subscription](https://instagram.com/developer/realtime/) using your `https://<your-subdomain>.parseapp.com/instagram` as the callback URL and `token` as the verify_token.
24+
25+
Read more about using Parse objects to save your Facebook data in the [Parse JavaScript Guide](https://parse.com/docs/js/guide).

parse/cloud/main.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var express = require('express');
2+
var app = express();
3+
4+
app.get('/', function(req, res) {
5+
console.log(req);
6+
res.send('It works!');
7+
});
8+
9+
app.get(['/facebook', '/instagram'], function(req, res) {
10+
if (
11+
req.param('hub.mode') == 'subscribe' &&
12+
req.param('hub.verify_token') == 'token'
13+
) {
14+
res.send(req.param('hub.challenge'));
15+
} else {
16+
res.send(400);
17+
}
18+
});
19+
20+
app.post('/facebook', function(req, res) {
21+
console.log('Facebook request body:');
22+
console.log(req.body);
23+
// Process the Facebook updates here
24+
});
25+
26+
app.post('/instagram', function(req, res) {
27+
console.log('Instagram request body:');
28+
console.log(req.body);
29+
// Process the Instagram updates here
30+
});
31+
32+
app.listen();

0 commit comments

Comments
 (0)