Celebrate St Patrick's Day: Translate English to Gaelic with Twilio Serverless, SMS, and IBM Watson

March 14, 2022
Written by

Copy of C01 Blog Text (3).png

Celebrate St. Patrick's Day on March 17th by texting a sentence to +13863564094 to translate it to Gaelic, and read on to learn how to build the app using IBM Watson, Twilio Functions, JavaScript, and the Twilio Serverless Toolkit

Screen Shot 2022-03-16 at 10.12.41 AM.png

 

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. Node.js installed - download it here
  4. IBM Cloud account - sign up for a free one here

Setup IBM Watson

Go to the IBM Watson Cloud Translator console, select the free Lite plan, and make a new service by clicking Create on the righthand panel.

watson create service

Whoop whoop! You've just made your free Watson Translator service!

To get our required credentials, click the Navigation Menu in the top left corner.

navmenu.png

Select Resource list from the dropdown menu followed by your language translator resource.

Keep this page with your API key and URL (which are unique to you) open--we'll use those in the next step!

ibm credentials

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 the lone requirement ibm-watson, an HTTP client library to make HTTP requests in Node.js, by running:

twilio serverless:init irishtranslator
cd irishtranslator
npm install ibm-watson@^6.2.2

Make a .env file and add an environment variable for your IBM API key from the IBM Cloud page in the last step. In this blog post, the API key is called `WATSON_KEY`.

Make a Twilio Function with JavaScript

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

const LanguageTranslatorV3 = require('ibm-watson/language-translator/v3');
const { IamAuthenticator } = require('ibm-watson/auth');
exports.handler = async function(context, event, callback) {
  const twiml = new Twilio.twiml.MessagingResponse();
  const message = twiml.message();
  const msgToTranslate = event.Body.toLowerCase().trim();
  const languageTranslator = new LanguageTranslatorV3({
    version: '2018-05-01',
    authenticator: new IamAuthenticator({
      apikey: context.WATSON_KEY,
    }),
    serviceUrl: {INSERT-YOUR-URL},
  });
  const translateParams = {
    text: msgToTranslate,
    modelId: 'en-ga', //Gaelic language model
  };
  languageTranslator.translate(translateParams)
    .then(translationResult => {
      message.body(`"${msgToTranslate}" translated to Gaelic is: ${translationResult.result.translations[0].translation}`);
      callback(null, twiml);
    })
    .catch(err => {
      console.log('error:', err);
      message.body(err);
      callback(null, twiml);
    });
  };

This code imports the Watson language translator, makes a Twilio Messaging Response object, authenticates to the Watson API using ​​IBM Cloud Identity and Access Management (IAM), and passes the inbound message and the English to Gaelic `en-ga` model to the `translateParams` object`. You could also translate to or from other languages, listed here. We then return the inbound message translated to Gaelic and a St. Patrick's day image in our message response.

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, sentence, or paragraph and receive a response containing it translated to Gaelic.

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, Twilio Runtime seamlessly handles servers for you, and IBM Watson makes it easy to perform a variety of machine learning tasks.

Let me know online what you're building with Serverless and/or machine learning!