How to Translate English to Mandalorian with Twilio Serverless and SMS
Time to read: 3 minutes
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.
Prerequisites
- A Twilio account - sign up for a free one here and receive an extra $10 if you upgrade through this link
- A Twilio phone number with SMS capabilities - configure one here
- Postman (you could alternatively make cURL requests from the command line)
- Node.js installed - download it here
Make an API Request to Fun Translations
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:
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:
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:
Create your new project and install our lone requirement superagent
, an HTTP client library to make HTTP requests in Node.js, by running:
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:
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:
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.
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?
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
- GitHub: elizabethsiegle
- Email: lsiegle@twilio.com
- Livestreams: twitch.tv/lizziepikachu
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.