Skip to content

Commit

Permalink
#11 - implement message proofing for staff command and bot responses
Browse files Browse the repository at this point in the history
  • Loading branch information
kmaida committed May 2, 2020
1 parent 63a8d80 commit 0a65b81
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
5 changes: 4 additions & 1 deletion bot-response/message-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ const msgText = {
return 'The "' + rotation + '" rotation already exists. You can assign someone to be on-call with `@rota "' + rotation + '" assign [@user]`.';
},
staffConfirm: (rotation) => {
return ':busts_in_silhouette: The "' + rotation + '" rotation staff has been saved! You can now use `@rota "' + rotation + '" assign next` to rotate assignments. If the currently on-call person is not in the staff list, the assignment will default to the _first person_ in the rotation staff list.';
return ':busts_in_silhouette: The "' + rotation + '" rotation staff has been saved! You can now use `@rota "' + rotation + '" assign next` to rotate assignments.\nWhen using `next`, if the currently on-call person is not in the staff list, the assignment will default to the _first person_ in the rotation.\n_Note: I remove duplicates. If you want someone to pull double duty, you\'ll need to do a manual assignment._';
},
staffEmpty: (rotation) => {
return `:disappointed: I didn't understand that staff list. I think it's empty. To save staff, please make sure you pass me a space-separated list of valid usernames. (I can also understand a comma+space separated list, but that's just more typing for you!)`;
},
staffError: (rotation) => {
return ':shrug: I couldn\'t save staff for the "' + rotation + '" rotation because it does not exist. To create this rotation, first tell me `@rota "' + rotation + '" create [description]`, _then_ set up staffing.';
Expand Down
28 changes: 20 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ app.event('app_mention', async({ event, context }) => {
const isMessage =
testMessage &&
!isCreate &&
!isStaff &&
!isStaff && !text.includes('" staff <@') // catch malformed staff commands and don't put them through as messages
!isAssign &&
!isWho &&
!isAbout &&
Expand Down Expand Up @@ -103,6 +103,7 @@ app.event('app_mention', async({ event, context }) => {
STAFF
@rota "[rotation-name]" staff [@user @user @user]
Staffs a rotation by passing a space-separated list of users
Also catches comma-separated lists
--*/
if (isStaff) {
try {
Expand All @@ -111,13 +112,24 @@ app.event('app_mention', async({ event, context }) => {
const staff = pCmd.staff;

if (rotation in store.getStoreList()) {
// If rotation exists, set staff list
store.saveStaff(rotation, staff);
const result = await app.client.chat.postMessage({
token: botToken,
channel: channelID,
text: msgText.staffConfirm(rotation)
});
if (!staff.length) {
// If staff array is empty, send an error message
const result = await app.client.chat.postMessage({
token: botToken,
channel: channelID,
text: msgText.staffEmpty(rotation)
});
} else {
// Rotation exists and list isn't empty
// Save to store
store.saveStaff(rotation, staff);
// Confirm in channel with message about using assign next
const result = await app.client.chat.postMessage({
token: botToken,
channel: channelID,
text: msgText.staffConfirm(rotation)
});
}
} else {
// Rotation doesn't exist; prompt to create it first
const result = await app.client.chat.postMessage({
Expand Down
21 changes: 11 additions & 10 deletions utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ const utils = {
create: /^<@(U[A-Za-z0-9|._\-]+?)> "([a-z0-9\-]+?)" (create) (.*)$/g,
// @rota "[rotation]" staff [@username, @username, @username]
// Accepts a space-separated list of usernames to staff a rotation
// (List of mentions has to start with <@U and end with > but can contain spaces and multiple user mentions)
staff: /^<@(U[A-Za-z0-9|._\-]+?)> "([a-z0-9\-]+?)" (staff) (<@U[<@>A-Za-z0-9|._\s\-]+?>)$/g,
// List of mentions has to start with <@U and end with > but can contain spaces, commas, multiple user mentions
staff: /^<@(U[A-Za-z0-9|._\-]+?)> "([a-z0-9\-]+?)" (staff) (<@U[<@>A-Za-z0-9|._,\s\-]+?>)$/g,
// Test for a single user mention
// <@U03LKJ> or <@U0345|name>
usermention: /^<@(U[A-Za-z0-9|._\-]+?)>$/g,
usermention: /^<@U[A-Za-z0-9|._\-]+?>$/g,
// @rota "[rotation]" assign [@username]
// Assigns a user to a rotation
assign: /^<@(U[A-Za-z0-9|._\-]+?)> "([a-z0-9\-]+?)" (assign) (<@U[A-Za-z0-9|._\-]+?>)(.*)$/g,
Expand Down Expand Up @@ -85,14 +85,15 @@ const utils = {
}
}
// Rotation, command, list of space-separated usermentions
// Proofed to accommodate use of comma+space separation and minor whitespace typos
else if (cmd === 'staff') {
const getStaffArray = (staffStr) => {
const arr = staffStr.trim().split(' ');
const testValue = (str) => {
const regex = new RegExp(this.regex.usermention);
return regex.test(str);
};
return arr.every(testValue) ? arr : [];
const cleanStr = staffStr.replace(/,/g, '').trim();
const arr = cleanStr.split(' ');
const noEmpty = arr.filter(item => !!item !== false); // Remove falsey values
const noDupes = new Set(noEmpty); // Remove duplicates
const cleanArr = [...noDupes]; // Convert set back to array
return cleanArr || [];
};
return {
rotation: res[2],
Expand Down Expand Up @@ -127,7 +128,7 @@ const utils = {
return {
command: cmd,
rotation: res[2],
params: res[3]
message: res[3]
};
}
}
Expand Down

0 comments on commit 0a65b81

Please sign in to comment.