From 85dceca99f3d9808b18ca79e04c47e778be92aa8 Mon Sep 17 00:00:00 2001 From: Adam Sullovey Date: Wed, 26 Jul 2017 14:59:04 -0400 Subject: [PATCH 1/4] add code for storing info about drawing submissions --- drawings.json | 1 + src/drawing-handlers.js | 28 ++++++++++++++++++++++++++++ src/rtm-handlers.js | 30 +++++++++++++++++++++++++++++- 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 drawings.json create mode 100644 src/drawing-handlers.js diff --git a/drawings.json b/drawings.json new file mode 100644 index 0000000..0637a08 --- /dev/null +++ b/drawings.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/src/drawing-handlers.js b/src/drawing-handlers.js new file mode 100644 index 0000000..985d51b --- /dev/null +++ b/src/drawing-handlers.js @@ -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 +}; diff --git a/src/rtm-handlers.js b/src/rtm-handlers.js index 8ed22ae..05a82fc 100644 --- a/src/rtm-handlers.js +++ b/src/rtm-handlers.js @@ -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; @@ -38,11 +39,28 @@ function onOpenConnection(env) { // Handle receiving a message // Check if the message includes penny's ID. If it does, send a message to the draw_it channel function onReceiveMessage(env) { + // this function is nested in onReceiveMessage for easy access to the env variable + function handleDrawingSubmission(message) { + // console.log('handleDrawingSubmission', message.file); + if (!message.file) { + env.rtm.sendMessage("Where is drawing?", 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)); + } + } + env.rtm.on(RTM_EVENTS.MESSAGE, function handleRtmMessage(message) { function checkMessage(msg) { return message.text.includes(msg); } + console.log(message); + // TODO: Turn this into a nice ol' switch case if (message.text.includes(`<@${env.penny.id}>`)) { let responseText = ""; @@ -77,7 +95,9 @@ 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 ''`"; + responseText += "`@penny_bot, submit prompt ''`\n "; + responseText += + "You can also DM me your drawings as attachments with the comment `submit drawing`"; // Channel to respond in message.channel == "C63GFH05V" @@ -85,6 +105,14 @@ function onReceiveMessage(env) { : 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(message); + } }); } From 43eefc083ee6d5d84f70a3a42381061abcd52c20 Mon Sep 17 00:00:00 2001 From: Adam Sullovey Date: Wed, 26 Jul 2017 20:34:32 -0400 Subject: [PATCH 2/4] shuffle functions around so handleDrawingSubmission isn't redefined all the time --- src/rtm-handlers.js | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/rtm-handlers.js b/src/rtm-handlers.js index 05a82fc..0ea3310 100644 --- a/src/rtm-handlers.js +++ b/src/rtm-handlers.js @@ -39,21 +39,6 @@ function onOpenConnection(env) { // Handle receiving a message // Check if the message includes penny's ID. If it does, send a message to the draw_it channel function onReceiveMessage(env) { - // this function is nested in onReceiveMessage for easy access to the env variable - function handleDrawingSubmission(message) { - // console.log('handleDrawingSubmission', message.file); - if (!message.file) { - env.rtm.sendMessage("Where is drawing?", 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)); - } - } - env.rtm.on(RTM_EVENTS.MESSAGE, function handleRtmMessage(message) { function checkMessage(msg) { return message.text.includes(msg); @@ -111,11 +96,25 @@ function onReceiveMessage(env) { * message, so this check happens outside the other if statement */ if (checkMessage("submit drawing") && message.channel === "D63S3TAM7") { - handleDrawingSubmission(message); + handleDrawingSubmission(env, message); } }); } +function handleDrawingSubmission(env, message) { + // console.log('handleDrawingSubmission', message.file); + if (!message.file) { + env.rtm.sendMessage("Where is drawing?", 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(); } From 193766b9015dcb259411de24329f34ce880c9fa4 Mon Sep 17 00:00:00 2001 From: Adam Sullovey Date: Thu, 27 Jul 2017 17:44:28 -0400 Subject: [PATCH 3/4] filter out non-image submissions --- src/rtm-handlers.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/rtm-handlers.js b/src/rtm-handlers.js index 0ea3310..36b8146 100644 --- a/src/rtm-handlers.js +++ b/src/rtm-handlers.js @@ -101,10 +101,16 @@ function onReceiveMessage(env) { }); } +const validFileTypes = ["jpg", "png", "gif"]; + function handleDrawingSubmission(env, message) { - // console.log('handleDrawingSubmission', message.file); 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); From 60475c7a9da1abbd37bcc2a2bed2e656984b40fc Mon Sep 17 00:00:00 2001 From: Adam Sullovey Date: Thu, 27 Jul 2017 17:48:35 -0400 Subject: [PATCH 4/4] remove a console.log statement --- src/rtm-handlers.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/rtm-handlers.js b/src/rtm-handlers.js index 36b8146..7a26e25 100644 --- a/src/rtm-handlers.js +++ b/src/rtm-handlers.js @@ -44,8 +44,6 @@ function onReceiveMessage(env) { return message.text.includes(msg); } - console.log(message); - // TODO: Turn this into a nice ol' switch case if (message.text.includes(`<@${env.penny.id}>`)) { let responseText = "";