-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
177 lines (165 loc) · 6.07 KB
/
index.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
'use strict';
const bodyParser = require('body-parser');
const request = require('request');
const BootBot = require('bootbot');
const express = require('express');
const app = express();
const MONGO_URL = process.env.MONGO_URL;
const FB_ACCESS_TOKEN = process.env.FB_ACCESS_TOKEN;
const VERIFY_TOKEN = process.env.VERIFY_TOKEN;
const APP_SECRET = process.env.APP_SECRET;
const STACK_KEY = process.env.STACK_KEY;
const STACK_CLIENT_SECRET = process.env.STACK_CLIENT_SECRET;
var MongoClient = require('mongodb').MongoClient;
var mydb, stackusers, userIds = [];
MongoClient.connect(MONGO_URL, function (err, db) {
mydb = db;
if (err) {
console.log(err);
} else {
setInterval(getUserInfo, 300000)
console.log("==== database connected whoohooo ====");
}
})
const bot = new BootBot({
accessToken: FB_ACCESS_TOKEN,
verifyToken: VERIFY_TOKEN,
appSecret: APP_SECRET
});
function addUserToDB(accessToken) {
mydb.collection("stackusers").findOneAndUpdate({
'userId': userIds.pop()
}, {
$set: {
'accessToken': accessToken
}
}, {
upsert: true
}, function (err, post) {
if (err) {
console.log(err);
} else {
console.log(post);
}
})
}
function getUnreadInbox(user) {
request.get({
uri: 'https://api.stackexchange.com/2.2/me/inbox/unread?site=stackoverflow&key=' + STACK_KEY + '&page=1&pagesize=12&access_token=' + user.accessToken,
gzip: true
}, function (error, response, body) {
if (error) {
console.log(error);
return;
} else {
let jsonbody = JSON.parse(body);
if (jsonbody["items"] && jsonbody["items"].length > 0) {
jsonbody["items"].forEach(function (item) {
if (item["creation_date"] * 1000 > (new Date().getTime() - 300000)) {
bot.say(user.userId, {
text: 'You got a reply in ' + item["item_type"] + '\n\n"' + item["title"] + '"',
buttons: [{
type: 'web_url',
title: 'Visit post',
url: item["link"]
}]
});
}
})
}
}
})
}
function getUnreadReputationChanges(user) {
request.get({
uri: 'https://api.stackexchange.com/2.2/me/reputation?site=stackoverflow&key=' + STACK_KEY + '&access_token=' + user.accessToken,
gzip: true
}, function (error, response, body) {
if (error) {
console.log(error)
return;
} else {
let jsonbody = JSON.parse(body);
if (jsonbody["items"] && jsonbody["items"].length > 0) {
jsonbody["items"].forEach(function (item) {
if (item["on_date"] * 1000 > (new Date().getTime() - 300000)) {
bot.say(user.userId, {
text: "+" + item["reputation_change"] + ", " + item["vote_type"].replace(/_/g, " "),
buttons: [{
type: 'web_url',
title: 'Visit post',
url: 'https://stackoverflow.com/q/' + item["post_id"]
}]
});
}
})
}
}
})
}
function getUserInfo() {
mydb.collection("stackusers").find().toArray(function (err, items) {
items.forEach(function (user) {
getUnreadInbox(user)
getUnreadReputationChanges(user)
})
})
}
function getAccessToken(code) {
request.post({
headers: {
'content-type': 'application/x-www-form-urlencoded'
},
url: 'https://stackexchange.com/oauth/access_token',
form: {
code: code,
client_id: 9261,
client_secret: STACK_CLIENT_SECRET,
redirect_uri: 'https://stack-unread-notifier.herokuapp.com/register'
}
}, function (error, response, body) {
console.log(body.substr(body.indexOf("=") + 1));
const accessToken = body.substr(body.indexOf("=") + 1);
addUserToDB(accessToken);
});
}
bot.app.get('/register', function (req, res) {
res.sendFile('register.html', {
root: __dirname
});
});
bot.app.get('/registered-code', function (req, res) {
console.log(req.query);
if (req.query.code) {
res.send("OK");
getAccessToken(req.query.code)
}
})
bot.app.get('/privacy', function (req, res) {
res.send('<h2 style="padding: 30px; font-family: consolas; text-decoration:underline">The StackBot</h2><p style="font-family: consolas; font-size: 18px; padding: 10px 30px">This is built solely for learning purposes and is not intended for any kind of commercial activity and hence we do not collect any kind of user\'s personal information at all. We honor user\'s privacy and do not track anything at all. It is not developed in order to attract anyone under 13.</p>');
});
bot.hear([/.*/], (payload, chat) => {
mydb.collection("stackusers").findOne({
'userId': payload.sender.id
}, function (err, post) {
if (err) {
console.log(err);
} else {
console.log(post)
if (post) {
chat.say("Doesn't look like anything to me!");
} else {
userIds.push(payload.sender.id);
chat.say({
text: "Hey... you're new.. not much of a rind on you! :D \n\nI'm the StackBot! \nI send your StackOverflow notifications/inbox here! :D",
buttons: [{
type: 'web_url',
title: 'Let\'s get started!',
url: 'https://stackexchange.com/oauth?client_id=9261&scope=read_inbox,no_expiry&redirect_uri=https://stack-unread-notifier.herokuapp.com/register'
}]
});
}
}
})
});
bot.start(process.env.PORT || 3000);