The making of translation-bot

Screen Shot 2014-10-15 at 12.01.23 pm.png

When I first started programming, I often frequented many IRC channels on freenode to learn Ruby, Rails, and JavaScript. There were many natural languages spoken on those channels and I was inspired to write an IRC bot that would translate their words. Alas, I slowly retired from those channels and no longer found the use for such a bot.

Then, I arrived at Hacker School. We use Zulip for our internal chat at Hacker School, and although 99% of the conversation there is in the English language, I thought it would be a fun project to tackle anyway.

The first step was to find a translation service that had an API wrapper in Python (my language of choice). I could not find any that I liked so I built one myself.

To build the API wrapper, I first checked to see what results the Google Translate API would give me and what the API needs to fulfil my request. From there, I wrote the functions that would handle those results. When I was building this API wrapper, I ran into the challenge of hiding my API key. There were two approaches I tried and suggested to me. The first was to store the key in a file that is also in the .gitignore list and the second was to add it as an environment variable. I chose to go with the latter because I found the code to be cleaner.

Another challenge was how to handle tests. As of the moment, I am making requests to the Google Translate API but from reading other people’s API wrappers, many of them use mock results to test. Writing better tests is already in my issue list, and something I’ll tackle post-Hacker School.

Perhaps the biggest hairball of all in my quest to write this API wrapper was packaging it and releasing it to PyPi. Much of what I did was taking a look at other people’s repositories of their API wrappers and see how they’ve set it up for distributing. The single most important component of packaging was the file. PyPi uses the metadata in to display the package and if applicable, run scripts necessary for the package as well. Also, PyPi does not upload any changes the developer has made if the version it not updated, so incrementing the version was necessary. I did not know a thing about versioning but Tom introduced me to semantic versioning and I’ve been (attempting to) practice it with my API wrapper.

With the API wrapper functioning, I got started to working on my Zulip bot. Zulip has a Python module for dropping into your project. Installing it however, isn’t exactly as a pip install. First, download the tarball then run python install. Then drop the zulip folder into your project folder and import zulip from your bot’s .py file. Zulip’s API docs have pretty good usage examples, but if you prefer reading other people’s code, I highly recommend Daria’s gif-bot, Alice’s mood-bot, and Brian’s meme-bot.

To keep translation-bot running, I created a Heroku instance with 1 worker. The Procfile tells Heroku what command to execute and in my case, it was python to keep the bot running on loop on their machines instead of mine.

To date, translation-bot is still alive and well (it has only been a week though so I may be speaking too soon!), and I was pleasantly surprised to see lots of people have fun with it, break it, and get funny results.

Screen Shot 2014-10-15 at 1.55.02 pm.png


Now read this

Pair Programming, Pair Blogging at Hacker School

Hacker School is an experiment and I am 5 weeks into it. I’ve written code I want to write for myself, had the opportunity to listen to many educational talks, and met a countless number of impressive and amazing people. I can... Continue →