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
179 lines (149 loc) · 4.04 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
if (process.env.NODE_ENV !== "production") require("dotenv").config();
const { WebClient, ErrorCode } = require("@slack/web-api");
const web = new WebClient(process.env.SLACK_TOKEN);
async function postUpdate({ text, channel }) {
try {
const result = await web.chat.postMessage({
text,
channel
});
console.log(
`Successfully send message ${result.ts} in conversation ${channel}`
);
} catch (error) {
if (error.code === ErrorCode.PlatformError) {
console.log(error.data);
} else {
console.log("Unexpected error in postUpdate()");
}
}
}
async function getMembers({ channel }) {
try {
const result = await web.groups.info({
channel
});
const memberSelection = await getStatuses(result.group.members);
const awayMessage = prepMessage(memberSelection);
if (awayMessage !== "") {
const posting = await postUpdate({
text: `In the this channel today: \n\n ${awayMessage}`,
channel
});
}
} catch (error) {
if (error.code === ErrorCode.PlatformError) {
console.log(error.data);
} else {
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;
}
const { AWAY_TYPES } = require("./data/types.js");
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: []
};
statuses.forEach(status => {
userStatus[status.awayStatus].push(status.user);
});
let message = "";
if (userStatus.holiday.length > 0) {
message += `:palm_tree: *On holiday:* \n ${userStatus.holiday.join(
", "
)} \n\n`;
}
if (userStatus.wfh.length > 0) {
message += `:house_with_garden: *WFH:* \n ${userStatus.wfh.join(", ")}\n\n`;
}
if (userStatus.ooo.length > 0) {
message += `:no_entry_sign: *Out of office:* \n ${userStatus.ooo.join(
", "
)}\n\n`;
}
if (userStatus.parent.length > 0) {
message += `:baby: *On parental leave:* \n ${userStatus.parent.join(
", "
)}\n\n`;
}
if (userStatus.ill.length > 0) {
message += `:ill: *Off sick:* \n ${userStatus.ill.join(", ")}\n\n`;
}
if (userStatus.cake.length > 0) {
message += `:cake: *Would like some cake:* \n ${userStatus.cake.join(
", "
)}\n\n`;
}
return message;
}
exports.post = async function(event, context) {
console.log("EVENT: \n" + JSON.stringify(event, null, 2));
console.log(
"JSON.parse(event.body).event.channel",
JSON.parse(event.body).event.channel
);
const result = await getMembers({
channel: JSON.parse(event.body).event.channel
});
return {
statusCode: 200,
body: "success"
};
};