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

Commit

Permalink
add sns emoji react
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiebrown201 committed Mar 4, 2020
1 parent 3b0da33 commit a8e2871
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 5 deletions.
18 changes: 18 additions & 0 deletions emojiReact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
exports.emojiReact = async function(event, context) {
try {
console.log(event);
console.log(JSON.stringify(event));
console.log(event.Records[0].Sns.Message);
console.log(JSON.parse(event.Records[0].Sns.Message));
return {
statusCode: 200,
body: "success"
};
} catch (err) {
console.log(err);
return {
statusCode: 401,
body: "fail"
};
}
};
2 changes: 2 additions & 0 deletions post.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ const times = x => f => {

exports.post = async function(event, context) {
try {
console.log("message", event.Records[0].Sns.Message);
await getMembers({
channel: event.Records[0].Sns.Message
});
Expand All @@ -186,6 +187,7 @@ exports.post = async function(event, context) {
body: "success"
};
} catch (err) {
console.log("error");
console.log(err);
return {
statusCode: 401,
Expand Down
33 changes: 33 additions & 0 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ custom:
port: 4002 # a free port for the sns server to run on
debug: false
mySnsTopic: "${self:service}-${self:provider.stage}-sns-consume"
emojiReactionSns: "${self:service}-${self:provider.stage}-emoji-reaction-consume"
mySnsTopicArn:
local: "arn:aws:sns:us-east-1:123456789012:${self:custom.mySnsTopic}"
dev:
Expand All @@ -31,6 +32,32 @@ custom:
],
],
}
emojiReactionSnsArn:
local: "arn:aws:sns:us-east-1:123456789012:${self:custom.emojiReactionSns}"
dev:
{
"Fn::Join":
[
"",
[
"arn:aws:sns:${self:provider.region}:",
{ "Ref": "AWS::AccountId" },
":${self:custom.emojiReactionSns}",
],
],
}
prod:
{
"Fn::Join":
[
"",
[
"arn:aws:sns:${self:provider.region}:",
{ "Ref": "AWS::AccountId" },
":${self:custom.emojiReactionSns}",
],
],
}

provider:
name: aws
Expand All @@ -48,6 +75,7 @@ provider:
environment:
NODE_ENV: "${file(./serverless.env.yml):NODE_ENV}"
SLACK_TOKEN: "${file(./serverless.env.yml):SLACK_TOKEN}"
APP_ID: "${file(./serverless.env.yml):APP_ID}"

plugins:
- serverless-offline
Expand All @@ -64,6 +92,10 @@ functions:
handler: post.post
events:
- sns: ${self:custom.mySnsTopic}
emojiReact:
handler: emojiReact.emojiReact
events:
- sns: ${self:custom.emojiReactionSns}

trigger:
handler: trigger.trigger
Expand All @@ -73,3 +105,4 @@ functions:
method: any
environment:
mySnsTopicArn: ${self:custom.mySnsTopicArn.${self:provider.stage}}
emojiReactionSnsArn: ${self:custom.emojiReactionSnsArn.${self:provider.stage}}
38 changes: 33 additions & 5 deletions trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ const AWS = require("aws-sdk");

exports.trigger = async function(event, context) {
try {
const eventBody = JSON.parse(event.body).event;
if (isReactionToOtherPost(eventBody)) {
return {
statusCode: 200,
body: "ok"
};
}

let snsOpts = {
region: "eu-west-1"
};
Expand All @@ -11,10 +19,27 @@ exports.trigger = async function(event, context) {
}

let sns = new AWS.SNS(snsOpts);
let messageData = {
Message: JSON.parse(event.body).event.channel,
TopicArn: process.env.mySnsTopicArn
};

let messageData;
switch (eventBody.type) {
case "app_mention":
messageData = {
Message: eventBody.channel,
TopicArn: process.env.mySnsTopicArn
};
break;
case "reaction_added":
console.log("getting into reaction");
console.log();
messageData = {
Message: JSON.stringify({
item: eventBody.item,
reaction: eventBody.reaction
}),
TopicArn: process.env.emojiReactionSnsArn
};
break;
}

console.log("PUBLISHING MESSAGE TO SNS:", messageData);
await sns.publish(messageData).promise();
Expand All @@ -25,11 +50,14 @@ exports.trigger = async function(event, context) {
body: "ok"
};
} catch (err) {
console.log("error", err);
console.error(err);
return {
statusCode: 200,
body: "ok"
};
}
};

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

0 comments on commit a8e2871

Please sign in to comment.