The Babel-bot: Make your bot fluent in every language

Olivier Leplus
Botfuel
Published in
3 min readAug 22, 2018

Chatbots are everywhere. There is no messaging platform without its chatbot solution. Even the web has its chatbots.

There are roughly 6,500 spoken languages in the world today but most of the chatbot platforms only support a few of them. Fortunately, we can use 3rd party solutions to "add" new languages support.

In this article, we are going to see how to use middlewares in Botfuel to make our bot fluent in any language!

How do middlewares work?

Middlewares are functions that are executed either before a message is processed by the bot or before the message is sent back to the user.

Typical chatbot workflow with middlewares

As per Botfuel documentation, middlewares are defined in the file src/middlewares.js of our project, which exports two lists of functions called "in" and "out".

module.exports = {
in: [
async (context, next, done) => { /** middleware IN 1 */ },
async (context, next, done) => { /** middleware IN 2 */ },
],
out: [
async (context, next, done) => { /** middleware OUT 1 */ },
],
};

In this article, we are going to use the Google Translate API using the google-translate-api node package.

We will also use the brain to remember the language detected by Google Translate in order to reuse it later.

Our chatbot workflow

The input middleware

Let’s start with the input middleware, which will be responsible for translating a user phrase to English from any supported language.

Fortunately, Google translate can automatically detect the language of a given text so we won't have to deal with this part. All we need to do is translate the text to english (en) as this is the language of our bot. Of course, we need to make sure the message is of type text before calling the Google translate API ;)

Once we have translated the message, we replace the original text by its translation and return next() to continue the normal bot workflow.

We also store the detected language in the brain so that the bot will be able to reply with the user’s language and not forget it later in the conversation.

The output middleware

The output middleware is quite similar to the input middleware.

We retrieve the language of the input from the brain and use it as the destination language in Google translate for every message. And, as we did in the input middleware, we simply replace the original message by the translated message before returning next().

Wrapping up

Once we have our two methods, we just need to export them in the file src/middlewares.js of our project and we are done.

Try it

You can try this middleware on the bot used in the Getting started tutorial.

Follow @olivierleplus to know more about the Web Platform and JS development.

Follow @botfuel to know more about Botfuel chatbot platform.

If you liked it, support us on Github

--

--

Olivier Leplus
Botfuel
Writer for

Developer Relations Manager @Microsoft & Google Developer Expert Web Technologies