kulmapaikka-ircbot/plugins/getInstantAnswer.js
2019-08-13 19:37:22 +03:00

38 lines
1.1 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(new Error(''))
}
} else {
reject(new Error(''))
}
})
})
return promise
}
return getAnswer
}