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
/
post.js
210 lines (185 loc) · 4.82 KB
/
post.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
if (process.env.NODE_ENV !== "production") require("dotenv").config();
const { WebClient, ErrorCode } = require("@slack/web-api");
const { AWAY_TYPES } = require("./data/types.js");
const { MESSAGE } = require("./data/message.js");
const web = new WebClient(process.env.SLACK_TOKEN);
async function postUpdate({ text, channel, test }) {
try {
const postingChannel = test ? test : channel;
const result = await web.chat.postMessage({
text,
channel: postingChannel,
unfurl_links: false
});
console.log(
`Successfully send message ${result.ts} in conversation ${postingChannel}`
);
return result.ts;
} catch (error) {
if (error.code === ErrorCode.PlatformError) {
console.log(error.data);
} else {
console.log("Unexpected error in postUpdate()");
}
}
}
async function getMembers({ channel, test }) {
try {
const channelType = { G: "group", C: "channel" };
const result = await web[channelType[`${channel[0]}`] + "s"].info({
channel
});
const members = result[channelType[`${channel[0]}`]].members;
const memberSelection = await getStatuses(members);
const awayMessage = prepMessage(memberSelection);
let posting;
if (awayMessage !== "") {
posting = await postUpdate({
text: `In the this channel today: \n\n ${awayMessage}`,
channel,
test
});
} else {
posting = await postUpdate({
text: `No one in this channel has a status set`,
channel,
test
});
}
return posting;
} catch (error) {
if (error.code === ErrorCode.PlatformError) {
console.log(error.data);
} else {
console.log(error);
console.log("Unexpected error in getMembers()");
}
}
}
async function getStatuses(members) {
const results = [];
for (let i = 0; i < members.length; ++i) {
try {
const memberProfile = await web.users.info({
user: members[i]
});
const memberStatus = checkStatus(memberProfile.user.profile);
if (memberStatus) {
results.push(memberStatus);
}
} catch (error) {
if (error.code === ErrorCode.PlatformError) {
console.log(error.data);
} else {
console.log("Unexpected error in getStatuses()");
}
}
}
return results;
}
function checkStatus(profile) {
let status = "";
const statusText = profile.status_text.toLowerCase();
const statusEmoji = profile.status_emoji;
for (let i = 0; i < AWAY_TYPES.length; ++i) {
const type = AWAY_TYPES[i];
const matchingWords = type.keywords.filter(word => {
return statusText.split(" ").includes(word);
});
if (matchingWords.length > 0) {
console.log(`MATCHING KEYWORD ${type.type} :: ${profile.real_name}`);
status = type.type;
break;
}
const matchingEmoji = type.emoji.filter(emoji => {
return statusEmoji.includes(emoji);
});
if (matchingEmoji.length > 0) {
console.log(`MATCHING EMOJI ${type.type} :: ${profile.real_name}`);
status = type.type;
break;
}
}
if (status !== "") {
return {
user: profile.real_name,
awayStatus: status
};
}
return null;
}
function prepMessage(statuses) {
const userStatus = {
holiday: [],
ill: [],
wfh: [],
ooo: [],
cake: [],
parent: [],
caring: [],
unavailable: [],
walking: [],
lunch: []
};
statuses.forEach(status => {
userStatus[status.awayStatus].push(status.user);
});
let message = "";
Object.keys(userStatus).forEach(key => {
message += addStatus({
statuses: userStatus[key],
statusType: key
});
});
message += MESSAGE.ending;
return message;
}
function addStatus({ statuses, statusType }) {
message = "";
if (statuses.length > 0) {
message += `${
AWAY_TYPES.find(typeObj => typeObj.type === statusType).message
}\n`;
message += `${statuses.join(", ")}`;
message += `\n\n`;
}
return message;
}
async function addReactions({ channel, timestamp }) {
for (let i = 0; i < AWAY_TYPES.length; ++i) {
try {
const result = await web.reactions.add({
channel,
timestamp,
name: AWAY_TYPES[i].defaultEmoji
});
} catch (error) {
if (error.code === ErrorCode.PlatformError) {
console.log(error.data);
} else {
console.log("Unexpected error in addReactions()");
}
}
}
}
exports.post = async function(event, context) {
try {
const { channel, test } = JSON.parse(event.Records[0].Sns.Message);
const timestamp = await getMembers({
channel,
test
});
await addReactions({ channel, timestamp });
return {
statusCode: 200,
body: "success"
};
} catch (err) {
console.log("error");
console.log(err);
return {
statusCode: 401,
body: "fail"
};
}
};