/
main.js
147 lines (130 loc) · 3.29 KB
/
main.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
// load .env
require('dotenv').config();
const SLACK_TOKEN = process.env.SLACK_TOKEN;
const SLACK_BOT_TOKEN = process.env.SLACK_BOT_TOKEN;
const SEND_CHANNEL = process.env.SEND_CHANNEL;
const util = require('util');
const slack = require('slack');
const moment = require('moment-timezone');
const CronJob = require('cron').CronJob;
function getChannels(oldest, latest) {
return new Promise(function(onFulfilled, onRejected) {
const param = {
token: SLACK_TOKEN,
exclude_archived: true,
types: 'public_channel, private_channel'
};
slack.conversations.list(param).then(response => {
onFulfilled({
channels: response.channels.map(channel => channel.id),
oldest: oldest,
latest: latest
});
});
});
}
function getReactions(responses) {
return new Promise(function(onFulfilled, onRejected) {
reactions = [];
const channels = responses.channels;
const oldest = responses.oldest;
const latest = responses.latest;
let i = 0;
channels.map(channel => {
slack.conversations
.history({
token: SLACK_TOKEN,
channel: channel,
oldest: oldest,
latest: latest
})
.then(response => {
response.messages.map(message => {
if ('reactions' in message) {
reactions = reactions.concat(message.reactions);
}
});
if (++i == channels.length) {
onFulfilled(reactions);
}
})
.catch(error => {
console.log('error!!');
});
});
});
}
sortResult = reactions => {
// counter
totalReaction = [];
reactions.map(reaction => {
name = ':' + reaction.name + ':';
if (name in totalReaction === false) {
totalReaction[name] = 0;
}
totalReaction[name] += reaction.count;
});
// convert for sort
sortReaction = [];
for (name in totalReaction) {
sortReaction.push({
name: name,
count: totalReaction[name]
});
}
sortReaction = sortReaction.sort((a, b) => {
if (a.count > b.count) return -1;
else if (a.count < b.count) return 1;
else return 0;
});
// create send message
msg = '';
for (reaction of sortReaction) {
msg += reaction.name + ' : ' + reaction.count + '\n';
}
return msg;
};
postMessage = () => {
// oldest ※前日0時0分
oldest = moment()
.subtract(1, 'days')
.startOf('day');
latest = moment().startOf('day');
getChannels(oldest.unix(), latest.unix())
.then(getReactions)
.then(reactions => {
if (reactions.length === 0) {
msg =
'Good Morning!! ' +
oldest.format('M/D(ddd)') +
'のリアクションは・・・なしですyo〜';
} else {
msg =
'Good Morning!! ' +
oldest.format('M/D(ddd)') +
'のリアクション集計したyo〜\n\n';
msg += sortResult(reactions);
}
console.log(msg);
slack.chat.postMessage({
token: SLACK_BOT_TOKEN,
channel: SEND_CHANNEL,
text: msg
});
});
};
const job = new CronJob({
/*
Seconds: 0-59
Minutes: 0-59
Hours: 0-23
Day of Month: 1-31
Months: 0-11
Day of Week: 0-6
*/
cronTime: '0 0 9 * * *',
onTick: postMessage,
start: false,
timeZone: 'Asia/Tokyo'
});
job.start();