-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.js
111 lines (94 loc) · 3.2 KB
/
handler.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
'use strict';
Promise = require('bluebird');
var rp = require('request-promise');
var fs = require('fs');
module.exports.run = (event, context) => {
var memberBlacklist = [
"U34BXH2P7", // -> plusplus
"U32D1PZ08", // -> ed
"U7G33RG22" // -> brunna
];
const SLACK_CHANNEL_ID = "";
const GROUP_INFO_URL = 'https://slack.com/api/groups.info';
const WEBHOOK_URL = 'https://hooks.slack.com/services/xxx/xxx/xxx';
const TOKEN = "";
var getRandomUser = function() {
console.log("getRandomUser()");
const options = {
method: 'POST',
uri: GROUP_INFO_URL,
simple: false,
resolveWithFullResponse: true,
headers: {
"content-type": "multipart/form-data"
},
form: {
token: TOKEN,
channel: SLACK_CHANNEL_ID
}
};
return rp(options).
then(r => {
var body = JSON.parse(r.body);
var randomIndex = Math.floor(Math.random() * (body.group.members.length));
console.log("[getRandomUser] userList[" + randomIndex + "]: " + body.group.members[randomIndex]);
console.log("memberBlacklist: " + memberBlacklist);
while (memberBlacklist.indexOf(body.group.members[randomIndex]) > -1){
console.log("User Blacklisted - Reroll");
randomIndex = Math.floor(Math.random() * (body.group.members.length));
console.log("[getRandomUser] userList[" + randomIndex + "]: " + body.group.members[randomIndex]);
}
var questionArr = [body.group.members[randomIndex]];
console.log("just configuring the random index " + randomIndex);
return questionArr;
})
}
var getRandomQuestion = function(questionArr) {
return new Promise(function(resolve, reject) {
var filename = "questions.txt"
console.log("[getRandomQuestion] opening filename: " + filename);
return fs.readFile(filename, function(err, data) {
if (err) reject(err);
data = data += '';
var lines = data.split('\n');
var line = lines[Math.floor(Math.random() * lines.length)];
console.log("[getRandomQuestion] Got Line: " + line);
questionArr.push(line);
resolve(questionArr);
});
});
}
var createDirectedQuestion = function(questionArr) {
var memberId = questionArr[0];
var line = questionArr[1];
return new Promise(function(resolve, reject) {
var regex = /\d+\) /;
line = line.replace(regex, "");
console.log("[createDirectedQuestion] Line: " + line);
console.log("[createDirectedQuestion] memberId: " + memberId);
var postString = "<@" + memberId + ">: " + line;
console.log("[createDirectedQuestion] directedQuestion: " + postString);
resolve(postString);
});
}
var postMessage = function(postString) {
console.log("[postMessage] postString: " + postString);
const options = {
method: 'POST',
uri: WEBHOOK_URL,
body: {
text: postString
},
json: true // Automatically stringifies the body to JSON
};
return rp(options)
.then(r => {
console.log("[postMessage] Response: " + r);
})
}
// Let's go Harold!
getRandomUser()
.then(getRandomQuestion)
.then(createDirectedQuestion)
.then(postMessage)
};