-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_wp.js
60 lines (50 loc) · 1.5 KB
/
index_wp.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
var express = require('express');
var app = express();
var url = require('url');
var request = require('request');
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('port', (process.env.PORT || 9001));
app.get('/', function(req, res){
res.send('This works fine too!!');
});
app.post('/post', function(req, res){
var parsed_url = url.format({
pathname: 'https://en.wikipedia.org/w/api.php',
query: {
action: 'query',
prop: 'extracts',
exintro: '',
explaintext: '',
prop: 'extracts',
format: 'json', //json format
titles: req.body.text // search query
}
});
request(parsed_url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var data = JSON.parse(body);
var obj = data.query.pages;
var first_page = obj[Object.keys(obj)[0]];
var first_snippet = first_page.extract.substring(0,250)+'...';
var result_url = 'http://en.wikipedia.org/wiki/' + first_page.title;
var return_result = result_url + " " + first_snippet;
var body = {
response_type: "in_channel",
text: "Wikipedia says...",
attachments: [
{
title: first_page.title,
title_link: result_url,
text: first_snippet
}
]
};
res.send(body);
}
});
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});