Skip to content
Open
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
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
node_modules/
.drone.yml
.env.example
phrases.json.example
.eslintrc.json
.gitignore
LICENSE
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ typings/
.next

keys/
phrases.json
17 changes: 17 additions & 0 deletions middleware/Authorization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require("dotenv").config();
const API_VERIFICATION = process.env.API_VERIFICATION_TOKEN;

class Authorization {
static authorization(req, res, next) {
if (!req.headers.authorization) {
return res.status(403).json({error: "No authorization token sent!"});
}
if (req.headers.authorization === `BEARER ${API_VERIFICATION}`) {
next();
return;
}
res.status(401).send();
}
}

module.exports = {Authorization};
13 changes: 0 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
"dotenv": "^8.1.0",
"eslint": "^6.5.1",
"googleapis": "^43.0.0",
"moment-timezone": "^0.5.26",
"node-cron": "^2.0.3"
}
}
23 changes: 5 additions & 18 deletions utilities/randomPhrases.js → phrases.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const phrases = {
greeting: [
{
"greeting": [
":wave: Good evening!",
":wave: Howdy!",
"Hey hey hey!",
Expand All @@ -12,7 +12,7 @@ const phrases = {
"Howdy there! :face_with_cowboy_hat:",
"Hey crew! :v:"
],
request: [
"request": [
"Please click the button below as soon as they're done.",
"After you're done, click the \"Done!\" button!",
"Please be so kind as to hit that button below once everything is done :hugging_face:",
Expand All @@ -21,7 +21,7 @@ const phrases = {
"Once you're done, be sure to hit that \"Done!\" button!",
"Don't forget to hit the button when you're done! Enjoy your night!"
],
no_chore: [
"no_chore": [
":+1: There are no chores tonight! Have fun on crew!",
"Wow! You lucked out! No chores tonight :simple_smile:",
"Have an awesome night! There are no chores to do.",
Expand All @@ -30,17 +30,4 @@ const phrases = {
"Oh man! There are no chores tonight! Enjoy your night!",
"Go get some food! :hamburger: There are no chores to do!"
]
};

const randomPhrase = type => {
const potentialPhrases = phrases[type];
if (type === "request") {
const currentMonth = (new Date()).getMonth() + 1;
if (4 <= currentMonth && currentMonth <= 9) {
potentialPhrases.push("After you go to the Snowman :icecream: or something, make sure that gets done! And then click the \"Done!\" button!");
}
}
return potentialPhrases[Math.floor(Math.random() * potentialPhrases.length)];
};

module.exports = { randomPhrase };
}
11 changes: 11 additions & 0 deletions phrases.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"greeting": [
":wave: Good evening!",
],
"request": [
"Please click the button below as soon as they're done."
],
"no_chore": [
":+1: There are no chores tonight! Have fun on crew!"
]
}
39 changes: 21 additions & 18 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ const Sentry = require("@sentry/node");
require("dotenv").config();

//local packages
const { app } = require("./utilities/bolt.js");
const { getTodaysChores } = require("./utilities/getChores.js");
const { sendNoChores, postToSlack } = require("./utilities/notify.js");
const { markChoreDone } = require("./utilities/markChoreDone.js");

//package configuration

//local configuration
const {Authorization} = require("./middleware/Authorization");
const {app, expressReceiver} = require("./utilities/bolt.js");
const {Notifications} = require("./utilities/Notifications");
const {Actions} = require("./utilities/Actions");
const {Chores} = require("./utilities/Chores");

//globals
const CRON_SCHEDULE = process.env.CRON_SCHEDULE;
Expand All @@ -26,14 +23,10 @@ if (process.env.SENTRY_DSN) {
Sentry.init(sentryConfig);
}

//helper functions
const runChores = async () => {
console.log("Running ChoreBot!");
const chores = await getTodaysChores();
if (chores === -1) sendNoChores();
// TODO: in the future, notify the officers that there are no chores
else postToSlack(chores);
};
//Initialize Classes
const chores = new Chores();
const notifications = new Notifications(chores);
const actions = new Actions();

app.action(
/^\d+$/,
Expand All @@ -43,9 +36,19 @@ app.action(
},
async ({ action, body }) => {
if (!action || !body || !body.user || !body.channel || !body.message) return;
markChoreDone(action.action_id, body.user.id, body.channel.id, body.message.ts,
actions.markChoreDone(action.action_id, body.user.id, body.channel.id, body.message.ts,
body.message.blocks);
}
);

cron.schedule(CRON_SCHEDULE, runChores);
cron.schedule(CRON_SCHEDULE, notifications.runChores.bind(notifications));

expressReceiver.app.use((req, res, next) => {
Authorization.authorization(req, res, next);
});

expressReceiver.app.get("/get/chores", async (req, res) => {
let todayschores = await chores.getTodaysChores();
todayschores = todayschores === -1 ? {chores: []} : {todayschores};
res.send(todayschores);
});
30 changes: 30 additions & 0 deletions utilities/Actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//local packages
const {
app: {
client: {
chat: {update}
}
}
} = require("./bolt.js");

//globals
const TOKEN = process.env.SLACK_BOT_TOKEN;

class Actions {

markChoreDone(index, user, channel, ts, initialBlocks) {
const blocks = this.crossOffAndTag(user, parseInt(index), initialBlocks);
update({token: TOKEN, channel, ts, blocks});
}

//helper functions

crossOffAndTag(user, index, blocks) {
const choreText = blocks[index + 2].text.text.replace("&gt;", "");
blocks[index + 2].text.text = `>~${choreText}~ Completed by <@${user}>`;
delete blocks[index + 2].accessory;
return blocks;
}
}

module.exports = {Actions};
51 changes: 51 additions & 0 deletions utilities/Chores.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//node packages
const {google} = require("googleapis");
require("dotenv").config();

//local packages
const privatekey = require("../keys/sheets-api.json");

//globals
const SPREADSHEET_ID = process.env.SPREADSHEET_ID;
const GDRIVE_EMAIL = process.env.GDRIVE_EMAIL;

class Chores {
constructor() {
this.jwtClient = new google.auth.JWT(
privatekey.client_email,
null,
privatekey.private_key,
["https://www.googleapis.com/auth/spreadsheets.readonly"],
GDRIVE_EMAIL
);
}

async getTodaysChores() {
const now = new Date();
const today = new Date(now.getTime() - (now.getTimezoneOffset() * 60000))
.toISOString().split("T")[0];
const todaysChores = (await this.getChores()).map(c => c[0] === today ? c[1] : null).filter(Boolean);
return todaysChores.length === 0 ? -1 : todaysChores;
}


async getChores() {
const sheets = google.sheets({
version: "v4",
auth: this.jwtClient
});

this.jwtClient.authorize(err => console.log(err ? err : "Successfully connected to Google Sheets!"));
try {
const {data} = await sheets.spreadsheets.values.get({
spreadsheetId: SPREADSHEET_ID,
range: "A2:B"
});
return data ? data.values : null;
} catch (error) {
console.error(error);
}
}
}

module.exports = {Chores};
92 changes: 92 additions & 0 deletions utilities/Notifications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const {
app: {
client: {
chat: {postMessage}
}
}
} = require("./bolt.js");

const fs = require("fs");

require("dotenv").config();

const TOKEN = process.env.SLACK_BOT_TOKEN;
const CHORES_CHANNEL = process.env.CHORES_CHANNEL;


class Notifications {
constructor(chores) {
this.chores = chores;
this.phrases = JSON.parse(fs.readFileSync("./phrases.json"));
}

async runChores() {
console.log("Running ChoreBot!");
const todayschores = await this.chores.getTodaysChores();
if (todayschores === -1) this.postNoChores();
else this.postChores(todayschores);
}

postChores(chores) {
this.postSlackMessage("Today's chores have been posted!", [
this.buildMarkdownSection(this.randomPhrase("greeting")),
this.buildMarkdownSection("Tonight's chores:"),
...chores.map((c, i) => this.buildChoreElement(c, i)),
this.buildMarkdownSection(this.randomPhrase("request") + " If you have any questions " +
":thinking_face: please reach out to the vice president!")
]);
}

postNoChores() {
this.postSlackMessage("No chores tonight!", [
this.buildMarkdownSection(this.randomPhrase("no_chore"))
]);
}

//helper functions

postSlackMessage(text, blocks) {
postMessage({
token: TOKEN,
channel: CHORES_CHANNEL,
text, blocks
});
}

buildChoreElement(chore, index) {
let section = this.buildMarkdownSection(`>${chore}`);
section.accessory = {
type: "button",
text: {
type: "plain_text",
text: "Done!",
emoji: true
},
action_id: `${index}`
};
return section;
}

buildMarkdownSection(text) {
return {
type: "section",
text: {
type: "mrkdwn", text
}
};
}

randomPhrase(type) {
const potentialPhrases = this.phrases[type];
if (type === "request") {
const currentMonth = (new Date()).getMonth() + 1;
if (4 <= currentMonth <= 9) {
potentialPhrases.push("After you go to the Snowman :icecream: or something, make sure that gets done!" +
" And then click the \"Done!\" button!");
}
}
return potentialPhrases[Math.floor(Math.random() * potentialPhrases.length)];
}
}

module.exports = {Notifications};
Loading