forked from flaredragon/hackvsit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (83 loc) · 3.42 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
var flock = require('flockos');
var util = require('util');
var express = require('express');
var Mustache = require('mustache');
var config = require('./config');
var store = require('./store');
// read the app id and secret from the config file (config.js in the
// same directory), and set them for the SDK. This required for event
// token verification to work
flock.setAppId(config.appId);
flock.setAppSecret(config.appSecret);
var app = express();
// since we use express, we can simply use the token verifier
// middleware to ensure that all event tokens are valid
app.use(flock.events.tokenVerifier);
app.get('/',function(req, res){
res.send('works!');
});
// listen for events on /events
app.post('/events', flock.events.listener);
// listen for app.install event, mapping of user id to tokens is saved
// in the in-memory database
flock.events.on('app.install', function (event) {
store.saveUserToken(event.userId, event.token);
});
// listen for client.slashCommand, this gives us the scrap entered by
// the user in the "text" property of the event. This text is saved in
// the in-memory database, following which a message is sent to the
// conversation.
//
// We make use of FlockML to send a richer message then what was
// possible using plain text. This FlockML makes use of the <action>
// tag to open the list of scraps in a sidebar widget. See
// message.mustache.flockml.
var messageTemplate = require('fs').readFileSync('message.mustache.flockml', 'utf8');
flock.events.on('client.slashCommand', function (event) {
store.saveScrap(event.userId, event.chat, event.text);
var flockml = Mustache.render(messageTemplate, { event: event, widgetURL: config.endpoint + '/scraps' });
console.log(flockml);
flock.callMethod('chat.sendMessage', store.getUserToken(event.userId), {
to: event.chat,
text: util.format('%s saved a scrap: %s', event.userName, event.text),
flockml: flockml
}, function (error, response) {
if (!error) {
console.log('uid for message: ' + response.uid);
} else {
console.log('error sending message: ' + error);
}
});
});
// The widget path is /scraps. The userId and chat properties of the
// event are sufficient for us to retrieve the list of scraps for this
// conversation.
var widgetTemplate = require('fs').readFileSync('index.mustache.html', 'utf8');
var urlRegex = new RegExp('(http|ftp|https)://([\\w_-]+(?:(?:\\.[\\w_-]+)+))([\\w.,@?^=%&:/~+#-]*[\\w@?^=%&/~+#-])?');
app.get('/scraps', function (req, res) {
console.log('request query: ', req.query);
var userId = res.locals.eventTokenPayload.userId;
console.log('user id: ', userId);
var event = JSON.parse(req.query.flockEvent);
if (event.userId !== userId) {
console.log('userId in event doesn\'t match the one in event token');
res.sendStatus(403);
return;
}
console.log('event: ', event);
res.set('Content-Type', 'text/html');
var list = store.listScraps(userId, event.chat);
console.log('list: ', list);
if (list) {
list = list.map(function (text) {
return text.replace(urlRegex, '<a href="$&">$&</a>');
});
}
var body = Mustache.render(widgetTemplate, { list: list, event: event });
res.send(body);
});
// Start the listener after reading the port from config
var port = config.port || 8080;
app.listen(port, function () {
console.log('Listening on port: ' + port);
});