forked from sunnypatel165/flockSO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index1.js
217 lines (158 loc) · 8.74 KB
/
index1.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
209
210
211
212
213
214
215
216
217
var config = require('./config.js');
var flock = require('flockos');
var express = require('express');
var store = require('./store.js');
var chrono = require('chrono-node');
var Mustache = require('mustache');
var fs = require('fs');
var util = require('util');
var request = require('request');
var Client = require('node-rest-client').Client;
var client = new Client();
var watcher = require('./watcher.js');
flock.appId = config.appId;
flock.appSecret = config.appSecret;
var url = 'https://api.stackexchange.com/2.2/';
var searchUrlAppend = 'search?order=desc&sort=votes&site=stackoverflow&intitle=';
var questionAnswerUrlAppend = '?site=stackoverflow&filter=withbody';
var baseUrl = 'https://f1dbdfaf.ngrok.io/';
var app = express();
app.use(flock.events.tokenVerifier);
app.post('/events', flock.events.listener);
app.set('view engine', 'ejs');
app.listen(8080, function() {
console.log('Listening on 8080');
});
setTimeout(function(){
watcher.startWatcher(function(userId, questionId) {
flock.chat.sendMessage(config.botToken, {
to: userId,
text: "Question Id " + questionId + " has changed!"
});
});
}, 0);
app.get('/addToWatchList/:userId/:questionId',function(req,res){
var userId = req.params.userId;
var questionId = req.params.questionId;
console.log( "userId = " + userId + " questionId " + questionId );
watcher.addWatcher(userId, questionId);
var date = new Date().toISOString().slice(0, 19).replace('T', ' ');
res.send("Hi, we will be watching the question and notify you if any answers are added");
//add_to_watch_list(userId, questionId);
});
app.get('/upvoteAnswer/:userId/:answerId', function(req, res){
var userId = req.params.userId;
var questionId = req.params.answerId;
var date = new Date().toISOString().slice(0, 19).replace('T', ' ');
res.send('Thank you for upvoting! :)');
});
app.get('/list',function(req,res){
var event = JSON.parse(req.query.flockEvent);
res.set('Content-Type', 'text/html');
var userId = event.userId;
console.log( "userId = " + userId);
var watchList = watcher.getWatchList(userId);
var questionTitles = getQuestionTitles(watchList[userId], res);
});
function getQuestionTitles(watchList, res) {
questionTitles = [];
var questionUrl = url + '/questions/';
console.log(watchList);
for(var i = 0; i < watchList.length-1; i++) {
console.log(i);
var questionId = watchList[i];
questionUrl = questionUrl + questionId + ';' ;
}
questionUrl = questionUrl + watchList[watchList.length - 1]+questionAnswerUrlAppend;
console.log(questionUrl);
client.get(questionUrl, function (data, response) {
console.log("Got question title for watch list!");
console.log(data);
for(i in data.items){
console.log("Sunnsunn = " + i ) ;
questionTitles.push(data.items[i].title);
}
console.log("questionTitles " + questionTitles);
var body = '<html><head></head><body><style>.list-type1{width:400px;margin:0 auto}.list-type1 ol{counter-reset:li;list-style:none;font-size:15px;font-family:Raleway,sans-serif;padding:0;margin-bottom:4em}.list-type1 ol ol{margin:0 0 0 2em}.list-type1 a{position:relative;display:block;padding:.4em .4em .4em 2em;margin:.5em 0;background:#93C775;color:#000;text-decoration:none;-moz-border-radius:.3em;-webkit-border-radius:.3em;border-radius:10em;transition:all .2s ease-in-out}.list-type1 a:hover{background:#d6d4d4;text-decoration:none;transform:scale(1.1)}.list-type1 a:before{content:counter(li);counter-increment:li;position:absolute;left:-1.3em;top:50%;margin-top:-1.3em;background:#93C775;height:2em;width:2em;line-height:2em;border:.3em solid #fff;text-align:center;font-weight:700;-moz-border-radius:2em;-webkit-border-radius:2em;border-radius:2em;color:#FFF} </style><div class="list-type1"><ol>'
var end = '</ol></div></body></html>'
for (i in questionTitles) {
body += '<li><a href="https://www.stackoverflow.com/q/' + watchList[i] + '" target="_blank">'+ questionTitles[i] + "</a></li>";
}
body += end ;
console.log(body);
res.send(body);
});
return questionTitles;
}
function sendMessageByBot(event, questions) {
var css = ".btn { line-height: 30px;display: inline-block;float:left;width: 200px;text-align: center; cursor:pointer; background: #0abe50; background-image: -webkit-linear-gradient(top, #0abe50, #0abe50); background-image: -moz-linear-gradient(top, #0abe50, #0abe50); background-image: -ms-linear-gradient(top, #0abe50, #0abe50); background-image: -o-linear-gradient(top, #0abe50, #0abe50); background-image: linear-gradient(to bottom, #0abe50, #0abe50); -webkit-border-radius: 0; -moz-border-radius: 0; border-radius: 0px; font-family: Arial; color: #f2f2f2; font-size: 14px; padding: 5px 5px 5px 5px; text-decoration: none; }.btn:hover { background: #5ce08f; background-image: -webkit-linear-gradient(top, #5ce08f, #5ce08f); background-image: -moz-linear-gradient(top, #5ce08f, #5ce08f); background-image: -ms-linear-gradient(top, #5ce08f, #5ce08f); background-image: -o-linear-gradient(top, #5ce08f, #5ce08f); background-image: linear-gradient(to bottom, #5ce08f, #5ce08f); text-decoration: none; }" ;
var start = '<!DOCTYPE html><html><head><title>Questions</title></head><style>'+css+'</style><body>' ;
var end = '</body></html>';
console.log("Firing BOT! :D");
questions.forEach(function(e){
var linkToWatch = baseUrl + 'addToWatchList/' + event.userId + '/'+ e.questionId ;
e.answers.forEach(function(d){
var linkToUpvote = baseUrl + 'upvoteAnswer/' + event.userId + '/' + d.answerId ;
var start = '<!DOCTYPE html><html><head><title>Questions</title></head><style>' + css + '</style><body>';
start += '<li style="display:inline-block;">' + e.question + '<br><a href="' + linkToWatch + '" class="btn">Watch</a></li><ul>' ;
start += '<div><code>' + d.answer + '</code></div>' ;
start += '</ul><br><a href="' + linkToUpvote +'" class="btn">Upvote this answer</a>' + end;
body = start;
console.log(body);
flock.chat.sendMessage(config.botToken, {
to: event.userId,
attachments: [{
"title": "We found something relevant on stackoverflow",
"views": {
"html": { "inline": body, "width" : 400, "height" : 400}
}
}]
});
});
});
}
flock.events.on('app.install', function (event, callback) {
flock.chat.sendMessage(config.botToken, {
to: event.userId,
text: "Welcome to the FlockSo app! FlockSo is StackOverflow for FlockOs"
});
store.saveToken(event.userId, event.token);
callback();
});
flock.events.on('client.slashCommand', function (event, callback) {
var commandText = event.text;
console.log("Whole command is " + commandText);
var searchUrl = url + searchUrlAppend + encodeURIComponent(commandText.trim());
console.log(searchUrl);
var questions = [];
var question = {};
client.registerMethod("jsonMethod", searchUrl, "GET");
client.methods.jsonMethod(function (data, response) {
if (data.items == null) {
console.log("Unable to find any matching questions");
return -1;
}
var questionId = data.items[0].question_id;
console.log("Found questionId: " + questionId);
var questionUrl = url + '/questions/' + questionId + questionAnswerUrlAppend;
client.registerMethod("jsonMethod", questionUrl, "GET");
client.methods.jsonMethod(function (data, response) {
var questionTitle = data.items[0].title;
question['question'] = questionTitle;
question['questionId'] = questionId;
console.log("Question Title - " + questionTitle);
var answerUrl = url + '/questions/' + questionId + '/answers' + questionAnswerUrlAppend;
client.registerMethod("jsonMethod", answerUrl , "GET");
client.methods.jsonMethod(function (data, response) {
var answers = data.items.slice(1, 3).map(function(item) {return {'answer': item.body, 'answerId': item.answer_id}});
console.log("Found answers: ")
answers.forEach(console.log);
question['answers'] = answers;
console.log("===========================");
questions.push(question);
console.log(JSON.stringify(questions));
sendMessageByBot(event, questions);
});
});
});
});