forked from HorizonsBot/NewBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
196 lines (165 loc) · 5.41 KB
/
app.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
'use strict';
var mongoose = require('mongoose');
mongoose.connect(process.env.MONGODB_URI);
var {web, rtm} = require('./bot');
var {User, Reminder, Meeting} = require('./models');
//extras
var axios = require('axios');
var moment = require('moment');
var _ = require('underscore');
moment().format();
//express
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
//google
var google = require('googleapis');
var OAuth2 = google.auth.OAuth2;
function getGoogleAuth() {
return new OAuth2(
process.env.GOOGLE_CLIENT_ID,
process.env.GOOGLE_CLIENT_SECRET,
process.env.DOMAIN + '/connect/callback'
);
}
// functions
var {clearState, taskPath, meetingPath} = require('./functions.js');
app.get('/connect', function(req, res){
var userId = req.query.auth_id;
if (!userId) {
res.status(400).send("Missing user id");
} else {
User.findById(userId)
.then(function(user){
if (!user) {
res.status(404).send("Cannot find user");
}
else {
var googleAuth = getGoogleAuth();
var url = googleAuth.generateAuthUrl({
access_type: 'offline', //'online' (default) or 'offline' (gets refresh_token)
prompt: 'consent',
scope: [
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/calendar',
'email'
], // generate a url that asks permissions for Google+ and Google Calendar scopes
state: userId
});
res.redirect(url);
}
})
}
})
var bluebird = require('bluebird');
var moment = require('moment');
var calendar = google.calendar('v3');
var eventInsert = bluebird.promisify(calendar.events.insert.bind(calendar.events));
var plus = google.plus('v1');
var peopleGet = bluebird.promisify(plus.people.get.bind(plus.people));
app.get('/connect/callback', function(req, res){
var googleAuth = getGoogleAuth();
var getToken = bluebird.promisify(googleAuth.getToken.bind(googleAuth));
var tokens;
var googleUser;
getToken(req.query.code)
.then(function(t) {
console.log("HERE ARE THE TOKENS", t); // Now tokens contains an access_token and an optional refresh_token. Save them.
tokens = t;
googleAuth.setCredentials(tokens);
return peopleGet({auth: googleAuth, userId: 'me'});
})
.then(function(g) {
googleUser = g;
return User.findById(req.query.state);
})
.then(function(mongoUser) {
mongoUser.googleAccount = tokens;
mongoUser.googleAccount.email = googleUser.emails[0].value;
mongoUser.googleAccount.profile_ID = googleUser.id;
mongoUser.googleAccount.profile_name = googleUser.displayName;
return mongoUser.save();
})
.then(function(mongoUser){
res.send('You are connected to Google Calendar'); //To /connect/callback webpage
rtm.sendMessage('You are connected to Google Calendar. Now set your first reminder by talking to me!', mongoUser.slack_DM_ID) //To slack channel
})
.catch(function(err){
res.status(500).json({error: err});
})
})
app.post('/bot-test', function(req, res){
var payload = JSON.parse(req.body.payload);
// cancelled the reuqest
if(payload.actions[0].value === "cancel"){
User.findOne({slack_ID: payload.user.id})
.then(function(user){
clearState(user)
})
res.send("Your request has been cancelled. " + ':pray: :100: :fire:');
}
// accepted request
else{
var curTime = Date.now();
User.findOne({slack_ID: payload.user.id})
.then(function(user){
// task path
console.log("checking which path to take");
if(user.pendingState.invitees.length === 0){
console.log("task path taken");
taskPath(user)
.then(flag => {
if(flag){
clearState(user);
res.send("Task has been added to your calendar " + ':pray: :100: :fire:');
}
else{
clearState(user);
res.send("Failed to post task to calendar")
}
})
}
// meeting path
else{
console.log("meeting path taken");
if(payload.actions[0].name==='alt_dates'){
var newDate = payload.actions[0].selected_options[0];
newDate = newDate.value.split(' ');
user.pendingState.date = newDate[0];
user.pendingState.time = newDate[1] + ":00";
user.save(function(err,user){
meetingPath(user).then((flag) => {
console.log("FLAG", flag);
if(flag){
clearState(user);
res.send("Meeting has been added to your calendar " + ':pray: :100: :fire:');
}else{
clearState(user);
res.send("Failed to post meeting to calendar")
}
});
});
}
else{
meetingPath(user)
.then(function(flag){
console.log("after meetingPath we have flag = " , flag);
if(flag){
clearState(user);
res.send("Meeting has been added to your calendar " + ':pray: :100: :fire:');
}
else{
clearState(user);
res.send("Failed to post meeting to calendar")
}
})
}
}
})
}
})
app.listen(3000);
rtm.start();