Skip to content

Commit

Permalink
Merge pull request #793 from heycasa/master
Browse files Browse the repository at this point in the history
Add support for new messenger 2.0 profile home_url
  • Loading branch information
Ben Brown committed Apr 26, 2017
2 parents fca6452 + f0ae14b commit 0fbad25
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 11 deletions.
28 changes: 28 additions & 0 deletions docs/readme-facebook.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,23 @@ Remove all domains

Get a list of the whitelisted domains.

### controller.api.messenger_profile.home_url()
| Argument | Description
|--- |---
| payload | A home_url object with the properties `url`, `webview_height_ratio`, `in_test`

View the facebook documentation for details of the [home_url](https://developers.facebook.com/docs/messenger-platform/messenger-profile/home-url) payload object.

*NB.* The value of the `url` property must be present in the domain_whitelist array

### controller.api.messenger_profile.delete_home_url()

Remove the home_url setting

### controller.api.messenger_profile.get_home_url()

Get the home_url

#### Using the The Messenger Profile API

```js
Expand Down Expand Up @@ -451,6 +468,17 @@ controller.api.messenger_profile.get_domain_whitelist(function (err, data) {
console.log('****** Whitelisted domains :', data);
});

controller.api.messenger_profile.home_url({
"url": 'https://mydomain.com',
"webview_height_ratio": 'tall',
"in_test": false
})

controller.api.messenger_profile.get_home_url(function (err, data) {
console.log('****** Home url :', data);
});

controller.api.messenger_profile.delete_home_url();

controller.hears(['hello'],'facebook_postback', function(bot, message) {
//...
Expand Down
4 changes: 2 additions & 2 deletions lib/CoreBot.js
Original file line number Diff line number Diff line change
Expand Up @@ -963,8 +963,8 @@ function Botkit(configuration) {
keywords = [keywords];
}

if(keywords instanceof RegExp) {
keywords = [keywords]
if (keywords instanceof RegExp) {
keywords = [keywords];
}

if (typeof(events) == 'string') {
Expand Down
12 changes: 12 additions & 0 deletions lib/Facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,18 @@ function Facebookbot(configuration) {
get_domain_whitelist: function(cb) {
facebook_botkit.api.messenger_profile.getAPI('whitelisted_domains', cb);
},
home_url: function(payload) {
var message = {
home_url: payload
};
facebook_botkit.api.messenger_profile.postAPI(message);
},
delete_home_url: function() {
facebook_botkit.api.messenger_profile.deleteAPI('home_url');
},
get_home_url: function(cb) {
facebook_botkit.api.messenger_profile.getAPI('home_url', cb);
},
postAPI: function(message) {
request.post('https://graph.facebook.com/v2.6/me/messenger_profile?access_token=' + configuration.access_token,
{form: message},
Expand Down
18 changes: 9 additions & 9 deletions lib/Tropo.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ function Tropobot(configuration) {

controller.handleWebhookPayload = function(req, res, bot) {

console.log('GOT PAYLOAD', req.body);
var payload = req.body.session;
var msg = {
user: payload.from.id,
text: payload.initialText,
channel: payload.from.id,
}
console.log('GOT PAYLOAD', req.body);
var payload = req.body.session;
var msg = {
user: payload.from.id,
text: payload.initialText,
channel: payload.from.id,
};

controller.receiveMessage(bot, msg);
controller.receiveMessage(bot, msg);

};

Expand Down Expand Up @@ -157,7 +157,7 @@ function Tropobot(configuration) {
ciscospark_message[k] = message[k];
}

console.log("SAY", message);
console.log('SAY', message);


// controller.api.messages.create(ciscospark_message).then(function(message) {
Expand Down
59 changes: 59 additions & 0 deletions tests/Facebook.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,65 @@ describe('FacebookBot', function() {
});
});

describe('messenger profile api', function(done) {
var facebook_bot = Botkit.facebookbot({});
describe('home_url', function(done) {
it('home_url should be a function', function(done) {
facebook_bot.api.messenger_profile.home_url.should.be.a.Function();
done();
});
it('home_url should post a payload', function(done) {
var expectedPayload = {
home_url: {
url: 'https://testurl.com',
webview_height_ratio: 'tall',
in_test: true
}
};

var expectedApiCall = sinon.spy();
facebook_bot.api.messenger_profile.postAPI = expectedApiCall;
facebook_bot.api.messenger_profile.home_url({
url: 'https://testurl.com',
webview_height_ratio: 'tall',
in_test: true
});
expectedApiCall.should.be.calledWith(expectedPayload);
done();
});
it('get_home_url should be a function', function(done) {
facebook_bot.api.messenger_profile.get_home_url.should.be.a.Function();
done();
});
it('get_home_url should trigger a callback', function(done) {
var apiGet = sinon.stub(facebook_bot.api.messenger_profile, 'getAPI').callsFake(function fakeFn(fields, cb) {
return cb(null, {
"home_url" : {
"url": "http://petershats.com/send-a-hat",
"webview_height_ratio": "tall",
"in_test":true
}
});
});
facebook_bot.api.messenger_profile.get_home_url(function(err, result) {
done();
});
});
it('delete_home_url should be a function', function(done) {
facebook_bot.api.messenger_profile.get_home_url.should.be.a.Function();
done();
});
it('delete_home_url should trigger a delete api call', function(done) {
var expectedApiCall = sinon.spy();
facebook_bot.api.messenger_profile.deleteAPI = expectedApiCall;
facebook_bot.api.messenger_profile.delete_home_url();
expectedApiCall.should.be.calledWith('home_url');
done();
})
});


});

describe('handleWebhookPayload()', function(done) {
it('Should be function', function(done) {
Expand Down

0 comments on commit 0fbad25

Please sign in to comment.