This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
trigger.js
105 lines (96 loc) · 2.72 KB
/
trigger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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) || appEmojiReact(eventBody)) {
console.log("not valid emoji react");
return {
statusCode: 200,
body: "ok"
};
}
let snsOpts = {
region: "eu-west-1"
};
if (process.env.IS_OFFLINE) {
snsOpts.endpoint = "http://127.0.0.1:4002";
}
let sns = new AWS.SNS(snsOpts);
let messageData;
console.log("eventBody.type", eventBody.type);
switch (eventBody.type) {
case "app_mention":
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: JSON.stringify({
channel,
test
}),
TopicArn: process.env.mySnsTopicArn
};
break;
case "reaction_added":
console.log("eventBody.item", eventBody.item);
console.log("eventBody", eventBody);
messageData = {
Message: JSON.stringify({
item: eventBody.item,
reaction: eventBody.reaction,
user: eventBody.user,
eventType: eventBody.type
}),
TopicArn: process.env.emojiReactionSnsArn
};
break;
case "reaction_removed":
console.log("Reaction removed");
console.log("eventBody", eventBody);
messageData = {
Message: JSON.stringify({
item: eventBody.item,
reaction: eventBody.reaction,
user: eventBody.user,
eventType: eventBody.type
}),
TopicArn: process.env.emojiReactionSnsArn
};
console.log("messageData", messageData);
break;
}
console.log("PUBLISHING MESSAGE TO SNS:", messageData);
await sns.publish(messageData).promise();
console.log("PUBLISHED MESSAGE TO SNS:", messageData);
return {
statusCode: 200,
body: "ok"
};
} catch (err) {
console.error(err);
return {
statusCode: 200,
body: "ok"
};
}
};
function isReactionToOtherPost({ type, item_user }) {
return (
(type === "reaction_added" || type === "reaction_removed") &&
item_user !== process.env.APP_ID
);
}
function appEmojiReact({ user, type }) {
return type === "reaction_added" && user === process.env.APP_ID;
}