Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
Added Facebook config API methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
tkornblit committed Jul 12, 2016
1 parent 1b603e6 commit f448e9a
Showing 1 changed file with 162 additions and 19 deletions.
181 changes: 162 additions & 19 deletions lib/Facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ function Facebookbot(configuration) {
facebook_message.access_token = configuration.access_token;

request({
method: "POST",
json: true,
headers: {
"content-type": "application/json",
method: "POST",
json: true,
headers: {
"content-type": "application/json",
},
body: facebook_message,
uri: 'https://graph.facebook.com/v2.6/me/messages'
},
body: facebook_message,
uri: 'https://graph.facebook.com/v2.6/me/messages'
},
function(err, res, body) {


Expand Down Expand Up @@ -215,6 +215,149 @@ function Facebookbot(configuration) {
return facebook_botkit;
};

facebook_botkit.persistentMenu = function(cb) {

var buttons = configuration.persistent_menu && configuration.persistent_menu.buttons;

if (buttons) {
request({
uri: "https://graph.facebook.com/v2.6/me/thread_settings?access_token=" + configuration.access_token,
json: true,
method: "POST",
headers: {
"content-type": "application/json",
},
body: {
"setting_type": "call_to_actions",
"thread_state": "existing_thread",
"call_to_actions": .map(function(button) {
return {
"type": "postback",
"title": button.title,
"payload": button.payload
}
}): []
}
}, function(err, res, body) {

if (err) {
facebook_botkit.error(err);
cb(err);
} else {
facebook_botkit.log(body.result);
cb();
}

});
} else {
return cb();
}

};

facebook_botkit.greetingText = function(cb) {

var greetingText = configuration.greeting && configuration.greeting.text;

if (greetingText) {

request({
uri: "https://graph.facebook.com/v2.6/me/thread_settings?access_token=" + configuration.access_token,
json: true,
method: "POST",
headers: {
"content-type": "application/json",
},
body: {
"setting_type": "greeting",
"greeting": {
"text": configuration.greeting.text
}
}
}, function(err, res, body) {

if (err) {
facebook_botkit.error(err);
return cb(err);
} else {
facebook_botkit.log(body.result);
return cb();
}

});

} else {
return cb();
}

};

facebook_botkit.greetingPayload = function(cb) {

var payload = configuration.greeting && configuration.greeting.payload;

if (payload) {

request({
uri: "https://graph.facebook.com/v2.6/me/thread_settings?access_token=" + configuration.access_token,
json: true,
method: "POST",
headers: {
"content-type": "application/json",
},
body: {
"setting_type": "call_to_actions",
"thread_state": "new_thread",
"call_to_actions": [{
"payload": payload
}]
}
}, function(err, res, body) {

if (err) {
facebook_botkit.error(err);
return cb(err);
} else {
facebook_botkit.log(body.result);
return cb && cb();
}

});
} else {
return cb()
}

};

facebook_botkit.reactivateFacebook = function(cb) {

request.post("https://graph.facebook.com/v2.6/me/subscribed_apps?access_token=" + configuration.access_token,
function(err, res, body) {

if (err) {
facebook_botkit.error(err);
return cb && cb(err);
} else {

var isSuccess = JSON.parse(body).success;

if (isSuccess) {

facebook_botkit.log("Successfully subscribed to Facebook events");

return cb && cb();

} else {
facebook_botkit.error("Could not subscribe to page messages.");
return cb("Could not subscribe to page messages.");
}

}

});

};

facebook_botkit.setupWebserver = function(port, cb) {

if (!port) {
Expand All @@ -224,7 +367,7 @@ function Facebookbot(configuration) {
throw new Error('Specified port is not a valid number');
}

var static_dir = __dirname + '/public';
var static_dir = __dirname + '/public';

if (facebook_botkit.config && facebook_botkit.config.webserver && facebook_botkit.config.webserver.static_dir)
static_dir = facebook_botkit.config.webserver.static_dir;
Expand All @@ -233,27 +376,27 @@ function Facebookbot(configuration) {

facebook_botkit.webserver = express();
facebook_botkit.webserver.use(bodyParser.json());
facebook_botkit.webserver.use(bodyParser.urlencoded({ extended: true }));
facebook_botkit.webserver.use(bodyParser.urlencoded({
extended: true
}));
facebook_botkit.webserver.use(express.static(static_dir));

var server = facebook_botkit.webserver.listen(
facebook_botkit.config.port,
function() {
facebook_botkit.log('** Starting webserver on port ' +
facebook_botkit.config.port);
if (cb) { cb(null, facebook_botkit.webserver); }
if (cb) {
cb(null, facebook_botkit.webserver);
}
});


request.post('https://graph.facebook.com/me/subscribed_apps?access_token=' + configuration.access_token,
function(err, res, body) {
if (err) {
facebook_botkit.log('Could not subscribe to page messages');
} else {
facebook_botkit.debug('Successfully subscribed to Facebook events:', body);
facebook_botkit.startTicking();
}
});
facebook_botkit.reactivateFacebook();
facebook_botkit.greetingText()
facebook_botkit.greetingPayload();
facebook_botkit.persistentMenu();
facebook_botkit.startTicking();

return facebook_botkit;

Expand Down

0 comments on commit f448e9a

Please sign in to comment.