Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
ee14948
conflict
jackkimball13 Jul 20, 2017
e1efc8f
conflict
jackkimball13 Jul 20, 2017
f5bbd13
conflict
jackkimball13 Jul 20, 2017
2a31a22
conflict
jackkimball13 Jul 20, 2017
ca4ed44
conflict
jackkimball13 Jul 20, 2017
c264b58
conflict
jackkimball13 Jul 20, 2017
1c47268
conflict
jackkimball13 Jul 20, 2017
e036dfb
conflict
jackkimball13 Jul 20, 2017
abda797
conflict
jackkimball13 Jul 20, 2017
4f5b0a5
conflict
jackkimball13 Jul 20, 2017
cd6ad9b
conflict
jackkimball13 Jul 20, 2017
5b84526
conflict
jackkimball13 Jul 20, 2017
194ec9f
conflict
jackkimball13 Jul 20, 2017
16ca73c
conflict
jackkimball13 Jul 20, 2017
55e615b
conflict
jackkimball13 Jul 20, 2017
445dea9
conflict
jackkimball13 Jul 20, 2017
553ef80
conflict
jackkimball13 Jul 21, 2017
1c626ce
conflict
jackkimball13 Jul 21, 2017
ef4c85e
conflict
jackkimball13 Jul 21, 2017
f57aeb0
conflict
jackkimball13 Jul 21, 2017
17b4e47
conflict
jackkimball13 Jul 21, 2017
d8c9144
conflict
jackkimball13 Jul 21, 2017
b48dd39
conflict
jackkimball13 Jul 21, 2017
7085205
conflict
jackkimball13 Jul 21, 2017
a04ad6b
conflict
jackkimball13 Jul 21, 2017
228b7d5
conflict
jackkimball13 Jul 21, 2017
af1ee29
conflict
jackkimball13 Jul 21, 2017
2400fad
conflict
jackkimball13 Jul 21, 2017
2499ceb
conflict
jackkimball13 Jul 21, 2017
e72d1c2
conflict
jackkimball13 Jul 21, 2017
e787e13
conflict
jackkimball13 Jul 21, 2017
bd22d6a
conflict
jackkimball13 Jul 21, 2017
4383f99
conflict
jackkimball13 Jul 21, 2017
f138194
conflict
jackkimball13 Jul 21, 2017
a7c6e82
conflict
jackkimball13 Jul 21, 2017
8dec1d2
conflict
jackkimball13 Jul 21, 2017
241b7e8
conflict
jackkimball13 Jul 21, 2017
914a7cf
conflict
jackkimball13 Jul 21, 2017
071ce96
conflict
jackkimball13 Jul 21, 2017
895e62e
conflict
jackkimball13 Jul 21, 2017
c6a3848
conflict
jackkimball13 Jul 21, 2017
a33482c
conflict
jackkimball13 Jul 21, 2017
83a9394
conflict
jackkimball13 Jul 21, 2017
b0e8439
conflict
jackkimball13 Jul 21, 2017
2f8af05
conflict
jackkimball13 Jul 21, 2017
5f2ba47
conflict
jackkimball13 Jul 21, 2017
5c0fd27
conflict
jackkimball13 Jul 21, 2017
7365d9d
conflict
jackkimball13 Jul 21, 2017
f1f7fca
conflict
jackkimball13 Jul 21, 2017
00df21d
conflict
jackkimball13 Jul 21, 2017
6def7ce
time conflicts with multiple people
jackkimball13 Jul 21, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ rtm.on(CLIENT_EVENTS.RTM.RTM_CONNECTION_OPENED, () => {
// things to do when the bot connects to slack
});

const mapping = {};
let mapping = {};

rtm.on(RTM_EVENTS.MESSAGE, (msg) => {
const dm = rtm.dataStore.getDMByUserId(msg.user);
Expand Down Expand Up @@ -107,8 +107,10 @@ rtm.on(RTM_EVENTS.MESSAGE, (msg) => {
} else {
console.log('Finish', data);
const ids = [];
const users = [];
for (const key in mapping) {
if (mapping.hasOwnProperty(key)) {
users.push(key);
ids.push(mapping[key]);
}
}
Expand All @@ -118,8 +120,10 @@ rtm.on(RTM_EVENTS.MESSAGE, (msg) => {
// text = text.slice(startIndex + 5, endIndex - 1).trim();
// console.log(mapping);
web.chat.postMessage(msg.channel, data.result.fulfillment.speech, messageConfirmation(data.result.fulfillment.speech, "remember to add code to actaully cancel the meeting/not schedule one"));
user.pending = JSON.stringify(Object.assign({}, data.result.parameters, { type: 'meeting', ids: ids }));
user.pending = JSON.stringify(Object.assign({}, data.result.parameters, { type: 'meeting', ids: ids, users: users }));
console.log("In bot.js", ids);
user.save();
mapping = {};
}
break;
case 'reminder.add':
Expand Down
24 changes: 23 additions & 1 deletion constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import google from 'googleapis';

const OAuth2 = google.auth.OAuth2;
const calendar = google.calendar('v3');

const messageConfirmation = (confirmation, cancellation) => ({
"text": "Is this correct?",
Expand Down Expand Up @@ -47,4 +48,25 @@ const getQueryParams = (msg, sessionId) => ({
sessionId: sessionId
});

export { messageConfirmation, getQueryParams, getGoogleAuth };
const getFreeBusy = (auth, timeMin, timeMax, id) => (
new Promise( (resolve, reject) => {
calendar.freebusy.query({
auth: auth,
resource: {
timeMin: timeMin,
timeMax: timeMax,
items: [{
id: id
}]
}
}, (err, result) => {
if(err) {
reject(err);
} else {
resolve(result);
}
});
})
);

export { messageConfirmation, getQueryParams, getGoogleAuth, getFreeBusy };
63 changes: 37 additions & 26 deletions routes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import express from 'express';
import axios from 'axios';

const router = express.Router();

import { rtm } from './bot';
Expand All @@ -8,7 +10,7 @@ import google from 'googleapis';
const OAuth2 = google.auth.OAuth2;

import { User, Reminder } from './models';
import { getGoogleAuth } from './constants';
import { getGoogleAuth, getFreeBusy } from './constants';

const scopes = [
'https://www.googleapis.com/auth/calendar',
Expand All @@ -17,35 +19,17 @@ const scopes = [
];

const calendar = google.calendar('v3');

/* test44607 */
router.post('/slack/interactive', (req, res) => {
User.findOne({ slackId: JSON.parse(req.body.payload).user.id })
.then((user) => {
if (!user) {
console.log("User not found");
} else {
console.log(user);
// console.log(user);
const googleAuth = getGoogleAuth();
const pending = JSON.parse(user.pending);
googleAuth.setCredentials(user.google);
/* not sure if this commented block should be removed -- due to merge
const event = {
'summary': pending.subject,
'start': {
'date': pending.date,
'timeZone': 'America/Los_Angeles',
},
'end': {
'date': pending.date,
'timeZone': 'America/Los_Angeles',
}
};
console.log(event);
var newReminder = new Reminder({
subject: pending.subject,
date: pending.date,
userId: user.slackDmId,
}); */
const currentDate = new Date();
if (currentDate > user.google.expiry_date) {
googleAuth.refreshAccessToken((err, tokens) => {
Expand Down Expand Up @@ -89,8 +73,6 @@ router.post('/slack/interactive', (req, res) => {
});
});
} else if (pending.type === "meeting") {
console.log('START HERE');
console.log(pending.ids);
const attendees = [];
for(let i = 0; i < pending.ids.length; i++) {
const object = {};
Expand All @@ -99,7 +81,6 @@ router.post('/slack/interactive', (req, res) => {
object.email = rtm.dataStore.getUserById(id).profile.email;
attendees.push(object);
}
console.log(attendees);
const event2 = {
'summary': 'Meeting',
'description': pending.type,
Expand All @@ -113,7 +94,37 @@ router.post('/slack/interactive', (req, res) => {
},
'attendees': attendees,
};
console.log("Here");
const busy = [];
console.log("Before the loop", pending.ids);
for(let i = 0; i < pending.ids.length; i++) {
console.log("In for-loop");
const id = pending.ids[i];
User.findOne({"slackId": id})
.then((pendingUser, err) => {
if(err) {
console.log("User not found", err);
return;
}
const userEmail = rtm.dataStore.getUserById(id).profile.email;
// console.log(userEmail);
const userAuth = getGoogleAuth();
userAuth.setCredentials(pendingUser.google);
getFreeBusy(userAuth, "2017-07-10T23:44:28.917Z", "2017-07-25T23:44:28.917Z", userEmail)
.then((response) => {
console.log("Busy times are", userEmail, response.calendars[userEmail].busy);
busy.push(response.calendars[userEmail].busy);
console.log("THIS IS BUSY", busy);
process.busyOutput = busy;
console.log("Out of for-loop");
})
.catch((err2) => {
// console.log("Error", err2);
console.log("Error is", err2);
console.log("Out of for-loop");
});
});
}

calendar.events.insert({
auth: googleAuth,
calendarId: 'primary',
Expand Down Expand Up @@ -205,4 +216,4 @@ router.get('/connect/callback', (req, res) => {
});
});

export default router;
export default router;