Skip to content

Commit

Permalink
implement getPageSubscription
Browse files Browse the repository at this point in the history
  • Loading branch information
wtflink authored and chentsulin committed Jan 29, 2019
1 parent 8236629 commit 9a2494e
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 4 deletions.
26 changes: 25 additions & 1 deletion packages/messaging-api-messenger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3478,7 +3478,7 @@ Default Fields:

## `getSubscriptions`

Get the current Webhook subscriptions setted up on your app.
Get the current Webhook subscriptions set up on your app.

| Param | Type | Description |
| ------------ | -------- | ----------------- |
Expand All @@ -3500,6 +3500,30 @@ client.getSubscriptions({
});
```

## `getPageSubscription`

Get the current page subscriptions set up on your app.

| Param | Type | Description |
| ------------ | -------- | ----------------- |
| access_token | `String` | App access token. |

Example:

```js
client.getPageSubscription({
access_token: APP_ACCESS_TOKEN,
});
```

Or provide app id and app secret instead of app access token:

```js
client.getPageSubscription({
access_token: `${APP_ID}|${APP_SECRET}`,
});
```

<br />

## `getPageInfo`
Expand Down
42 changes: 40 additions & 2 deletions packages/messaging-api-messenger/src/MessengerClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,10 @@ export default class MessengerClient {
);

const appId = app_id || this._appId;
invariant(appId, 'App ID is required to create subscription');
invariant(appId, 'App ID is required to get subscriptions');
invariant(
this._appSecret || appAccessToken,
'App Secret or App Token is required to create subscription'
'App Secret or App Token is required to get subscriptions'
);

const accessToken =
Expand All @@ -357,6 +357,44 @@ export default class MessengerClient {
.then(res => res.data.data, handleError);
}

/**
* Extract page subscription from subscriptions
*
* https://developers.facebook.com/docs/graph-api/reference/app/subscriptions
*/
getPageSubscription({
app_id,
access_token: appAccessToken,
}: {
app_id?: string,
access_token?: string,
} = {}): Promise<MessengerSubscription> {
warning(
!app_id,
'Provide App ID in the function is deprecated. Provide it in `MessengerClient.connect({ appId, ... })` instead'
);

const appId = app_id || this._appId;
invariant(appId, 'App ID is required to get subscription');
invariant(
this._appSecret || appAccessToken,
'App Secret or App Token is required to get subscription'
);

const accessToken =
appAccessToken || `${appId}|${((this._appSecret: any): string)}`;

return this.getSubscriptions({
app_id: appId,
access_token: accessToken,
}).then(
subscriptions =>
subscriptions.filter(
subscription => subscription.object === 'page'
)[0] || null
);
}

/**
* Messaging Feature Review API
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ describe('subscription', () => {
});

describe('#getSubscriptions', () => {
it('should set other optional parameters', async () => {
it('should get current subscriptions', async () => {
const { client, mock } = createMock();
const reply = {
data: [
Expand Down Expand Up @@ -186,6 +186,55 @@ describe('subscription', () => {
]);
});
});

describe('#getPageSubscription', () => {
it('should get current page subscription', async () => {
const { client, mock } = createMock();
const reply = {
data: [
{
object: 'page',
callback_url: 'https://mycallback.com',
active: true,
fields: [
{
name: 'messages',
version: 'v2.12',
},
],
},
{
object: 'user',
callback_url: 'https://mycallback.com',
active: true,
fields: [
{
name: 'feed',
version: 'v2.12',
},
],
},
],
};

mock
.onGet(`/${APP_ID}/subscriptions?access_token=${APP_ID}|${APP_SECRET}`)
.reply(200, reply);

const res = await client.getPageSubscription();
expect(res).toEqual({
object: 'page',
callback_url: 'https://mycallback.com',
active: true,
fields: [
{
name: 'messages',
version: 'v2.12',
},
],
});
});
});
});

describe('#getMessagingFeatureReview', () => {
Expand Down

0 comments on commit 9a2494e

Please sign in to comment.