Excited to find a use for the new Amazon Translate service from AWS, I finally decided on creating a Twitter bot that would translate tweets when a user would @mention the bot. If you haven’t already, check out the features of this new service currently in preview mode out of us-east-1. (Want to be a member of the completely free preview? Just fill out this form!)

As I was building this, I was on the fence with how to get the data out of Twitter. I originally went down the path of building an ECS service that would subscribe to the Twitter streaming API (similar to something I did here), but noticed that Twitter is deprecating the streaming API in favor of a new, webhook based workflow. Unfortunately, that new API is in beta, and according to the sign up email I received, will take a few weeks to be approved.

Hooked on this notion of using webhooks instead of a streaming API, I scoured the internet and found Zapier. I’ve never heard of this service before, but it’s super intuitive to use and has an outstanding UI. Purpose built to

“… move(s) info between your web apps automatically, so you can focus on your most important work.”

I bite, because it has a native Twitter -> webhook integration. Setup is really straightforward — build a new Twitter application, create the appropriate keys, share them with Zapier, and voilà! With this configured, I now had a methodology that would listen for @MyTranslatorBot on Twitter, and when found, send a webhook to wherever I’d like, containing whatever data I wanted. Of particular use where the fields:

  • screen_name: so I knew who to @mention in return, necessary for a response tweet
  • status_id: also required for the response tweet
  • status: the actual tweet the user posted

Zapier is pretty bad ass — the UI parses out all this information for you, allows you to add all sorts of additional headers (helpful for specifying an API key later on), change the payload type, and oh yeah, test it as much as you need before going live. OK, enough of the Zapier love fest.

With the webhook in place, it was time to build the serverless application. Serverless was the first “serverless” framework I ever used — then I experimented with others (Chalice, Zappa, AWS SAM), and I’ve found myself returning to Serverless. It’s by far the most mature and has a serious community behind them… and it shows.

Building the app using Serverless was straightforward. I have a single AWS Lambda function that is invoked by an API Gateway endpoint via an HTTP POST method (the webhook from Zapier). Once invoked, the function does the following:

  • Parse the body and check for the mandatory fields mentioned earlier
  • Extract the target language specified in the tweet and ensure it’s one of the Amazon Translate supported languages
  • Makes a call to Amazon Translate to translate the tweet
  • Sanitizes the translated tweet — e.g. checks for character length and splits it into 2 tweets if needed, removes the @MyTranslatorBot mention, includes an @user_who_tweeted mention so the tweet will get tracked as a response, etc.
  • Saves the original tweet, screen_name, timestamp, target language, and translated tweet into DynamoDB
  • Finally, tweets the translated tweet back to the user via the Twitter API

I used AWS Systems Manager Parameter Store to store all of my Twitter API credentials, and used my own CMK in KMS, which was easy enough to define within the serverless.yml template. I’m still a little shaky on the workflow of storing secrects — on the one hand I used the serverless template to create the KMS CMK, but then I manually create the parameters via the CLI. Something just feels off though as I want everything to be contained together. But I also don’t want to specify secrets in cleartext, so perhaps this is just the process?

Anyway, all in all this was a fun weekend project. The translation works great (even though I can’t really verify its accuracy!), though it is fun to specify target languages like #ar or #zh and have it return fun characters.

Want to see it in action? Give it a try yourself! Simply compose a tweet, being sure to include @MyTranslatorBot along with a target language with a hashtag like #es, and you’ll get a response within 5 minutes.

Let me know what you think!