Skip to content

Commit

Permalink
feat: includesBotMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
chentsulin committed Jan 22, 2020
1 parent 073eb85 commit 95bb56c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
5 changes: 4 additions & 1 deletion packages/bottender/src/slack/SlackBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,22 @@ export default class SlackBot extends Bot<
verificationToken,
origin,
skipLegacyProfile,
includesBotMessages,
}: {
accessToken: string;
sessionStore?: SessionStore;
sync?: boolean;
verificationToken?: string;
origin?: string;
skipLegacyProfile?: boolean | null;
skipLegacyProfile?: boolean;
includesBotMessages?: boolean;
}) {
const connector = new SlackConnector({
accessToken,
verificationToken,
origin,
skipLegacyProfile,
includesBotMessages,
});
super({ connector, sessionStore, sync });
this._accessToken = accessToken;
Expand Down
23 changes: 16 additions & 7 deletions packages/bottender/src/slack/SlackConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ type EventsAPIBody = {
export type SlackRequestBody = EventsAPIBody | { payload: string };

type CommonConstructorOptions = {
skipLegacyProfile?: boolean | null;
skipLegacyProfile?: boolean;
verificationToken?: string;
includesBotMessages?: boolean;
};

type ConstructorOptionsWithoutClient = {
Expand All @@ -63,8 +64,14 @@ export default class SlackConnector

_skipLegacyProfile: boolean;

_includesBotMessages: boolean;

constructor(options: ConstructorOptions) {
const { verificationToken, skipLegacyProfile } = options;
const {
verificationToken,
skipLegacyProfile,
includesBotMessages,
} = options;
if ('client' in options) {
this._client = options.client;
} else {
Expand All @@ -83,15 +90,17 @@ export default class SlackConnector

this._verificationToken = verificationToken || '';

this._skipLegacyProfile =
typeof skipLegacyProfile === 'boolean' ? skipLegacyProfile : true;

if (!this._verificationToken) {
warning(
false,
'`verificationToken` is not set. Will bypass Slack event verification.\nPass in `verificationToken` to perform Slack event verification.'
);
}

this._skipLegacyProfile =
typeof skipLegacyProfile === 'boolean' ? skipLegacyProfile : true;

this._includesBotMessages = includesBotMessages || false;
}

_getRawEventFromRequest(body: SlackRequestBody): SlackRawEvent {
Expand Down Expand Up @@ -304,8 +313,8 @@ export default class SlackConnector
mapRequestToEvents(body: SlackRequestBody): SlackEvent[] {
const rawEvent = this._getRawEventFromRequest(body);

if (this._isBotEventRequest(body)) {
return []; // FIXME
if (!this._includesBotMessages && this._isBotEventRequest(body)) {
return [];
}

return [new SlackEvent(rawEvent)];
Expand Down
8 changes: 8 additions & 0 deletions packages/bottender/src/slack/SlackEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,14 @@ export default class SlackEvent implements Event<SlackRawEvent> {
}
return null;
}

/**
* Determine if the event is a bot message event.
*
*/
get isBotMessage(): boolean {
return (this._rawEvent as any).subtype === 'bot_message';
}
}

// https://api.slack.com/events
Expand Down
20 changes: 20 additions & 0 deletions packages/bottender/src/slack/__tests__/SlackConnector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ const RtmMessage = {
function setup({
verificationToken = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
skipLegacyProfile,
includeBotMesssages,
} = {}) {
const mockSlackOAuthClient = {
getUserInfo: jest.fn(),
Expand All @@ -164,6 +165,7 @@ function setup({
accessToken,
verificationToken,
skipLegacyProfile,
includeBotMesssages,
}),
mockSlackOAuthClient,
};
Expand Down Expand Up @@ -396,6 +398,24 @@ describe('#mapRequestToEvents', () => {
expect(events).toHaveLength(1);
expect(events[0]).toBeInstanceOf(SlackEvent);
});

it('should not include bot message by default', () => {
const { connector } = setup();
const events = connector.mapRequestToEvents(botRequest.body);

expect(events).toHaveLength(1);
expect(events[0]).toBeInstanceOf(SlackEvent);
});

it('should include bot message when includeBotMesssages is true', () => {
const { connector } = setup({
includeBotMesssages: true,
});
const events = connector.mapRequestToEvents(botRequest.body);

expect(events).toHaveLength(1);
expect(events[0]).toBeInstanceOf(SlackEvent);
});
});

describe('#createContext', () => {
Expand Down
15 changes: 15 additions & 0 deletions packages/bottender/src/slack/__tests__/SlackEvent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,16 @@ const message = {
ts: '1355517523.000005',
};

const botMessage = {
type: 'message',
user: 'U13A00000',
text: 'hello',
botId: 'B6AK00000',
ts: '1500435914.425136',
channel: 'C6A900000',
eventTs: '1500435914.425136',
};

const channelsMessage = {
type: 'message',
channel: 'C2147483705',
Expand Down Expand Up @@ -894,3 +904,8 @@ describe('interactive message event', () => {
expect(new SlackEvent(message).action).toEqual(null);
});
});

it('#isBotMessage', () => {
expect(new SlackEvent(message).isBotMessage).toEqual(false);
expect(new SlackEvent(botMessage).isBotMessage).toEqual(true);
});

0 comments on commit 95bb56c

Please sign in to comment.