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
.
- Clone the repository
git clone https://github.com/Microsoft/botbuilder-samples.git
- In a terminal, navigate to samples/javascript_nodejs/17.multilingual-conversations
cd samples/javascript_nodejs/17.multilingual-conversations
- [Optional] Update the .env file under samples/javascript_nodejs/17.multilingual-conversations with your botFileSecret For Azure Bot Service bots, you can find the botFileSecret under application settings.
- Install modules and start the bot
Alternatively you can also use nodemon via
npm i & npm start
npm i & npm run watch
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));
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
- Launch Bot Framework Emulator
- File -> Open Bot Configuration and navigate to samples/javascript_nodejs/17.multilingual-conversations folder
- Select multilingual-conversations.bot file
Translation Middleware: We create a translation middleware than can translate text from bot to user and from user to bot, allowing the creation of multilingual bots. Users can specify their language preference, which is stored in the user state. The translation middleware translates to and from the user's preferred language.
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.
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>