diff --git a/modules/translate/translate.js b/modules/translate/translate.js new file mode 100644 index 0000000..6470908 --- /dev/null +++ b/modules/translate/translate.js @@ -0,0 +1,70 @@ +/** + * Module name: Translate + * Description: Translate things (funnily enough) + */ + +var _ = require('underscore')._, + request = require('request'); + +var translate = function(dbot) { + var ApiRoot = 'https://glosbe.com/gapi/translate'; + + this.api = { + 'getTranslation': function(from, to, word, callback) { + request.get(ApiRoot, { + 'qs': { + 'from': from, + 'dest': to, + 'phrase': word, + 'format': 'json' + }, + 'json': true + }, function(err, res, body) { + console.log(err); + console.log(body); + if(!err && _.has(body, 'tuc')) { + callback(false, body.tuc); + } else { + callback(true, null); + } + }); + } + }; + + this.commands = { + 't': function(event) { + var from = event.params[1], + to = event.params[2], + word = event.params[3]; + + this.api.getTranslation(from, to, word, function(err, results) { + if(!err) { + if(results.length > 0) { + var answerStrings = [], + translation, + aString; + + for(var i=0;i 0) { + aString += ' (' + _.unique(_.pluck(translation.meanings, 'text')).join(', ') + ')'; + } + answerStrings.push(aString); + } + + event.reply(word + ' in ' + to + ': ' + answerStrings.join(' ')); + } else { + event.reply('No known translations for that word'); + } + } else { + event.reply('Unable to get translations :\'('); + } + }); + } + }; +} + +exports.fetch = function(dbot) { + return new translate(dbot); +};