2019-08-13 19:37:22 +03:00
|
|
|
var Promise = require('promise')
|
|
|
|
var DDG = require('node-ddg-api').DDG
|
2016-01-19 18:43:41 +02:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
module.exports = function (config) {
|
|
|
|
var ddg = new DDG('kulmapaikka-ircbot')
|
2016-01-19 18:43:41 +02:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
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
|
|
|
|
}
|
2016-01-19 18:43:41 +02:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
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
|
|
|
|
}
|
2016-01-19 18:43:41 +02:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
return getAnswer
|
|
|
|
}
|