Skip to content

Commit

Permalink
feat: implement getClient helper function (#634)
Browse files Browse the repository at this point in the history
  • Loading branch information
chentsulin authored Jan 22, 2020
1 parent 5211016 commit 9f0b334
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
6 changes: 2 additions & 4 deletions docs/channel-messenger-multi-page.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,9 @@ We recommend you at least enable the following fields:
We assume that you might need to add your bot to a Facebook Pages dynamically. In the following code, you can see how to subscribe `Facebook Page Subscriptions Fields` your bot needs.

```js
const { MessengerClient } = require('messaging-api-messenger');
const { getClient } = require('bottender');

const config = require('./bottender.config.js');

const messenger = new MessengerClient(config.channels.messenger);
const messenger = getClient('messenger');

// subscribe app for page
await messenger.axios.post(
Expand Down
4 changes: 4 additions & 0 deletions packages/bottender/src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ describe('core', () => {
expect(core.getSessionStore).toBeDefined();
});

it('export getClient', () => {
expect(core.getClient).toBeDefined();
});

it('export chain', () => {
expect(core.chain).toBeDefined();
});
Expand Down
38 changes: 38 additions & 0 deletions packages/bottender/src/getClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import LineBot from './line/LineBot';
import MessengerBot from './messenger/MessengerBot';
import SlackBot from './slack/SlackBot';
import TelegramBot from './telegram/TelegramBot';
import ViberBot from './viber/ViberBot';
import getBottenderConfig from './shared/getBottenderConfig';
import getSessionStore from './getSessionStore';
import { Channel } from './types';

const BOT_MAP = {
messenger: MessengerBot,
line: LineBot,
slack: SlackBot,
telegram: TelegramBot,
viber: ViberBot,
};

function getClient(channel: Channel) {
const { channels = {} } = getBottenderConfig();
const sessionStore = getSessionStore();

const channelConfig = (channels as any)[channel];

if (!channelConfig) {
return null;
}

const ChannelBot = BOT_MAP[channel];

const channelBot = new ChannelBot({
...channelConfig,
sessionStore,
} as any);

return channelBot.connector.client;
}

export default getClient;
4 changes: 1 addition & 3 deletions packages/bottender/src/getSessionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import warning from 'warning';
import getBottenderConfig from './shared/getBottenderConfig';

function getSessionStore() {
const bottenderConfig = getBottenderConfig();

const { session } = bottenderConfig;
const { session } = getBottenderConfig();

const sessionDriver = (session && session.driver) || 'memory';

Expand Down
1 change: 1 addition & 0 deletions packages/bottender/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export { bottender };
export { default as Bot } from './bot/Bot';
export { default as Context } from './context/Context';
export { default as getSessionStore } from './getSessionStore';
export { default as getClient } from './getClient';

/* Action */
export { default as chain } from './chain';
Expand Down

0 comments on commit 9f0b334

Please sign in to comment.