From 212ae87e8b39d33181862dfae4aa454844dd8f2d Mon Sep 17 00:00:00 2001 From: reality Date: Thu, 3 Mar 2016 15:52:29 +0000 Subject: [PATCH] translate module --- modules/translate/translate.js | 70 ++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 modules/translate/translate.js 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); +};