This repository has been archived by the owner on Mar 29, 2024. It is now read-only.
forked from tjk911/lohud-trellobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailer.js
208 lines (178 loc) · 6.2 KB
/
mailer.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
var outlook = require('node-outlook')
, authHelper = require('./authHelper')
, credentials = require('./credentials')
, outlook_refresh = require('outlook-refresh')
, moment = require('moment-timezone')
, fs = require("fs")
, savedRefresh = require('./token.json')
, bot = require('./bot')
var savedToken, refreshToken, text, channel, username, emoji;
var alertCounter = 0;
// Use below for stage/prod
var momenttime = moment().tz("America/Los_Angeles").format();
// Use below for dev
// var momenttime = moment().tz("America/New_York").format();
// console.log('this is momenttime', momenttime);
var savedTime = momenttime;
// check what was saved
// console.log('this is the saved refresh token: ', savedRefresh);
// console.log('this is the local email: ', localEmail);
// var savedEmail = localEmail;
var refresh = function (){
outlook_refresh(refreshToken, credentials.oauthcredID, credentials.oauthcredSecret, function (err, res){
if (err){
console.log('refresh error: ', err);
outlook_refresh(savedRefresh, credentials.oauthcredID, credentials.oauthcredSecret, function (err, res){
if (err){
console.log('still not refreshing: ', err);
} else {
console.log('Calling for mailerRefresh1');
mailerRefresh(res);
}
})
} else {
console.log('Calling for mailerRefresh2');
mailerRefresh(res);
}
});
};
var mailerRefresh = function (res){
savedToken = res.token;
console.log('refresh triggered');
refreshToken = res.refreshToken;
console.log('this is the new refresh token');
var newJSON = JSON.stringify(refreshToken);
fs.writeFile('./token.json', newJSON, function (err){
if(err){
return handleError(err);
} else {
// console.log('your json has been saved');
console.log(newJSON);
}
});
setTimeout(refresh, 1800000); // 1000 = 1 sec, currently 30 mins
}
var tokenReceived = function (res, error, token){
if (error){
console.log("Access token error: ", error.message);
}
else {
savedToken = token.token.access_token;
// savedEmail = authHelper.getEmailFromIdToken(token.token.id_token);
refreshToken = token.token.refresh_token;
// console.log(token);
// console.log('This is the token: '+savedToken);
// console.log('This is the email: '+credentials.inbox);
// console.log('This is the refresh token: '+refreshToken);
// console.log("We've auth'ed!");
var newJSON = JSON.stringify(refreshToken);
fs.writeFile('./token.json', newJSON, function (err){
if(err){
return handleError(err);
} else {
// console.log('your json has been saved');
// console.log(newJSON);
}
});
// Use below for stage/prod
// res.redirect(302, 'https://data.lohud.com/bots/trellobot/');
res.redirect(302, 'https://trellobot.lohudblogs.com/');
// Use below for dev
// res.redirect(302, 'http://localhost:8080/');
// res.end();
}
};
var getValueFromCookie = function (valueName, cookie){
if (cookie.indexOf(valueName) !== -1){
var start = cookie.indexOf(valueName) + valueName.length + 1;
var end = cookie.indexOf(';', start);
end = end === -1 ? cookie.length : end;
return cookie.substring(start, end);
}
};
var checkMail = function (req, res){
// Use below for stage/prod
var date = moment().tz("America/Los_Angeles").format();
channel = '#audience';
username = 'OutlookBot';
emoji = ':calculon:';
// Use below for dev
// var date = moment().tz("America/New_York").format();
// console.log('Checking mail now');
// console.log('Initial savedTime check: '+savedTime);
// console.log('Initial date check: '+date);
// console.log('Now we check for savedToken');
// console.log(savedToken);
// console.log('Now we check for savedEmail');
// console.log(savedEmail);
// t = new Date();
if (savedToken == undefined){
console.log(date, 'mailer.js broke!');
alertCounter ++;
if (alertCounter == 60){
alertCounter = 0;
text = 'Our outlook authentication is dead! Please re-login at `http://trellobot.lohudblogs.com` with our [email protected] account!';
bot.sendMessage(text, channel, username, emoji);
};
rePingMail();
} else {
var queryParams = {
'$filter':"ReceivedDateTime ge "+savedTime+" and From/EmailAddress/Address eq '[email protected]'",
// '$filter':"ReceivedDateTime ge "+savedTime,
'$select': 'Subject,ReceivedDateTime,From',
// '$orderby': 'ReceivedDateTime desc',
'$top': 10,
// '$search': '"from:[email protected]"'
};
// console.log('this is the queryParams');
// console.log(queryParams);
// Set the API endpoint to use the v2.0 endpoint
outlook.base.setApiEndpoint('https://outlook.office.com/api/v2.0');
// Set the anchor mailbox to the user's SMTP address
// Throwing errors for some reason
outlook.base.setAnchorMailbox(credentials.inbox);
outlook.mail.getMessages({token: savedToken, odataParams: queryParams},
function (error, result){
// console.log(result);
if (error){
console.log('getMessages returned an error: ' + error);
rePingMail(result);
} else if (result){
rePingMail(result);
}
}
);
}
// setTimeout(checkMail, 100000); // 10 secs
// setTimeout(checkMail, 10000);
// console.log(date, 'mail pinged');
// console.log(date);
// console.log(savedTime);
savedTime = date;
};
var rePingMail = function (result){
// console.log(result);
channel = '#audience';
username = 'Associated Press';
emoji = ':Deathstar:';
if (result == undefined){
console.log('this is rePing saying mailerjs is broken');
} else {
var inbox = result['value'];
// console.log(inbox);
for (var x = 0; x < inbox.length; x++){
text = "`AP NOTIFICATION:` *"+inbox[x]['Subject']+"*";
bot.sendMessage(text, channel, username, emoji);
}
}
var date = moment().tz("America/Los_Angeles").format();
// console.log(date, 'mail pinged');
setTimeout(checkMail, 30000);
};
module.exports = {
tokenReceived: tokenReceived,
getValueFromCookie: getValueFromCookie,
checkMail: checkMail,
refresh: refresh,
rePingMail: rePingMail
}