Added the missing getInstantAnswer plugin

This commit is contained in:
lanxu 2016-01-19 18:43:41 +02:00
parent 2285f87684
commit 62c8bcd584

View File

@ -0,0 +1,40 @@
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;
};