Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add middleware matching app_mention events' text #499

Merged
merged 2 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 57 additions & 13 deletions src/middleware/builtin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,18 @@ describe('matchMessage()', () => {
};
}

function matchesPatternTestCase(pattern: string | RegExp, matchingText: string): Mocha.AsyncFunc {
function matchesPatternTestCase(
pattern: string | RegExp,
matchingText: string,
buildFakeEvent: (content: string) => SlackEvent,
): Mocha.AsyncFunc {
return async () => {
// Arrange
const dummyContext: DummyContext = {};
const fakeNext = sinon.fake();
const fakeArgs = {
next: fakeNext,
message: createFakeMessageEvent(matchingText),
event: buildFakeEvent(matchingText),
context: dummyContext,
} as unknown as MessageMiddlewareArgs;
const { matchMessage } = await importBuiltin();
Expand All @@ -61,13 +65,17 @@ describe('matchMessage()', () => {
};
}

function notMatchesPatternTestCase(pattern: string | RegExp, nonMatchingText: string): Mocha.AsyncFunc {
function notMatchesPatternTestCase(
pattern: string | RegExp,
nonMatchingText: string,
buildFakeEvent: (content: string) => SlackEvent,
): Mocha.AsyncFunc {
return async () => {
// Arrange
const dummyContext = {};
const fakeNext = sinon.fake();
const fakeArgs = {
message: createFakeMessageEvent(nonMatchingText),
event: buildFakeEvent(nonMatchingText),
context: dummyContext,
next: fakeNext,
} as unknown as MessageMiddlewareArgs;
Expand All @@ -89,7 +97,7 @@ describe('matchMessage()', () => {
const dummyContext = {};
const fakeNext = sinon.fake();
const fakeArgs = {
message: createFakeMessageEvent([{ type: 'divider' }]),
event: createFakeMessageEvent([{ type: 'divider' }]),
context: dummyContext,
next: fakeNext,
} as unknown as MessageMiddlewareArgs;
Expand All @@ -110,10 +118,22 @@ describe('matchMessage()', () => {
const matchingText = 'foobar';
const nonMatchingText = 'bar';
it('should initialize', initializeTestCase(pattern));
it('should match message events with a pattern that matches', matchesPatternTestCase(pattern, matchingText));
it('should filter out message events with a pattern that does not match', notMatchesPatternTestCase(
pattern, nonMatchingText,
));
it(
'should match message events with a pattern that matches',
matchesPatternTestCase(pattern, matchingText, createFakeMessageEvent),
);
it(
'should match app_mention events with a pattern that matches',
matchesPatternTestCase(pattern, matchingText, createFakeAppMentionEvent),
);
it(
'should filter out message events with a pattern that does not match',
notMatchesPatternTestCase(pattern, nonMatchingText, createFakeMessageEvent),
);
it(
'should filter out app_mention events with a pattern that does not match',
notMatchesPatternTestCase(pattern, nonMatchingText, createFakeAppMentionEvent),
);
it('should filter out message events which do not have text (block kit)', noTextMessageTestCase(pattern));
});

Expand All @@ -122,10 +142,22 @@ describe('matchMessage()', () => {
const matchingText = 'foobar';
const nonMatchingText = 'bar';
it('should initialize', initializeTestCase(pattern));
it('should match message events with a pattern that matches', matchesPatternTestCase(pattern, matchingText));
it('should filter out message events with a pattern that does not match', notMatchesPatternTestCase(
pattern, nonMatchingText,
));
it(
'should match message events with a pattern that matches',
matchesPatternTestCase(pattern, matchingText, createFakeMessageEvent),
);
it(
'should match app_mention events with a pattern that matches',
matchesPatternTestCase(pattern, matchingText, createFakeAppMentionEvent),
);
it(
'should filter out message events with a pattern that does not match',
notMatchesPatternTestCase(pattern, nonMatchingText, createFakeMessageEvent),
);
it(
'should filter out app_mention events with a pattern that does not match',
notMatchesPatternTestCase(pattern, nonMatchingText, createFakeAppMentionEvent),
);
it('should filter out message events which do not have text (block kit)', noTextMessageTestCase(pattern));
});
});
Expand Down Expand Up @@ -678,6 +710,18 @@ function createFakeMessageEvent(content: string | MessageEvent['blocks'] = ''):
return event as MessageEvent;
}

function createFakeAppMentionEvent(text: string = ''): AppMentionEvent {
const event: Partial<AppMentionEvent> = {
text,
type: 'app_mention',
user: 'USER_ID',
ts: 'MESSAGE_ID',
channel: 'CHANNEL_ID',
event_ts: 'MESSAGE_ID',
};
return event as AppMentionEvent;
}

const validCommandPayload: SlashCommand = {
token: 'token-value',
command: '/hi',
Expand Down
14 changes: 8 additions & 6 deletions src/middleware/builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,23 @@ export function matchConstraints(
/*
* Middleware that filters out messages that don't match pattern
*/
export function matchMessage(pattern: string | RegExp): Middleware<SlackEventMiddlewareArgs<'message'>> {
return async ({ message, context, next }) => {
export function matchMessage(
pattern: string | RegExp,
): Middleware<SlackEventMiddlewareArgs<'message' | 'app_mention'>> {
return async ({ event, context, next }) => {
let tempMatches: RegExpMatchArray | null;

if (message.text === undefined) {
if (event.text === undefined) {
return;
}

// Filter out messages that don't contain the pattern
// Filter out messages or app mentions that don't contain the pattern
if (typeof pattern === 'string') {
if (!message.text.includes(pattern)) {
if (!event.text.includes(pattern)) {
return;
}
} else {
tempMatches = message.text.match(pattern);
tempMatches = event.text.match(pattern);

if (tempMatches !== null) {
context['matches'] = tempMatches;
Expand Down