-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
64 lines (56 loc) · 1.58 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
var express = require('express');
var request = require('request');
var app = express();
var port = process.env.PORT || 3001;
var slack_token = process.env.SLACK_TOKEN;
var channel = process.env.CHANNEL;
var image = process.env.IMAGE_NAME;
var BASE_URL = 'https://slack.com/api/chat.postMessage?token=' + slack_token + '&';
var imageMe = function(query, animated, faces, cb) {
if (typeof animated === 'function') {
cb = animated;
}
if (typeof faces === 'function') {
cb = faces;
}
var url = 'http://ajax.googleapis.com/ajax/services/search/images';
var qs = {
v: '1.0',
rsz: '8',
q: query,
safe: 'active'
};
return request({url: url, qs: qs}, function(err, res, body) {
var image, images, _ref;
images = JSON.parse(body);
images = (_ref = images.responseData) != null ? _ref.results : void 0;
if ((images != null ? images.length : void 0) > 0) {
image = random(images);
return cb(ensureImageExtension(image.unescapedUrl));
}
});
};
var ensureImageExtension = function(url) {
var ext;
ext = url.split('.').pop();
if (/(png|jpe?g|gif)/i.test(ext)) {
return url;
} else {
return url + "#.png";
}
};
var inquiry = function(channel) {
return imageMe(image, function(url) {
var slack_url = BASE_URL + 'channel=' + channel + '&text=' + url + '&link_names=1';
return request(slack_url, function(err, res, body) {
});
});
};
var random = function(items) {
return items[Math.floor(Math.random() * items.length)];
}
app.get('/image', function(req, res){
inquiry(channel);
res.sendStatus(200);
})
app.listen(port);