How to Translate English to Mandalorian with Twilio Serverless and SMS

lizziepika

Lizzie Siegle

Posted on May 3, 2021

How to Translate English to Mandalorian with Twilio Serverless and SMS

This blog post was written for Twilio and originally published on the Twilio blog.

Celebrate Star Wars Day on May 4th by texting a sentence to +14807573107 to translate it into the Mandalorian language and read on to learn how to build the app using the Mandalorian Translator API, Twilio Functions, and the Twilio Serverless Toolkit.
sms example

Prerequisites

  1. A Twilio account - sign up for a free one here and receive an extra $10 if you upgrade through this link
  2. A Twilio phone number with SMS capabilities - configure one here
  3. Postman (you could alternatively make cURL requests from the command line)
  4. Node.js installed - download it here

Make an API Request to Fun Translations

kylo approves gif
Fun Translations offers multiple translator APIs--in addition to the Mandalorian, there's a Yoda translator, pirate speak translator, Dothraki speak translator, and more. To use the Mandalorian translator API, we need to hit this URL: https://api.funtranslations.com/translate/mandalorian passing it some text to translate by appending ?text="<TEXT_TO_TRANSLATE_HERE>".

Open Postman and paste that URL along with some text in the <TEXT_TO_TRANSLATE_HERE> part such as "this is fun" into the URL bar.

Click Send to hit it with a GET request to see the following data returned as seen below:
postman

{
    "success": {
        "total": 1
    },
    "contents": {
        "translated": "Ibic is nuhur",
        "text": "this is fun",
        "translation": "mandalorian"
    }
}
Enter fullscreen mode Exit fullscreen mode

The free tier only lets you make five API requests an hour. You would need to request an API key in order to use the paid version. The URL would then turn into https://api.funtranslations.com/translate/mandalorian.json?text=${<TEXT_TO_TRANSLATE_HERE>}&X-Funtranslations-Api-Secret=${YOUR_API_KEY}, returning data in the same matter as shown below:
postman with api key

{
    "success": {
        "total": 1
    },
    "contents": {
        "translated": "Nynir url ti api key",
        "text": "hit url with api key",
        "translation": "mandalorian"
    }
}
Enter fullscreen mode Exit fullscreen mode

You can upgrade to a paid plan where you can make more HTTP requests here, but it's not necessary for this blog post.

Get Started with the Twilio Serverless Toolkit

The Serverless Toolkit is CLI tooling that helps you develop locally and deploy to Twilio Runtime. The best way to work with the Serverless Toolkit is through the Twilio CLI. If you don't have the Twilio CLI installed yet, run the following commands on the command line to install it and the Serverless Toolkit:

npm install twilio-cli -g
twilio login
twilio plugins:install @twilio-labs/plugin-serverless
Enter fullscreen mode Exit fullscreen mode

Create your new project and install our lone requirement superagent, an HTTP client library to make HTTP requests in Node.js, by running:

twilio serverless:init mandalorian-translation
cd mandalorian-translation
npm install superagent
Enter fullscreen mode Exit fullscreen mode

If you are on a paid subscription with Fun Translations, open the .env file and add an environment variable for your API key. In this blog post below, the API key is called FUNTRANSLATIONS_API_SECRET.

Make a Twilio Function with JavaScript

cd into the \functions directory and make a new file called translate.js containing the following code:

const superagent = require('superagent');
var apiurl="https://api.funtranslations.com/translate/mandalorian.json"
exports.handler = function(context, event, callback) {
  const twiml = new Twilio.twiml.MessagingResponse();
  const message = twiml.message(); 
  const msgToTranslate = event.Body.toLowerCase().trim();
  superagent.get(`${apiurl}?text=${msgToTranslate}`) 
  //.set('X-Funtranslations-Api-Secret',context.FUNTRANSLATIONS_API_SECRET) // use this line to reference your API key from a paid Fun Translations plan if you have one
  .end((err, res) => {
    message.body(`"${msgToTranslate}" in Mandalorian translates to "${res.body.contents.translated}". May the 4th be with you!`);
    message.media("https://cdn.pixabay.com/photo/2015/09/04/18/55/yoda-922520_1280.png")
    callback(null, twiml);
  })
  };
Enter fullscreen mode Exit fullscreen mode

This code imports superagent, makes a Twilio Messaging Response object, retrieves the inbound text message and appends it to the URL we wish to make a HTTP request to. Then the code parses the response from the endpoint (as seen in Postman earlier), to return a text message containing the translated text and an image of Yoda!

You can view the complete app on GitHub here.

Configure the Function with a Twilio Phone Number

To open up our app to the web with a public-facing URL, go back to the mandalorian-translation root directory and run twilio serverless:deploy. You should see this at the bottom of your terminal:
Function URLs
In the phone numbers section of your Twilio Console, select the Twilio number you purchased and scroll down to the Messaging section. Under A MESSAGE COMES IN change Webhook to Function and then under Service select Mandalorian-Translation, for Environment select dev-environment, and then for Function Path select /translate.
Messaging section of phone number
Click the Save button below and tada! You can now text your Twilio number a phrase and receive the phrase translated from the Mandalorian language back in a response.

What's Next for Twilio Serverless, APIs, and Star Wars?

lol gif
How are you celebrating May Fourth? Twilio's Serverless Toolkit makes it possible to deploy web apps quickly, and Twilio Runtime seamlessly handles servers for you.

Let me know online what you're building with Serverless and what your favorite Star Wars quote is! Some of mine include "Never tell me the odds", "Do. Or do not. There is no try", and "Strike me down, and I will become more powerful than you could possibly imagine".
-Twitter: @lizziepika

💖 💪 🙅 🚩
lizziepika
Lizzie Siegle

Posted on May 3, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related