-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
84 lines (72 loc) · 2.35 KB
/
Copy pathindex.js
File metadata and controls
84 lines (72 loc) · 2.35 KB
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
/* a slash command bot for Slack */
/* returns a summary from wikipedia for a searched query through slack */
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 || 7001));
app.get('/', function(req, res){
res.send('Wikipedia works fine on port 7001!!');
});
// rest api url format
// https://en.wikipedia.org/w/api.php?action=query&prop=extracts&exintro&explaintext&format=json&titles=tiger
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 page_id = first_page.pageid;
var title = first_page.title;
var return_result = result_url + " " + first_snippet;
var body = {
response_type: "ephemeral", // ephemeral is only visible to the user, other option is in_channel
text: "According to *Wikipedia*...",
attachments: [
{
title: first_page.title,
title_link: result_url,
thumb_url: "https://upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png",
text: first_snippet,
fields: [
{
title: "Page ID",
value: page_id,
short: true
},
{
title: "Title",
value: title,
short: true
}
]
}
]
};
res.send(body);
}
else {
res.send('something is wrong');
}
});
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});