Skip to content

Commit

Permalink
Bot saves drawing submissions (#30)
Browse files Browse the repository at this point in the history
* add code for storing info about drawing submissions

* shuffle functions around

so handleDrawingSubmission isn't redefined all the time

* filter out non-image submissions

* remove a console.log statement
  • Loading branch information
adamsullovey authored and teesloane committed Jul 27, 2017
1 parent 410c60e commit 90f110b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
1 change: 1 addition & 0 deletions drawings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
28 changes: 28 additions & 0 deletions src/drawing-handlers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const fs = require("fs");
const { promisify } = require("util");

async function addImageToList(drawingInfo) {
// throws error if file doesn't exist
const drawingsListJson = await promisify(fs.readFile)(
"./drawings.json",
"utf-8"
);

// throws error if file is blank
let drawingsList = JSON.parse(drawingsListJson);

// just in case drawings.json has some other type at the root of structure
if (!Array.isArray(drawingsList)) {
drawingsList = [];
}

drawingsList.push(drawingInfo);

const newJson = JSON.stringify(drawingsList, null, 2);

return promisify(fs.writeFile)("./drawings.json", newJson);
}

module.exports = {
addImageToList
};
33 changes: 32 additions & 1 deletion src/rtm-handlers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require("fs");
const prompts = require("./prompt-handlers");
const drawings = require("./drawing-handlers");
const RtmClient = require("@slack/client").RtmClient;
const CLIENT_EVENTS = require("@slack/client").CLIENT_EVENTS;
const RTM_EVENTS = require("@slack/client").RTM_EVENTS;
Expand Down Expand Up @@ -77,17 +78,47 @@ function onReceiveMessage(env) {
responseText =
"Hi I'm Penny; I can do the following if you `@` mention me!\n";
responseText += "`@penny_bot give prompt` \n";
responseText += "`@penny_bot, submit prompt '<your prompt here>'`";
responseText += "`@penny_bot, submit prompt '<your prompt here>'`\n ";
responseText +=
"You can also DM me your drawings as attachments with the comment `submit drawing`";

// Channel to respond in
message.channel == "C63GFH05V"
? env.rtm.sendMessage(responseText, "C63GFH05V")
: env.rtm.sendMessage(responseText, message.channel);
}
}

/**
* DMs to penny with attachments might not include penny's user id in the
* message, so this check happens outside the other if statement
*/
if (checkMessage("submit drawing") && message.channel === "D63S3TAM7") {
handleDrawingSubmission(env, message);
}
});
}

const validFileTypes = ["jpg", "png", "gif"];

function handleDrawingSubmission(env, message) {
if (!message.file) {
env.rtm.sendMessage("Where is drawing?", message.channel);
} else if (message.file && !validFileTypes.includes(message.file.filetype)) {
env.rtm.sendMessage(
"I don't think you attached a drawing. Make sure you send me a .png, .jpg, or .gif",
message.channel
);
} else {
env.rtm.sendMessage("Got drawing!", message.channel);

drawings
.addImageToList(message.file)
.then(result => console.log("saved image info", result))
.catch(error => console.error("error", error));
}
}

function startRtm(env) {
env.rtm.start();
}
Expand Down

0 comments on commit 90f110b

Please sign in to comment.