-
Notifications
You must be signed in to change notification settings - Fork 0
/
knowledge.js
51 lines (45 loc) · 1.72 KB
/
knowledge.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
const datastore = require('./datastore');
const maps = require('./maps');
const DEFAULT_RESPONSE = `Sorry, I couldn't find anything to help with that!`;
const graph = {
['geo-city']: {
ServiceProvider: servicesNearLoc
}
};
// Populate the cache on start-up.
maps.populateCache();
// Save the maps cache every 5 mins.
setInterval(function () {
maps.saveCache();
}, 300000);
// Make sure the cache saves before shutdown.
process.on('exit', maps.saveCache);
process.on('SIGINT', maps.saveCache);
process.on('SIGUSR1', maps.saveCache);
process.on('SIGUSR2', maps.saveCache);
process.on('uncaughtException', maps.saveCache);
// TODO: Maybe make this so that they are only retrieved once from the database at start-up?
let trusts = [];
datastore.datastore().Trusts.findAll({raw: true, attributes: ['Name']}).then(values =>
trusts = values.map(value => value.Name)
);
// TODO: Need to make methods for transforms as well as make a knowledge base for DF of common questions.
async function servicesNearLoc(params) {
console.log('Called');
let results = await maps.distanceMatrix(params['geo-city'], trusts);
console.log(results);
return `Here are some services near ${params['geo-city']}:\n${results.map(result => {
return `\t${result.name} (${result.distance.text})`;
}).join('\n')}`;
}
exports.query = async function (intent, entities) {
console.log(`Querying the graph for mapping: ${Object.keys(entities)} -> ${intent}`);
const inputs = Object.keys(entities);
if (inputs in graph) {
if (intent in graph[inputs]) {
console.log(`Found transform: ${graph[inputs][intent]}`);
return await graph[inputs][intent](entities);
}
}
return DEFAULT_RESPONSE;
};