Skip to content

Commit

Permalink
added ability to override accessToken on all APIs - closes miki2826#6
Browse files Browse the repository at this point in the history
enhanced readme
  • Loading branch information
miki2826 committed Jul 30, 2016
1 parent 65e602c commit 3415fcd
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 11 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,18 @@ botly.setPersistentMenu({pageId: "myPage", buttons: [botly.createPostbackButton(
```

#### getUserProfile (userId[, callback])
Also supports passing an object as `{id: userId, accessToken: OTHER_TOKEN}`

```javascript
botly.getUserProfile(senduserIder, function (err, info) {
botly.getUserProfile(userId, function (err, info) {
//cache it
});
```

#### getPSID (accountLinkingToken[, callback])
Used to retrieve the user page-scoped ID (PSID) during the linking flow.
Also supports passing an object as `{token: accountLinkingToken, accessToken: OTHER_TOKEN}`

```javascript
botly.getUserProfile(accountLinkingToken, function (err, info) {
//cache it
Expand Down Expand Up @@ -297,6 +301,10 @@ botly.on("account_link", (sender, message, link) => {

### Change Log

#### version 1.1.4
- added support for account linking functionality (event, getPSID)
- added ability to override accessToken on all APIs for multiple pages support

#### version 1.1.0
- added support for sender actions using `sendAction` (mark seen/ typing on/ typing off)

Expand Down
24 changes: 16 additions & 8 deletions lib/Botly.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ Botly.prototype.router = function () {
return router;
};

Botly.prototype.getPSID = function (accountLinkingToken, callback) {
Botly.prototype.getPSID = function (options, callback) {
let accountLinkingToken = options;
if (typeof options === 'object') {
accountLinkingToken = options.token;
}
const USER_URL = `${FB_URL}me`;

request.get(
Expand All @@ -89,7 +93,7 @@ Botly.prototype.getPSID = function (accountLinkingToken, callback) {
qs: {
fields: 'recipient',
account_linking_token: accountLinkingToken,
access_token: this.accessToken
access_token: this.accessToken || options.accessToken
},
json: true

Expand All @@ -100,15 +104,19 @@ Botly.prototype.getPSID = function (accountLinkingToken, callback) {
});
};

Botly.prototype.getUserProfile = function (userId, callback) {
Botly.prototype.getUserProfile = function (options, callback) {
let userId = options;
if (typeof options === 'object') {
userId = options.id;
}
const USER_URL = `${FB_URL}${userId}`;

request.get(
{
url: USER_URL,
qs: {
fields: 'first_name,last_name,profile_pic,locale,timezone,gender',
access_token: this.accessToken
access_token: this.accessToken || options.accessToken
},
json: true

Expand All @@ -127,7 +135,7 @@ Botly.prototype.setGetStarted = function (options, callback) {
url: PAGE_URL,
json: true,
qs: {
access_token: this.accessToken
access_token: this.accessToken || options.accessToken
},
body: {
setting_type: 'call_to_actions',
Expand All @@ -154,7 +162,7 @@ Botly.prototype.setPersistentMenu = function (options, callback) {
url: PAGE_URL,
json: true,
qs: {
access_token: this.accessToken
access_token: this.accessToken || options.accessToken
},
body: {
setting_type: 'call_to_actions',
Expand All @@ -176,7 +184,7 @@ Botly.prototype.send = function (options, callback) {
url: FB_MESSENGER_URL,
json: true,
qs: {
access_token: this.accessToken
access_token: this.accessToken || options.accessToken
},
body: {
recipient: {id: options.id},
Expand All @@ -198,7 +206,7 @@ Botly.prototype.sendAction = function (options, callback) {
url: FB_MESSENGER_URL,
json: true,
qs: {
access_token: this.accessToken
access_token: this.accessToken || options.accessToken
},
body: {
recipient: {id: options.id},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "botly",
"version": "1.1.3",
"version": "1.1.4",
"description": "Simple Facebook Messenger Bot API",
"main": "index.js",
"scripts": {
Expand Down
98 changes: 97 additions & 1 deletion test/botly_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,54 @@ describe('Botly Tests', function () {

});

it('should handle account linking messages', done => {
var linkContent = {
"status":"linked",
"authorization_code":"PASS_THROUGH_AUTHORIZATION_CODE"
};
var botly = new Botly({
accessToken: 'myToken',
verifyToken: 'myVerifyToken',
webHookPath: '/webhook',
notificationType: Botly.CONST.NOTIFICATION_TYPE.NO_PUSH
});
var router = botly.router();

botly.on('account_link', (id, message, link) => {
expect(id).to.equal(USER_ID);
expect(link).to.eql(linkContent);
done();
});

var response = http.createResponse();

var request = http.createRequest({
method: 'POST',
url: '/webhook',
body: {
'object': 'page',
'entry': [
{
'id': PAGE_ID,
'time': 1458668856451,
'messaging': [
{
'sender': {
'id': USER_ID
},
'recipient': {
'id': PAGE_ID
},
'account_linking': linkContent
}
]
}
]
}
});
router.handle(request, response);
});

it('should handle postback messages', done => {

var botly = new Botly({
Expand Down Expand Up @@ -750,7 +798,30 @@ describe('Botly Tests', function () {

});

it('should get user profile', done => {
it('should get user profile with object ', done => {
request.get.yields(
{
first_name: 'miki'
}
);
var botly = new Botly({
accessToken: 'myToken',
verifyToken: 'myVerifyToken',
webHookPath: '/webhook',
notificationType: Botly.CONST.NOTIFICATION_TYPE.NO_PUSH
});

botly.getUserProfile({id: USER_ID}, (data) => {
expect(request.get.calledOnce).to.be.true;
expect(data).to.eql({
first_name: 'miki'
});
done();
});

});

it('should get page scoped id', done => {
request.get.yields(
{
"id": "111",
Expand All @@ -775,6 +846,31 @@ describe('Botly Tests', function () {

});

it('should get page scoped id with an object', done => {
request.get.yields(
{
"id": "111",
"recipient": "222"
}
);
var botly = new Botly({
accessToken: 'myToken',
verifyToken: 'myVerifyToken',
webHookPath: '/webhook',
notificationType: Botly.CONST.NOTIFICATION_TYPE.NO_PUSH
});

botly.getPSID({token: '333', accessToken: '7777'}, (data) => {
expect(request.get.calledOnce).to.be.true;
expect(data).to.eql({
"id": "111",
"recipient": "222"
});
done();
});

});

it('should set welcome screen', () => {
request.post.yields(null, {});
var botly = new Botly({
Expand Down

0 comments on commit 3415fcd

Please sign in to comment.