Celebrate St Patrick’s Day translating English to Irish with Twilio Serverless and SMS

Celebrate St. Patrick’s Day on March 17th by texting a sentence to +13863564094 to translate it to Irish, and read on to learn how to build the app using the Irish Translator API, Twilio Functions, and the Twilio Serverless Toolkit.

Prerequisites

A…


This content originally appeared on Twilio Blog and was authored by Lizzie Siegle

Celebrate St. Patrick's Day on March 17th by texting a sentence to +13863564094 to translate it to Irish, and read on to learn how to build the app using the Irish 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

Fun Translations offers multiple translator APIs--in addition to Irish, there's a Yoda translator, Mandalorian, translator, pirate speak translator, Dothraki speak translator, and more. To use the Irish translator API, we need to hit this URL: https://api.funtranslations.com/translate/irish 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 example

{
    "success": {
        "total": 1
    },
    "contents": {
        "translated": "'ello dere",
        "text": "hello there",
        "translation": "irish"
    }
}

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/irish.json?text=${<TEXT_TO_TRANSLATE_HERE>}&X-Funtranslations-Api-Secret=${YOUR_API_KEY}.

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

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 irishtranslator
cd irishtranslator
npm install superagent

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/irish.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}" translates to "${res.body.contents.translated}" in Irish!🍀 Happy St. Patrick's Day!☘️`);
message.media("https://www.worldatlas.com/r/w1200/upload/b3/28/8f/happy-st-patricks-day-3946675-1920.png")
    callback(null, twiml);
  })
  };

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 a St. Patrick's Day-themed image!

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 irishtranslator root directory and run twilio serverless:deploy. Once you see a Function URL ending in /translate, go to the phone numbers section of your Twilio Console and 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 irishtranslator, for Environment select dev-environment, and then for Function Path select /translate.

Configure phone number for messaging in console

Click the Save button below and tada! You can now text your Twilio number a phrase and receive the phrase translated to Irish back in a response.

What's Next for Twilio Serverless and APIs?

How are you celebrating St. Patrick's Day? 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!


This content originally appeared on Twilio Blog and was authored by Lizzie Siegle


Print Share Comment Cite Upload Translate Updates
APA

Lizzie Siegle | Sciencx (2022-03-15T06:17:38+00:00) Celebrate St Patrick’s Day translating English to Irish with Twilio Serverless and SMS. Retrieved from https://www.scien.cx/2022/03/15/celebrate-st-patricks-day-translating-english-to-irish-with-twilio-serverless-and-sms/

MLA
" » Celebrate St Patrick’s Day translating English to Irish with Twilio Serverless and SMS." Lizzie Siegle | Sciencx - Tuesday March 15, 2022, https://www.scien.cx/2022/03/15/celebrate-st-patricks-day-translating-english-to-irish-with-twilio-serverless-and-sms/
HARVARD
Lizzie Siegle | Sciencx Tuesday March 15, 2022 » Celebrate St Patrick’s Day translating English to Irish with Twilio Serverless and SMS., viewed ,<https://www.scien.cx/2022/03/15/celebrate-st-patricks-day-translating-english-to-irish-with-twilio-serverless-and-sms/>
VANCOUVER
Lizzie Siegle | Sciencx - » Celebrate St Patrick’s Day translating English to Irish with Twilio Serverless and SMS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/15/celebrate-st-patricks-day-translating-english-to-irish-with-twilio-serverless-and-sms/
CHICAGO
" » Celebrate St Patrick’s Day translating English to Irish with Twilio Serverless and SMS." Lizzie Siegle | Sciencx - Accessed . https://www.scien.cx/2022/03/15/celebrate-st-patricks-day-translating-english-to-irish-with-twilio-serverless-and-sms/
IEEE
" » Celebrate St Patrick’s Day translating English to Irish with Twilio Serverless and SMS." Lizzie Siegle | Sciencx [Online]. Available: https://www.scien.cx/2022/03/15/celebrate-st-patricks-day-translating-english-to-irish-with-twilio-serverless-and-sms/. [Accessed: ]
rf:citation
» Celebrate St Patrick’s Day translating English to Irish with Twilio Serverless and SMS | Lizzie Siegle | Sciencx | https://www.scien.cx/2022/03/15/celebrate-st-patricks-day-translating-english-to-irish-with-twilio-serverless-and-sms/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.