Building a Google Translate module with Converse.AI Extensions!

Graeme Cassels
State of the Conversation
3 min readDec 13, 2017

One of the more interesting features of the Converse.AI platform is the ability to execute Modules. Modules can be used to fetch, set, or manipulate external data in harmony with internal Converse.AI data. Need to synchronize customer data across Converse.AI and Hubspot? Use the Hubspot module. Need to run some calculus? Use the Maths module. Need to make an HTTP call? You get the idea.

This ability has allowed Converse.AI users to create some very powerful workflows integrated directly into their chatbot interfaces such as Facebook Workplace, Google Assistant, Slack, Intercom and many more.

Now, with the addition of Converse.AI Extensions, users of the platform can program their very own Modules from scratch 😮! This leads to an infinite number of possibilities satisfying any and all business scenarios.

Prerequisite

To write a Google Translate module for the Converse.AI platform, ensure you have the following prerequisites:

Setting up the Plugin

After initializing the project folder we can install our dependencies via npm. In this case, the only dependency is the Google Translate package.

npm install @google-cloud/translate --save

Remember to add --save or add it directly to your package.json file — this is important because during deployment, Converse.AI will need to install these dependencies.

In order to use the Google Translate package you’ll need to download the authentication credentials as a JSON file from your Google Cloud Platform project and store it in the root of your project directory as credentials.json.

Writing a Module

As you know by now, when generating a module through the Converse.AI CLI tool, all the boiler plate code is taken care of, allowing you to focus on the business logic.

Assuming that we have created a module called translate with two parameters, text and to, we can add the following business logic. First let’s require the @google-cloud/translate package into our module and set the keyFilename property to the credentials.json that you downloaded earlier.

const gcloud = require('@google-cloud/translate')({
keyFilename: path.join(__dirname, '../credentials.json')
});

Next after checking text and to are not undefined, replace the boiler plate code with the following:

gcloud.translate(text, to).then((results) => {
const translation = results[0];
var response = new ModuleResponse();
response.setValue({text: text, translation: translation});
app.send(Status.SUCCESS, response);
}).catch((err) => {
app.send(Status.FAIL);
});

And that’s it! 👍 You can now deploy your new Google Translate plugin to your Converse.AI account and add the Module to a template. You can access the translated data in the template via the module’s handlebars with the property translation.

What’s Next?

How about adding a from parameter to define the language of the input text. Better yet, use the Google Translate package to detect the language of the input text. Or, how about allowing the target language to be written as a word (“Spanish”) instead of a language token (“es”). What happens if there is an error, can you return the error in a better format. What else can you think of to improve your new plugin?

Take a look at the code for this module (and many more) in the converseai-extensions github page. Or dive into the Converse.AI Extensions docs to create some unique modules of your very own — feel free to share them with me.

--

--

Graeme Cassels
State of the Conversation

Software Engineer @Smartsheet working on @Converse.AI | Forever a student of film | Snowboarder | Adventurer