This repository has been archived by the owner on Aug 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbot.js
123 lines (108 loc) · 4.22 KB
/
bot.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
var MongoClient = require('mongodb').MongoClient;
function Bot(rtm, request, token) {
var startTime = new Date();
var queue;
var url;
if(process.env.VCAP_SERVICES) {
vcapServices = JSON.parse(process.env.VCAP_SERVICES);
url = vcapServices["mongodb32"][0].credentials.uri
} else if(process.env.MONGODB_URL) {
url = process.env.MONGODB_URL
} else {
url = 'mongodb://localhost:27017/coffeemate';
}
function emptyQueue(callback) {
MongoClient.connect(url, function(err, db) {
queue = db.collection("queue");
queue.remove({}, function(err, result) {
db.close();
callback();
});
});
}
function openGroupChat(coffeemateId, coffeeQueue) {
var users = coffeemateId + ',' + coffeeQueue[0].user + ',' + coffeeQueue[1].user;
var options = {url: "https://slack.com/api/mpim.open",
qs: {token: token, users: users}};
request.get(options, function(err, response, body) {
bodyObj = JSON.parse(body);
if(bodyObj.ok) {
rtm.sendMessage("Hey! You two have been paired up for coffee. Next step is to figure out a time that works for both of you. Enjoy! :coffee:"
, bodyObj.group.id);
}
});
}
function listenForPrompts(coffeemateId) {
rtm.start();
var RTM_EVENTS = require('@slack/client').RTM_EVENTS;
rtm.on(RTM_EVENTS.MESSAGE, function (message) {
if(message.type === 'message' && message.text && message.text.toLowerCase().indexOf('coffee me') >= 0
&& message.user !== coffeemateId) {
queue.insert({user: message.user, available: true, timestamp: new Date()}, function(err, result) {
queue.find({available: true}).toArray(function(err, coffeeQueue) {
if((coffeeQueue.length % 2) === 1) {
rtm.sendMessage("You’re in line for coffee! " +
"You’ll be introduced to the next person who wants to meet up.", message.channel);
}
if(coffeeQueue.length >= 2) {
nPairs = Math.floor(coffeeQueue.length / 2);
for(var pairIdx = 0; pairIdx < nPairs; pairIdx++) {
partner1 = pairIdx*2
partner2 = pairIdx*2 + 1
coffeeQueue[partner1].available = false;
coffeeQueue[partner2].available = false;
queue.update({_id: coffeeQueue[partner1]._id}, coffeeQueue[partner1]);
queue.update({_id: coffeeQueue[partner2]._id}, coffeeQueue[partner2]);
if(coffeeQueue[partner1].user !== coffeeQueue[partner2].user) {
rtm.sendMessage(
"You’ve been matched up for coffee with <@" + coffeeQueue[partner1].user + ">! " +
"I’ll start a direct message for you two. :coffee: :tada:",
message.channel);
openGroupChat(coffeemateId, coffeeQueue, rtm);
} else {
rtm.sendMessage("You’re no longer in line for coffee. " +
"But go ahead and pour yourself a cup—you deserve a break.",
message.channel);
}
}
}
});
});
} else {
if(message.type === 'message' && message.text && message.text.indexOf('coffee queue') >= 0) {
queue.find({available: true}).toArray(function(err, coffeeQueue) {
rtm.sendMessage("People in line for coffee: " + coffeeQueue.length,
message.channel);
});
} else {
if(message.type === 'message' && message.text &&
(message.text.indexOf(coffeemateId) >= 0 || message.channel[0] == 'D')) {
rtm.sendMessage("Sorry, I didn’t get that. To set up a virtual coffee, " +
"direct message me and say `coffee me`, and I’ll match you up with a teammate. " +
"You can also post `coffee me` in any channel I’m invited to.",
message.channel);
}
}}});}
function run() {
MongoClient.connect(url, function(err, db) {
if(err) {
throw err;
}
module.exports.db = db;
queue = db.collection("queue");
queue.ensureIndex('available');
var options = {url: "https://slack.com/api/users.list",
qs: {token: token}};
request.get(options, function(err, response, body) {
bodyObj = JSON.parse(body);
if(bodyObj.ok) {
var coffeemateId = bodyObj.members.filter(function(member) {
return member.name === 'coffeemate'})[0].id
listenForPrompts(coffeemateId);
}
})
});
}
return {run: run, emptyQueue: emptyQueue};
}
module.exports.Bot = Bot;