You are invited to Log in or Register a free Frihost Account!

Google Translate on a Bot

The first thing I did with the bot was the simple 'get a text, answer with another text (or with the same text)', and after I started checking the modules, I realized I could access the internet through node.js. It's wonderful, since I had been wanting to access some dictionaries remotely. I looked into the http modules and found some good ones like request and cheerio. I made a tiny scraper to get the dollar value in Bolivars (Venezuelan currency) from the twitter of DolarToday, who update it everyday.

bot.onText(/\/dolar/, function (msg) {
    request('', function (error, response, html) {
        if (!error && response.statusCode == 200) {
            var loadedHTML = cheerio.load(html);
            var contentContainer = loadedHTML('p.ProfileHeaderCard-bio').text();
            var soughtContent = contentContainer.substring("Bs.")," y el"));
            return bot.sendMessage(, soughtContent);
        } else {
    console.log('Sent dollar value');

Now whenever someone sends /dolar to my bot, it goes to Twitter, pulls a paragraph, then cuts it up to where the dollar value is and sends a message, to the chat where the command was sent, with the dollar exchange value. But I didn't stop there, I still wanted to access dictionaries online, so I found a module simply called node-google-translate-skidz and called it for my bot. I didn't even need to scrape anything. I had even requested a Google Translate API key a few days before thinking that I would have to build it myself. The code is very simple and works very efficiently.

bot.onText(/\/trans (.+)/, function (msg, match) {
    var langA = match[1].substring(0, 2);
    var langB = match[1].substring(2, 4);
    var texto = match[1].substring(match[1].lastIndexOf("\/trans")+5);
    /* bot.sendMessage(, 'Conectando con Google Translate...'); */
            text: texto,
            source: langA,
            target: langB
        }, function (result){
            var trans ={
                return resu.trans;}
            bot.sendMessage(, 'Traducción: ' + trans);

It's fun because at first I was struggling with how the hell I would work out what language people wanted to translate from and to. I even made the command look like /enes for English->Spanish while I built the translation getter and sender. But then I remembered the string cutters built into Javascript and was left astounded with the simplicity of just using the user-sent data and inputing it directly without actually creating a copy of the code for each language. Now people just have to send /trans ende Whatever text to translate and the bot grabs the first 7 characters, disposes of them, grabs the next two characters, puts them in the source language field, the next two characters on the target language field, disposes of the next character (the space) and sends the rest to the translation machine to be munched and swallowed and defecated, and sends the results to the source chat.

These were the first times I mixed two modules in the same code snippet and I felt giddy just thinking of the possibilities.

0 blog comments below

© 2005-2011 Frihost, forums powered by phpBB.