Skip to content
This repository has been archived by the owner on Jun 9, 2022. It is now read-only.

Commit

Permalink
remove trailing comma
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiebrown201 committed Mar 31, 2020
1 parent 71da0ce commit 5e0b8e3
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 32 deletions.
18 changes: 6 additions & 12 deletions data/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
return [
{
keywords: ["ill", "sick"],
emoji: [":ill:", ":face_with_head_bandage:"],
emoji: [":ill:", ":face_with_head_bandage:", ":face_with_thermometer:"],
type: "ill",
message: ":ill: *Off sick:*"
},
Expand All @@ -15,7 +15,7 @@ module.exports = {
},
{
keywords: ["home", "wfh", "working", "work"],
emoji: [":house:", ":house_with_garden:", ":computer:"],
emoji: [":house:", ":house_with_garden:", ":computer:", ":wfh:"],
type: "wfh",
message: ":house_with_garden: *WFH:*"
},
Expand Down Expand Up @@ -65,25 +65,19 @@ module.exports = {
message: ":spagbol: *On lunch:*"
},
{
keywords: ["walk", "walking", "break", "breaking"],
emoji: [":walking:", ":woman-walking:"],
keywords: ["walk", "walking", "break", "breaking", "air"],
emoji: [":walking:", ":woman-walking:", ":man-walking:"],
type: "walking",
message: ":walking: *Getting some fresh air:*"
},
{
keywords: ["caring", "child", "childcare", "entertaining"],
emoji: [":child:"],
type: "caring",
message: ":walking: *Looking after someone else:*"
message: ":child: *Looking after someone else:*"
},
{
keywords: ["caring", "child", "childcare", "entertaining"],
emoji: [":child:"],
type: "caring",
message: ":walking: *Looking after someone else:*"
},
{
keywords: ["Unavailable"],
keywords: ["unavailable"],
emoji: [":blockers:"],
type: "unavailable",
message: ":blockers: *Unavailable:*"
Expand Down
25 changes: 20 additions & 5 deletions emojiReact.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ exports.emojiReact = async function(event, context) {
const { item, reaction, user } = JSON.parse(event.Records[0].Sns.Message);
const { type, message } = getReactionType(reaction);
if (!type) {
console.log("Wrong away type");
return;
}
const messageDetails = await getItemMessage(item);
if (!messageDetails) {
throw new Error("No message being retrieved");
}
console.log("message", messageDetails);
const userDetails = await web.users.info({
user
});
Expand All @@ -31,7 +29,6 @@ exports.emojiReact = async function(event, context) {
item,
message: formattedMessage
});
console.log(updatedMessage);
return;
} catch (err) {
console.log(err);
Expand All @@ -41,7 +38,6 @@ exports.emojiReact = async function(event, context) {

async function updateMessage({ item, message }) {
const { ts, channel } = item;
console.log("update.ts", ts);
const result = await web.chat.update({ channel, ts, text: message });
return result;
}
Expand All @@ -58,7 +54,6 @@ function getReactionType(reaction) {
}

async function getItemMessage({ channel, ts }) {
console.log("ts", ts);
const messageResult = await web.conversations.history({
channel,
latest: ts,
Expand All @@ -78,13 +73,33 @@ function formatMessage({ text, name, message, type }) {

function addName({ text, name, message, type }) {
let finalMessage;

if (!text.includes(message)) {
finalMessage = text += `${message} \n ${name} \n\n`;
} else {
finalMessage = addNameToMessage({ text, name, message });
}
return finalMessage;
}

function addNameToMessage({ text, name, message }) {
const splitViaStatus = text.split(message);
const firstPart = splitViaStatus[0];
let lastPart = splitViaStatus[1].split("\n");
const originalNames = lastPart[1];
let newNames;
if (originalNames) {
newNames = name + ", " + originalNames;
} else {
newNames = name;
}
lastPart[1] = newNames;
const newLastPart = lastPart.join("\n");
const finalMessage = firstPart + message + newLastPart;

return finalMessage;
}

function removeName({ text, name }) {
return text.replace(name + ",", "").replace(name, "");
}
55 changes: 47 additions & 8 deletions post.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ const { WebClient, ErrorCode } = require("@slack/web-api");

const web = new WebClient(process.env.SLACK_TOKEN);

async function postUpdate({ text, channel }) {
async function postUpdate({ text, channel, test }) {
try {
const postingChannel = test ? test : channel;
console.log("postingChannel", postingChannel);
const result = await web.chat.postMessage({
text,
channel
channel: postingChannel
});

console.log(
`Successfully send message ${result.ts} in conversation ${channel}`
`Successfully send message ${result.ts} in conversation ${postingChannel}`
);
} catch (error) {
if (error.code === ErrorCode.PlatformError) {
Expand All @@ -24,7 +25,7 @@ async function postUpdate({ text, channel }) {
}
}

async function getMembers({ channel }) {
async function getMembers({ channel, test }) {
try {
const channelType = { G: "group", C: "channel" };
const result = await web[channelType[`${channel[0]}`] + "s"].info({
Expand All @@ -38,7 +39,14 @@ async function getMembers({ channel }) {
if (awayMessage !== "") {
const posting = await postUpdate({
text: `In the this channel today: \n\n ${awayMessage}`,
channel
channel,
test
});
} else {
const posting = await postUpdate({
text: `No one in this channel has a status set`,
channel,
test
});
}
} catch (error) {
Expand Down Expand Up @@ -127,7 +135,11 @@ function prepMessage(statuses) {
wfh: [],
ooo: [],
cake: [],
parent: []
parent: [],
caring: [],
unavailable: [],
walking: [],
lunch: []
};

statuses.forEach(status => {
Expand Down Expand Up @@ -168,6 +180,31 @@ function prepMessage(statuses) {
)}\n\n`;
}

if (userStatus.caring.length > 0) {
message += `${
AWAY_TYPES.find(obj => obj.type === "caring").message
} \n ${userStatus.caring.join(", ")}\n\n`;
}

if (userStatus.unavailable.length > 0) {
message += `${
AWAY_TYPES.find(obj => obj.type === "unavailable").message
} \n ${userStatus.unavailable.join(", ")}\n\n`;
}

if (userStatus.walking.length > 0) {
console.log("getting in walking");
message += `${
AWAY_TYPES.find(obj => obj.type === "walking").message
} \n ${userStatus.walking.join(", ")}\n\n`;
}

if (userStatus.lunch.length > 0) {
message += `${
AWAY_TYPES.find(obj => obj.type === "lunch").message
} \n ${userStatus.lunch.join(", ")}\n\n`;
}
console.log("message", message);
return message;
}

Expand All @@ -181,8 +218,10 @@ const times = x => f => {
exports.post = async function(event, context) {
try {
console.log("message", event.Records[0].Sns.Message);
const { channel, test } = JSON.parse(event.Records[0].Sns.Message);
await getMembers({
channel: event.Records[0].Sns.Message
channel,
test
});
return {
statusCode: 200,
Expand Down
46 changes: 39 additions & 7 deletions trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const AWS = require("aws-sdk");
exports.trigger = async function(event, context) {
try {
const eventBody = JSON.parse(event.body).event;
console.log("eventBody", eventBody);
if (isReactionToOtherPost(eventBody)) {
console.log("not valid emoji react");
return {
statusCode: 200,
body: "ok"
Expand All @@ -19,29 +21,56 @@ exports.trigger = async function(event, context) {
}

let sns = new AWS.SNS(snsOpts);

let messageData;
console.log("eventBody.type", eventBody.type);
switch (eventBody.type) {
case "app_mention":
console.log("eventBody", eventBody);
const eventMessage = eventBody.text
.split(">")[1]
.trim()
.toLowerCase();
let channel;
let test;
if (eventMessage[0] === "g" || eventMessage[0] === "c") {
channel = eventMessage.toUpperCase();
test = eventBody.channel;
} else {
channel = eventBody.channel;
test = "";
}
messageData = {
Message: eventBody.channel,
Message: JSON.stringify({
channel,
test
}),
TopicArn: process.env.mySnsTopicArn
};
break;
case "reaction_added":
console.log("getting into reaction");
console.log("item", eventBody.item);
console.log("eventBody.item", eventBody.item);
console.log("eventBody", eventBody);

messageData = {
Message: JSON.stringify({
item: eventBody.item,
reaction: eventBody.reaction,
user: eventBody.user
user: eventBody.user,
type: eventBody.type
}),
TopicArn: process.env.emojiReactionSnsArn
};
break;
case "reaction_removed":
console.log("eventBody", eventBody);

messageData = {
item: eventBody.item,
reaction: eventBody.reaction,
user: eventBody.user,
type: eventBody.type
};
return;
break;
}

console.log("PUBLISHING MESSAGE TO SNS:", messageData);
Expand All @@ -62,5 +91,8 @@ exports.trigger = async function(event, context) {
};

function isReactionToOtherPost({ type, item_user }) {
return type === "reaction_added" && item_user !== process.env.APP_ID;
return (
(type === "reaction_added" || type === "reaction_removed") &&
item_user !== process.env.APP_ID
);
}

0 comments on commit 5e0b8e3

Please sign in to comment.