40 lines
1.0 KiB
JavaScript
40 lines
1.0 KiB
JavaScript
var Promise = require('promise');
|
|
var DDG = require('node-ddg-api').DDG;
|
|
|
|
module.exports = function(config) {
|
|
|
|
var ddg = new DDG('kulmapaikka-ircbot');
|
|
|
|
var getAnswer = function(queryText) {
|
|
var promise = new Promise(function(resolve, reject) {
|
|
ddg.instantAnswer(queryText, {skip_disambig: '0'}, function(error, response) {
|
|
if(!error) {
|
|
var str = '';
|
|
if(response.AbstractText !== '') {
|
|
var text = '';
|
|
if(response.AbstractText.length > 350) {
|
|
text = response.AbstractText.substring(0, 350-response.AbstractURL.length) + '...';
|
|
} else {
|
|
text = response.AbstractText;
|
|
}
|
|
|
|
str = 'Vastaus: ' + text + '" | ' + response.AbstractURL;
|
|
resolve(str);
|
|
}
|
|
else if(response.RelatedTopics.length > 0) {
|
|
str = 'Vastaus: "' + response.RelatedTopics[0].Text + '" | ' + response.RelatedTopics[0].FirstURL;
|
|
resolve(str);
|
|
} else {
|
|
reject();
|
|
}
|
|
} else {
|
|
reject();
|
|
}
|
|
});
|
|
|
|
});
|
|
return promise;
|
|
};
|
|
|
|
return getAnswer;
|
|
}; |