-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.js
56 lines (49 loc) · 1.56 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const express = require("express");
const rp = require('request-promise');
const router = express.Router();
const firebase = require("firebase-admin");
const serviceAccount = JSON.parse(Buffer.from(process.env.FIREBASE64KEY, 'base64'));
firebase.initializeApp({
credential: firebase.credential.cert(serviceAccount),
databaseURL: "https://the-meatball-stoppe.firebaseio.com"
});
// Facebook verification
router.get("/webhook", (req, res) => {
if (req.query["hub.verify_token"] === process.env.FBTOKEN) {
res.send(req.query["hub.challenge"]);
} else {
res.send("No sir.");
}
});
// Change made to TMS FB page :: Update Firebase
router.post("/webhook", (req, res) => {
let options = {
uri: `https://graph.facebook.com/v2.8/790534394301792/feed?fields=permalink_url,from,message,full_picture&access_token=1828570360690824|${process.env.FBAPPSECRET}`,
json: true
};
rp(options)
.then(function(response) {
const postInfo = response.data.filter(post => {
return post.from.name === "The Meatball Stoppe" && post.full_picture;
})[0];
if (postInfo.message) {
postInfo.message = postInfo.message.slice(0, 115) + "...";
} else {
postInfo.message = "";
}
firebase
.database()
.ref("mostRecentFBPost")
.set({
imageURL: postInfo.full_picture,
url: postInfo.permalink_url,
message: postInfo.message
});
res.send(postInfo);
})
.catch(function(err) {
res.send(err);
console.log(err.body);
});
});
module.exports = router;