forked from sunnypatel165/flockSO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
177 lines (147 loc) · 5.7 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
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');
f
flock.appId = config.appId;
flock.appSecret = config.appSecret;
var app = express();
var url = 'https://api.stackexchange.com/2.2/';//=java%20how%20to%20read%20file&site=stackoverflow' ;
var searchUrlAppend = 'search?order=desc&sort=votes&site=stackoverflow&intitle=';
var questionAnswerUrlAppend = '?site=stackoverflow&filter=withbody';
app.use(flock.events.tokenVerifier);
app.post('/events', flock.events.listener);
app.listen(8080, function () {
console.log('Listening on 8080');
});
/*app.get("/abc",function (req, res){
var url = 'https://api.stackexchange.com/2.2/search?order=desc&sort=votes&intitle=java%20how%20to%20read%20file&site=stackoverflow' ;
console.log("hi sfsdcv");
console.log(res);
request.get({url:url}, function (error, response, body) {
if (!error && response.statusCode == 200) {
res.send(body) ;// Print the google web page.
}
});
});
*/
flock.events.on('app.install', function (event, callback) {
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);
client.registerMethod("jsonMethod", searchUrl, "GET");
client.methods.jsonMethod(function (data, response) {
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;
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 item.body})
console.log("Found answers: ")
answers.forEach(console.log);
});
});
});
flock.chat.sendMessage(config.botToken, {
to: event.userId,
html: '<flockml>Hello <strong>foobar</strong>, Welcome to <a style="background-color: #4CAF50; border: none;color: white;padding: 15px 32px;text-align: center;text-decoration: none;display: inline-block;font-size: 16px;" href="https://flock.co/">Flock!</a></flockml>' ,
});
//<flockml>Hello <strong>foobar</strong>, Welcome to <a href="https://flock.co/">Flock!</a></flockml>
/*var r = parseDate(event.text);
console.log('parse result', r);
var desc = event.text.slice(r.end).trim() ;
if (r) {
var alarm = {
userId: event.userId,
time: r.date.getTime(),
text: event.text.slice(r.end).trim()
};
console.log('adding alarm', alarm);
addAlarm(alarm);
callback(null, { text: 'Alarm added' });
} else {
callback(null, { text: 'Alarm time not specified' });
}*/
});
var parseDate = function (text) {
var r = chrono.parse(text);
if (r && r.length > 0) {
return {
date: r[0].start.date(),
start: r[0].index,
end: r[0].index + r[0].text.length
};
} else {
return null;
}
};
var listTemplate = fs.readFileSync('list.mustache.html', 'utf8');
app.get('/list', function (req, res) {
var event = JSON.parse(req.query.flockEvent);
var alarms = store.userAlarms(event.userId).map(function (alarm) {
return {
text: alarm.text,
timeString: new Date(alarm.time).toLocaleString()
}
});
res.set('Content-Type', 'text/html');
var body = Mustache.render(listTemplate, { alarms: alarms });
res.send(body);
});
flock.events.on('client.messageAction', function (event, callback) {
var messages = event.messages;
if (!(messages && messages.length > 0)) {
console.log('chat', event.chat);
console.log('uids', event.messageUids);
console.log('token', store.getToken(event.userId));
flock.chat.fetchMessages(store.getToken(event.userId), {
chat: event.chat,
uids: event.messageUids
}, function (error, messages) {
if (error) {
console.warn('Got error');
callback(error);
} else {
setAlarms(messages);
}
});
} else {
setAlarms(messages);
}
var setAlarms = function (messages) {
var alarms = messages.map(function (message) {
var parsed = parseDate(message.text);
if (parsed) {
return {
userId: event.userId,
time: parsed.date.getTime(),
text: util.format('In %s: %s', event.chatName, message.text)
}
} else {
return null;
}
}).filter(function (alarm) {
return alarm !== null;
});
if (alarms.length > 0) {
alarms.forEach(addAlarm);
callback(null, { text: util.format('%d alarm(s) added', alarms.length) });
} else {
callback(null, { text: 'No alarms found' });
}
};
});