-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
118 lines (93 loc) · 3.02 KB
/
app.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
var express = require('express');
var bodyParser = require('body-parser');
var curator = require('./curator');
var request = require('request');
var _ = require('underscore');
var app = express();
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.listen(process.env.PORT || 3000,function() {
console.log('curate listening on port 3000');
});
app.get('/', function(req,res) {
res.send("Heroku Web Dyno is up and running");
})
app.post('/', function(req,res) {
// ensure that this command came from slack, and if not don't do anything
if(process.env.SLACK_TOKEN !== req.body.token) {
res.send("Slack token could not be verified");
return;
};
console.log("token verified");
// check if exceeds max 20 twitter handles
names = req.body.text.split(" ");
var names = _.without(names, ''); // removing any string splits on extra whitespace
if(names.length > 20) {
res.send({
"response_type": "in_channel",
"text": "Try to keep it bite sized! Also slack allows 20 attachments, so keep it at most 20!"
});
return;
}
// check if user is asking for help
if(names[0].toLowerCase() === 'help') {
helpMessage = {
"text": "Get yesterday's highlights in bite-size. Example command: /curate @cnn @elonmusk @espn @spacex\n This will return the most popular tweet (using number favorites and retweets) from yesterday per handle.\nPlease limit to a maximum of 20 twitter handles "
}
res.send(helpMessage);
return;
}
console.log("about to send confirmation of receive");
var response_url = req.body.response_url;
// send confirmation bc a delayed post will be needed
res.writeHead(200);
res.end("Got it! I\'ll be right back :bowtie:");
// colors
var color = true;
var blue = "#1da1f2";
var purple = "#443642";
// prepare delayed post request
curator(names, function(result) {
// preparing message to post
var message = {
"response_type": "in_channel",
"text": "*Yesterday's Highlights*",
"attachments": []
}
// add each handle as an attachment
result.handles.forEach(function(handle) {
var attachment = {};
// record twitter handle
if(handle.key.charAt(0) === '@')
attachment.title = handle.key;
else
attachment.title = '@' + handle.key;
// create linkable title
attachment.title_link = 'https://twitter.com/statuses/' + handle.link;
// record tweet
attachment.text = handle.result;
// twitter blue or slack purple
attachment.color = color === true ? blue : purple;
color = !color;
// attachment.mrkdwn_in = ["title"];
// add timestamp, but fix formatting
// attachment.ts = handle.timestamp;
// add attachment to message array
message.attachments.push(attachment);
});
// prepare post request fields
var options = {
url: response_url,
method: 'POST',
contentType: 'application/json',
json: message
}
request(options, function(error, response, body){
if(error) {
console.log(error);
} else {
console.log(response.statusCode, body);
}
});
});
});