Skip to content

Commit

Permalink
support 'optional' triggers, and allow opting out of them
Browse files Browse the repository at this point in the history
  • Loading branch information
mgwalker committed Feb 27, 2024
1 parent 7a672f0 commit 58f0106
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 9 deletions.
19 changes: 13 additions & 6 deletions src/scripts/inclusion-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ module.exports = async (app) => {
);

const optout = optOut(
"inclusion_bot_strict",
"Inclusion Bot | Strict mode",
"Inclusion Bot operates in strict mode by default, highlighting all language that we have identified as racist, ableist, sexist, or otherwise exclusionary. We believe this is the ideal way to use Inclusion Bot. However, we recognize that some people may not want to be reminded about some of the language identified, so we offer a way to have Inclusion Bot only highlight language that we, as an organization, believe strongly should not be used. We encourage people to keep the stricter mode enabled as a learning aid, but if it makes you feel excluded yourself, please opt out of it.",
"inclusion_bot_extended",
"Inclusion Bot | Extended mode",
"Opt out of extended inclusive language notifications. These notifications are meant to help educate and remind us to be mindful of our colleagues.",
);

// Use the module exported version here, so that it can be stubbed for testing
Expand All @@ -61,10 +61,12 @@ module.exports = async (app) => {
const combinedRegex = new RegExp(`\\b${combinedString}\\b`, "i");

app.message(combinedRegex, (msg) => {
const extended = !optout.isOptedOut(msg.event.user);

// Find the specific match that triggered this bot. At this point, go ahead
// and remove things that should be ignored.
const specificMatch = triggers
.map(({ alternatives, ignore, matches }) => {
.map(({ alternatives, ignore, optional, matches }) => {
const { text } = msg.message;

// Wrap the match in word boundaries, so we don't match something that's
Expand All @@ -78,10 +80,12 @@ module.exports = async (app) => {
// makes the regexes a lot simpler.
return {
alternatives,
optional,
text: text.replace(ignoreRegex, "").match(matchRegex),
};
})
.filter(({ text }) => !!text)
.filter(({ optional }) => extended || !optional)
.map(({ text: [match], ...rest }) => ({ text: match, ...rest }));

// If there aren't any specific matches, it means the bot was triggered only
Expand All @@ -107,7 +111,11 @@ module.exports = async (app) => {
color: "#2eb886",
fallback: message,
blocks: [
{ type: "section", text: { type: "mrkdwn", text: message } },
{
type: "section",
text: { type: "mrkdwn", text: message },
...(extended ? optout.button : {}),
},
{
type: "section",
text: { type: "mrkdwn", text: pretexts.join("\n") },
Expand All @@ -127,7 +135,6 @@ module.exports = async (app) => {
},
],
},
...optout.button,
],
},
],
Expand Down
82 changes: 81 additions & 1 deletion src/scripts/inclusion-bot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const fs = require("fs");
const {
getApp,
utils: {
optOut,
slack: { addEmojiReaction, postEphemeralResponse },
},
} = require("../utils/test");
Expand Down Expand Up @@ -56,6 +57,9 @@ describe("Inclusion bot", () => {
const app = getApp();

const msg = {
event: {
user: "abc123",
},
message: {
id: "message id",
room: "channel id",
Expand All @@ -64,9 +68,18 @@ describe("Inclusion bot", () => {
},
};

const isOptedOut = jest.fn();

beforeEach(() => {
jest.resetAllMocks();

isOptedOut.mockReturnValue(false);

optOut.mockReturnValue({
button: { button: "goes here" },
isOptedOut,
});

fs.readFileSync.mockReturnValue(`
link: https://link.url
message: "This is the message"
Expand All @@ -85,14 +98,19 @@ triggers:
- match 2b
alternatives:
- b1
- matches:
- match 3
alternatives:
- b3
optional: true
`);
});

it("subscribes to case-insensitive utterances of uninclusive language", () => {
bot(app);

expect(app.message).toHaveBeenCalledWith(
/\b(match 1)(?=[^"']*(["'][^"']*["'][^"']*)*$)|(match 2a|match 2b)(?=[^"']*(["'][^"']*["'][^"']*)*$)\b/i,
/\b(match 1)(?=[^"']*(["'][^"']*["'][^"']*)*$)|(match 2a|match 2b)(?=[^"']*(["'][^"']*["'][^"']*)*$)|(match 3)(?=[^"']*(["'][^"']*["'][^"']*)*$)\b/i,
expect.any(Function),
);
});
Expand All @@ -110,6 +128,7 @@ triggers:
type: "mrkdwn",
text: "This is the message",
},
button: "goes here",
},
{
accessory: {
Expand Down Expand Up @@ -150,6 +169,8 @@ triggers:
beforeEach(() => {
bot(app);
handler = app.getHandler();

expectedMessage.attachments[0].blocks[1].accessory.value = "match 1";
});

it("handles a single triggering phrase", () => {
Expand Down Expand Up @@ -236,5 +257,64 @@ triggers:
// Reset the parts of the expected message that we changed above.
expectedMessage.attachments[0].blocks[1].accessory.value = "match 1";
});

it("does not add the opt-out button if the user is already opted-out", () => {
isOptedOut.mockReturnValue(true);

const noButton = JSON.parse(JSON.stringify(expectedMessage));
delete noButton.attachments[0].blocks[0].button;

msg.message.text = "hello this is the match 1 trigger";
handler(msg);

expect(addEmojiReaction).toHaveBeenCalledWith(msg, "wave");

noButton.attachments[0].blocks[1].text.text = expect.stringMatching(
/ Instead of saying "match 1," how about \*(a1|a2|a3)\*\?/,
);
expect(postEphemeralResponse).toHaveBeenCalledWith(msg, noButton);
});

it("does trigger on optional words if the user is not opted out", () => {
msg.message.text = "hello this is the match 3 trigger";
handler(msg);

expect(addEmojiReaction).toHaveBeenCalledWith(msg, "wave");

expectedMessage.attachments[0].blocks[1].accessory.value = "match 3";
expectedMessage.attachments[0].blocks[1].text.text =
expect.stringMatching(
/ Instead of saying "match 3," how about \*b3\*\?/,
);
expect(postEphemeralResponse).toHaveBeenCalledWith(msg, expectedMessage);
});

it("does not trigger if the user is opted-out and the trigger phrase is optional", () => {
isOptedOut.mockReturnValue(true);

msg.message.text = "hello this is the match 3 trigger";
handler(msg);

expect(addEmojiReaction).not.toHaveBeenCalled();

expect(postEphemeralResponse).not.toHaveBeenCalled();
});

it("does trigger on non-optional words but leaves out optional words if the user is opted-out", () => {
isOptedOut.mockReturnValue(true);

msg.message.text = "hello this is the match 1 and the match 3 trigger";
handler(msg);

expect(addEmojiReaction).toHaveBeenCalledWith(msg, "wave");

const noButton = { ...expectedMessage };
delete noButton.attachments[0].blocks[0].button;

noButton.attachments[0].blocks[1].text.text = expect.stringMatching(
/ Instead of saying "match 1," how about \*(a1|a2|a3)\*\?/,
);
expect(postEphemeralResponse).toHaveBeenCalledWith(msg, noButton);
});
});
});
4 changes: 2 additions & 2 deletions sync-inclusion-bot-words.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ const getTriggerExtraMetadataMap = (currentConfig) => {
}
// If there is an existing config that matches AND it has an ignore property
// add the existing ignore property on to the new trigger.
if (existing?.strict) {
mapped.strict = existing.strict;
if (existing?.optional) {
mapped.optional = existing.optional;
}

mapped.why = newTrigger.why;
Expand Down

0 comments on commit 58f0106

Please sign in to comment.