-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
223 lines (181 loc) · 7.02 KB
/
index.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
211
212
213
214
215
216
217
218
219
220
221
222
223
// 1. This implements most of the code, watching for slash commands, sets up event handling for various scenarios:
// a) /fqscores = display the table
// b) /fqscores @user number - updates the db table adding the number to the users existing score or adding the user
// if they don't already exist
// c) /fqscores @user :emoji: - updates the db table adding the emoji to the users existing emoji array or adding the user
// if they don't already exist
// 2) any other command gives the user an error
// 3) running the command in any channel other than #friday-question gives the user an error
"use strict";
const ts = require('./src/tinyspeck.js'),
users = {},
datastore = require("./src/datastore.js").async,
RtmClient = require('@slack/client').RTMClient;
require('dotenv').config();
var slack = ts.instance({});
var connected = false;
var message;
var twss = require('./src/twss.js');
// receive the /fqscores command and process it
slack.on('/fqscores', payload => {
console.log("Received /fqscores slash command from user " + payload.user_id);
// get all the data items we're interested - username, points, emoji, comments
let channel = payload.channel_name;
let user_id = payload.user_id;
let user_name = payload.user_name;
let response_url = payload.response_url;
let text = payload.text;
let splitText = text.split(" ");
let userAwardedPoints = splitText[0].toLowerCase();
let pointsAwarded = splitText[1];
let comment = '';
for (var i = 2; i < splitText.length; i++) {
comment = comment + ' ' + splitText[i];
}
if (channel === "friday-question") {
if (userAwardedPoints === '') {
console.log("displaying table");
getConnected() // make sure we have a database connection
.then(function() {
datastore.getAll(function(result) {
let message = getResults(result, user_name);
slack.send(response_url, message).then(res => { // on success
console.log("Response sent to /fqscores slash command");
}, reason => { // on failure
console.log("An error occurred when responding to /fqscores slash command: " + reason);
});
});
});
} else if (typeof(pointsAwarded) == "string" && pointsAwarded.charAt(0) == ':' && pointsAwarded.charAt(pointsAwarded.length - 1) == ':') {
console.log("adding emoji");
let message = Object.assign({
"response_type": "in_channel",
text: userAwardedPoints + " has been awarded a " + pointsAwarded + " by @" + payload.user_name + comment
});
console.log("message: " + message);
getConnected()
.then(function() {
datastore.setEmoji(userAwardedPoints, pointsAwarded);
datastore.get(userAwardedPoints)
.catch(function(e) {
if (e.type = "DatastoreDataParsingException") {
datastore.setScore(userAwardedPoints, 0);
}
});
slack.send(response_url, message).then(res => {
console.log("Response sent to /fqscores slash command");
}, reason => {
console.log("An error occurred when responding to /fqscores slash command: " + reason);
});
});
} else if (isNaN(pointsAwarded) == false) {
console.log("updating points for user");
getConnected()
.then(function() {
datastore.get(userAwardedPoints)
.then(function(score) {
let message = Object.assign({
"response_type": "in_channel",
text: userAwardedPoints + " has been awarded " + Number(pointsAwarded).toLocaleString() + " points by @" + payload.user_name + comment
});
console.log("message: " + message);
let newScore = Number(score) + Number(pointsAwarded);
datastore.setScore(userAwardedPoints, newScore);
slack.send(response_url, message).then(res => {
console.log("Response sent to /fqscores slash command");
}, reason => {
console.log("An error occurred when responding to /fqscores slash command: " + reason);
});
});
});
} else {
console.log("invalid instruction");
let message = Object.assign({
text: "Sorry. That's not a valid instruction. Try a little harder next time. (That's what she said.)"
});
slack.send(response_url, message).then(res => {
console.log("Response sent to /fqscores slash command");
}, reason => {
console.log("An error occurred when responding to /fqscores slash command: " + reason);
});
}
} else {
let message = Object.assign({
text: "This command only works in the #friday-question channel. If you would like to know more, come and talk to us. We're a friendly bunch."
});
slack.send(response_url, message).then(res => {
console.log("Response sent to /fqscores slash command");
}, reason => {
console.log("An error occurred when responding to /fqscores slash command: " + reason);
});
console.log("not fq");
}
});
function getResults(result, user_name) {
var resultText = "*The Friday Question Scores, as requested by @" + user_name + ":*\n";
for (var i = 0; i < result.length; i++) {
var obj = result[i];
resultText = resultText + (i + 1) + ". " + obj.name + ": " + Number(obj.score).toLocaleString();
if (typeof(obj.emojis) != "undefined") {
resultText = resultText + " and";
for (var j = 0; j < obj.emojis.length; j++) {
resultText = resultText + " " + obj.emojis[j];
}
}
resultText = resultText + "\n";
}
return Object.assign({
"response_type": "in_channel",
text: resultText
});
}
function getConnected() {
return new Promise(function(resolving) {
if (!connected) {
connected = datastore.connect().then(function() {
resolving();
});
} else {
resolving();
}
});
}
let rtm = new RtmClient(process.env.SLACK_API_TOKEN, {
logLevel: 'error',
useRtmConnect: true,
dataStore: false,
autoReconnect: true,
autoMark: true
});
rtm.start();
rtm.on('connected', () => {
console.log('Connected!');
});
// rtm.on('message', (message) => {
// let channel = message.channel;
// let text = message.text;
// let user = message.user;
// let type = message.type;
// let subtype = message.subtype;
// let thread_ts = message.thread_ts;
// let ts = message.ts;
// if (typeof(user) != "undefined") { // ignore bot messages
// console.log(">>>> channel: " + channel);
// twss.threshold = 0.8;
// let isTwss = twss.is(text);
// let prob = twss.prob(text);
// console.log("twss: " + prob);
// if (isTwss) {
// rtm.addOutgoingEvent(true,
// "message", {
// text: ":twss:",
// channel: channel,
// thread_ts: ts
// })
// .then(res => console.log(`Message sent: ${res}`))
// .catch(console.error);
// }
// }
// });
// incoming http requests
slack.listen(process.env.PORT || '3000');