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

Promisify Facebook messenger profile API #1300

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 119 additions & 77 deletions lib/Facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -559,161 +559,203 @@ function Facebookbot(configuration) {
'text': payload
});
}
facebook_botkit.api.messenger_profile.postAPI(message);
return facebook_botkit.api.messenger_profile.postAPI(message);
},
delete_greeting: function() {
facebook_botkit.api.messenger_profile.deleteAPI('greeting');
return facebook_botkit.api.messenger_profile.deleteAPI('greeting');
},
get_greeting: function(cb) {
facebook_botkit.api.messenger_profile.getAPI('greeting', cb);
return facebook_botkit.api.messenger_profile.getAPI('greeting', cb);
},
get_started: function(payload) {
var message = {
'get_started': {
'payload': payload
}
};
facebook_botkit.api.messenger_profile.postAPI(message);
return facebook_botkit.api.messenger_profile.postAPI(message);
},
delete_get_started: function() {
facebook_botkit.api.messenger_profile.deleteAPI('get_started');
return facebook_botkit.api.messenger_profile.deleteAPI('get_started');
},
get_get_started: function(cb) {
facebook_botkit.api.messenger_profile.getAPI('get_started', cb);
return facebook_botkit.api.messenger_profile.getAPI('get_started', cb);
},
menu: function(payload) {
var messege = {
persistent_menu: payload
};
facebook_botkit.api.messenger_profile.postAPI(messege);
return facebook_botkit.api.messenger_profile.postAPI(messege);
},
delete_menu: function() {
facebook_botkit.api.messenger_profile.deleteAPI('persistent_menu');
return facebook_botkit.api.messenger_profile.deleteAPI('persistent_menu');
},
get_menu: function(cb) {
facebook_botkit.api.messenger_profile.getAPI('persistent_menu', cb);
return facebook_botkit.api.messenger_profile.getAPI('persistent_menu', cb);
},
account_linking: function(payload) {
var message = {
'account_linking_url': payload
};
facebook_botkit.api.messenger_profile.postAPI(message);
return facebook_botkit.api.messenger_profile.postAPI(message);
},
delete_account_linking: function() {
facebook_botkit.api.messenger_profile.deleteAPI('account_linking_url');
return facebook_botkit.api.messenger_profile.deleteAPI('account_linking_url');
},
get_account_linking: function(cb) {
facebook_botkit.api.messenger_profile.getAPI('account_linking_url', cb);
return facebook_botkit.api.messenger_profile.getAPI('account_linking_url', cb);
},
domain_whitelist: function(payload) {
var message = {
'whitelisted_domains': Array.isArray(payload) ? payload : [payload]
};
facebook_botkit.api.messenger_profile.postAPI(message);
return facebook_botkit.api.messenger_profile.postAPI(message);
},
delete_domain_whitelist: function() {
facebook_botkit.api.messenger_profile.deleteAPI('whitelisted_domains');
return facebook_botkit.api.messenger_profile.deleteAPI('whitelisted_domains');
},
get_domain_whitelist: function(cb) {
facebook_botkit.api.messenger_profile.getAPI('whitelisted_domains', cb);
return facebook_botkit.api.messenger_profile.getAPI('whitelisted_domains', cb);
},
target_audience: function(payload) {
var message = {
'target_audience': payload
};
facebook_botkit.api.messenger_profile.postAPI(message);
return facebook_botkit.api.messenger_profile.postAPI(message);
},
delete_target_audience: function() {
facebook_botkit.api.messenger_profile.deleteAPI('target_audience');
return facebook_botkit.api.messenger_profile.deleteAPI('target_audience');
},
get_target_audience: function(cb) {
facebook_botkit.api.messenger_profile.getAPI('target_audience', cb);
return facebook_botkit.api.messenger_profile.getAPI('target_audience', cb);
},
home_url: function(payload) {
var message = {
home_url: payload
};
facebook_botkit.api.messenger_profile.postAPI(message);
return facebook_botkit.api.messenger_profile.postAPI(message);
},
delete_home_url: function() {
facebook_botkit.api.messenger_profile.deleteAPI('home_url');
return facebook_botkit.api.messenger_profile.deleteAPI('home_url');
},
get_home_url: function(cb) {
facebook_botkit.api.messenger_profile.getAPI('home_url', cb);
return facebook_botkit.api.messenger_profile.getAPI('home_url', cb);
},
postAPI: function(message) {
var uri = 'https://' + api_host + '/' + api_version + '/me/messenger_profile?access_token=' + configuration.access_token;
return new Promise(function(resolve, reject) {
var uri = 'https://' + api_host + '/' + api_version + '/me/messenger_profile?access_token=' + configuration.access_token;

if (facebook_botkit.config.require_appsecret_proof) {
uri += '&appsecret_proof=' + appsecret_proof;
}
if (facebook_botkit.config.require_appsecret_proof) {
uri += '&appsecret_proof=' + appsecret_proof;
}

request.post(uri,
{form: message},
function(err, res, body) {
if (err) {
facebook_botkit.log('Could not configure messenger profile');
} else {
request.post(uri,
{form: message},
function(err, res, body) {
if (err) {
facebook_botkit.log('Could not configure messenger profile');
reject(err);
} else {

var results = null;
try {
results = JSON.parse(body);
} catch (err) {
facebook_botkit.log('ERROR in messenger profile API call: Could not parse JSON', err, body);
}
var results = null;
try {
results = JSON.parse(body);
} catch (err) {
facebook_botkit.log('ERROR in messenger profile API call: Could not parse JSON', err, body);
reject(err);
}

if (results) {
if (results.error) {
facebook_botkit.log('ERROR in messenger profile API call: ', results.error.message);
} else {
facebook_botkit.debug('Successfully configured messenger profile', body);
if (results) {
if (results.error) {
facebook_botkit.log('ERROR in messenger profile API call: ', results.error.message);
reject(results.error);
} else {
facebook_botkit.debug('Successfully configured messenger profile', body);
resolve(body);
}
}
}
}
});
});
});
},
deleteAPI: function(type) {
var uri = 'https://' + api_host + '/' + api_version + '/me/messenger_profile?access_token=' + configuration.access_token;
return new Promise(function(resolve, reject) {
var uri = 'https://' + api_host + '/' + api_version + '/me/messenger_profile?access_token=' + configuration.access_token;

var message = {
'fields': [type]
};
var message = {
'fields': [type]
};

if (facebook_botkit.config.require_appsecret_proof) {
uri += '&appsecret_proof=' + appsecret_proof;
}
if (facebook_botkit.config.require_appsecret_proof) {
uri += '&appsecret_proof=' + appsecret_proof;
}

request.delete(uri,
{form: message},
function(err, res, body) {
if (err) {
facebook_botkit.log('Could not configure messenger profile');
} else {
facebook_botkit.debug('Successfully configured messenger profile', message);
}
});
request.delete(uri,
{form: message},
function(err, res, body) {
if (err) {
facebook_botkit.log('Could not configure messenger profile');
reject(err);
} else {
var results = null;
try {
results = JSON.parse(body);
} catch (err) {
facebook_botkit.log('ERROR in messenger profile API call: Could not parse JSON', err, body);
reject(err);
}

if (results) {
if (results.error) {
facebook_botkit.log('ERROR in messenger profile API call: ', results.error.message);
reject(results.error);
} else {
facebook_botkit.debug('Successfully configured messenger profile ', body);
resolve(message);
}
}
}
});
});
},
getAPI: function(fields, cb) {
var uri = 'https://' + api_host + '/' + api_version + '/me/messenger_profile?fields=' + fields + '&access_token=' + configuration.access_token;
return new Promise(function(resolve, reject) {
var uri = 'https://' + api_host + '/' + api_version + '/me/messenger_profile?fields=' + fields + '&access_token=' + configuration.access_token;

if (facebook_botkit.config.require_appsecret_proof) {
uri += '&appsecret_proof=' + appsecret_proof;
}
if (facebook_botkit.config.require_appsecret_proof) {
uri += '&appsecret_proof=' + appsecret_proof;
}

request.get(uri,
function(err, res, body) {
if (err) {
facebook_botkit.log('Could not get messenger profile');
if (cb) {
cb(err);
}
} else {
facebook_botkit.debug('Successfully got messenger profile ', body);
if (cb) {
cb(null, body);
request.get(uri,
function(err, res, body) {
if (err) {
facebook_botkit.log('Could not get messenger profile');
if (cb) cb(err)
else reject(err)
} else {
var results = null;
try {
results = JSON.parse(body);
} catch (err) {
facebook_botkit.log('ERROR in messenger profile API call: Could not parse JSON', err, body);
if (cb) cb(err)
else reject(err)
}

if (results) {
if (results.error) {
facebook_botkit.log('ERROR in messenger profile API call: ', results.error.message);
if (cb) cb(results.error)
else reject(results.error)
} else {
facebook_botkit.debug('Successfully got messenger profile ', body);
if (cb) cb(null, body)
else resolve(body)
}
}
}
}
});
});
});
},
get_messenger_code: function(image_size, cb, ref) {
var uri = 'https://' + api_host + '/' + api_version + '/me/messenger_codes?access_token=' + configuration.access_token;
Expand Down