Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bot saves drawing submissions #30

Merged
merged 4 commits into from
Jul 27, 2017
Merged
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 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