Skip to content

Latest commit

 

History

History

javascript_typescript

Luis Translator Bot Sample

This sample shows how to translate incoming and outgoing text using a custom middleware and the Microsoft Translator Text API. This bot example uses restify and dotenv.

To try this sample

  • Clone the repository
    git clone https://github.com/Microsoft/botbuilder-samples.git
  • In a terminal, navigate to experimental/javascript_typescript/multilingual-luis-bot
    cd experimental/javascript_typescript/multilingual-luis-bot
  • [Optional] Update the .env file under experimental/javascript_typescript/multilingual-luis-bot with your botFileSecret For Azure Bot Service bots, you can find the botFileSecret under application settings.
  • Install modules and start the bot
    npm i & npm start
    Alternatively you can also use nodemon via
    npm i & npm run watch

Microsoft Translator Text API

To consume the Microsoft Translator Text API, first obtain a key following the instructions in the Microsoft Translator Text API documentation. Paste the key in the translationKey setting in the .env file, or use your preferred configuration and update the following line in index.js with your translation key:

adapter.use(new TranslatorMiddleware(languagePreferenceProperty, process.env.translatorKey));

Testing the bot using Bot Framework Emulator

Microsoft Bot Framework Emulator is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel.

Install the Bot Framework Emulator from here

Connect to bot using Bot Framework Emulator V4

  • Launch Bot Framework Emulator
  • File -> Open Bot Configuration and navigate to experimental/javascript_typescript/multilingual-luis-bot folder
  • Select luis-translator-bot.bot file

Prerequisite

Install TypeScript

In order to run this sample, you must have TypeScript installed. To install TypeScript:

  • Navigate to the TypeScript portal.
  • Click the Download button.
  • Follow the installation instructions for your development environment.

Set up LUIS

{
    "type":"luis",
    "name":"<some name>",
    "appId":"<an app id>",
    "version":"<a version number>",
    "authoringKey":"<your authoring key>",
    "subscriptionKey":"<your subscription key>",
    "endpoint":"<region>",
    "id":"<some number>"
}
  • Update index.ts and set the LUIS_CONFIGURATION value to match the name or id field in your service declaration.

Creating a custom middleware

Translation Middleware: We create a translation middleware than can translate text from from user to bot and apply some customizations like using a custon no-translate patterns or a custom vocab dictionary, allowing the creation of multilingual bots.

LUIS

Language Understanding service (LUIS) allows your application to understand what a person wants in their own words. LUIS uses machine learning to allow developers to build applications that can receive user input in natural language and extract meaning from it.

Microsoft Translator Text API

The Microsoft Translator Text API, Microsoft Translator Text API is a cloud-based machine translation service. With this API you can translate text in near real-time from any app or service through a simple REST API call. The API uses the most modern neural machine translation technology, as well as offering statistical machine translation technology.

Deploy this bot to Azure

You can use the MSBot Bot Builder CLI tool to clone and configure any services this sample depends on.

To install all Bot Builder tools -

Ensure you have Node.js version 8.5 or higher

npm i -g msbot chatdown ludown qnamaker luis-apis botdispatch luisgen

To clone this bot, run

msbot clone services -f deploymentScripts/msbotClone -n <BOT-NAME> -l <Azure-location> --subscriptionId <Azure-subscription-id>

Further reading