-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
169 lines (127 loc) · 4.04 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
var config = require('config');
var AgentKeepAlive = require('agentkeepalive');
var express = require('express');
var sprintf = require("sprintf-js").sprintf;
var bodyParser = require('body-parser');
var OAuth = require('oauth');
var exphbs = require('express-handlebars');
var fs = require('fs');
var OAuth2 = OAuth.OAuth2;
var oauth2 = new OAuth2(config.slack.client_id,
config.slack.client_secret,
'https://slack.com/',
'oauth/authorize',
'api/oauth.access',
null);
const { Client } = require('@elastic/elasticsearch')
const client = new Client({
node: config.elasticsearch.node,
auth: {
apiKey: config.elasticsearch.api_key_encoded,
},
tls: {
ca: fs.readFileSync('./certs/http_ca.crt'),
rejectUnauthorized: false
}
})
var app = express();
app.engine('hb', exphbs({
defaultLayout: 'main',
extname: 'hb'
}));
app.set('view engine', 'hb');
app.enable('view cache');
app.use(express.static('static'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', function (req, res) {
res.render('home');
});
app.get('/complete', function (req, res) {
res.render('complete', {complete: true});
});
app.get('/oauth', function (req, res) {
oauth2.getOAuthAccessToken(
req.query.code,
{'grant_type':'client_credentials'},
function (e, access_token, refresh_token, results) {
res.redirect('/complete');
}
);
});
app.post('/search', async function (req, res) {
if (!(req.body && typeof req.body.text !== 'undefined')) {
return res.status(500).json({
ok: false,
error: "query_missing"
});
}
var searchQuery = req.body.text;
try {
response = await client.search({
index: config.elasticsearch.index,
query: {
match: {
text: searchQuery
}
},
size: 5
});
var output = {};
if (response && response.hits && response.hits.hits) {
var clips = [];
response.hits.hits.forEach(function(hit) {
var url = config.imageBaseUrl +
'S' + sprintf("%02f", hit._source.season) +
'E' + sprintf("%02f", hit._source.episode) +
'/' + hit._source.frame + '.gif';
var clip = {
id: hit._id,
season: hit._source.season,
episode: hit._source.episode,
url: url,
text: hit._source.text
};
clips.push(clip);
});
if (clips.length === 0) {
return res.json({
"response_type": "ephemeral",
"attachments": [{
"text": 'Could not match ' + searchQuery,
}]
});
}
var clip = clips[Math.floor(Math.random() * (clips.length))];
var text = clip.text.replace("\n",' ');
res.json({
"response_type": "in_channel",
"attachments": [{
"text": 'Season ' + clip.season + ', ' + 'Episode: ' + clip.episode,
"image_url": clip.url
}]
});
}
} catch (error) {
res.json({
"response_type": "ephemeral",
"attachments": [{
"text": 'Oops, there was an error searching for ' + searchQuery,
}]
});
}
});
async function main() {
try {
const result = await client.info();
console.log('Elasticsearch v' + result.version.number + ' is running...');
app.listen(config.port, function () {
console.log('Server started on port ' + config.port + '.');
});
} catch (error) {
console.error('Elasticsearch is not available at ' + config.elasticsearch.host);
process.exit(1);
return;
}
}
main();