-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
82 lines (61 loc) · 2.54 KB
/
server.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
// init
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
// const http = require('http');
const https = require('https');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
// webhook handler
app.post('/wikilookup', (req, res) => {
/*
static wiki lookup path - we will append the correct search term later
this type of lookup requires the title to be a match to the WIKI page we are trying to get an extract from
we can name our Dialogflow entities the same as the WIKI page to make this easier for us
*/
const wikiReqPath = 'https://en.wikipedia.org/w/api.php?format=json&formatversion=2&action=query&prop=extracts&exintro=&explaintext=&titles=';
// wiki also has "open search", which could be considered also
// const wikiOpenSearchPath = 'https://en.wikipedia.org/w/api.php?&format=json&action=opensearch&limit=2&profile=fuzzy&search=global%20warming';
// get all parameters from the Dialogflow POST request
let parameters = req.body.queryResult.parameters;
// only proceed if parameters are sent (shouldn't really happen)
if (isEmptyObject(parameters)) {
return res.json({
fulfillmentText: 'Something went wrong! Parameters object is empty!',
source: 'wikilookup'
});
}
// we can split logic based on e.g. parameters if we want
// in our case we have a parameter "painter" that we might want to look up in a specific way
if (parameters.painter) {
let wikiReqUrl = encodeURI(`${wikiReqPath}${parameters.painter}`);
https.get(wikiReqUrl, (responseFromAPI) => {
let completeResponse = '';
responseFromAPI.on('data', (chunk) => {
completeResponse += chunk;
});
responseFromAPI.on('end', () => {
let searchResults = JSON.parse(completeResponse);
return res.json({
fulfillmentText: searchResults.query.pages[0].extract,
source: 'wikilookup'
});
});
}, (error) => {
return res.json({
fulfillmentText: 'Something went wrong!',
source: 'wikilookup'
});
});
}
});
// simple check for empty object
function isEmptyObject(obj) {
return !Object.keys(obj).length;
}
// listen for requests :)
var listener = app.listen(process.env.PORT, function() {
console.log('Your app is listening on port ' + listener.address().port);
});