Bot Framework v4 multilingual bot sample
This bot has been created using Microsoft Bot Framework, it shows how to translate incoming and outgoing text using a custom middleware and the Microsoft Translator Text API.
-
Node.js version 10.14 or higher
# determine node version node --version
-
Microsoft Translator Text API key
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 inindex.js
with your translation key:adapter.use(new TranslatorMiddleware(languagePreferenceProperty, process.env.translatorKey));
- 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
- Install modules
npm install
- Start the bot
npm start
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 version 4.2.0 or greater from here
- Launch Bot Framework Emulator
- File -> Open Bot Configuration
- 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.
After creating the bot and testing it locally, you can deploy it to Azure to make it accessible from anywhere. To deploy your bot to Azure:
# login to Azure
az login
# set you Azure subscription
az account set --subscription "<azure-subscription>"
# provision Azure Bot Services resources to host your bot
msbot clone services --name "<your_bot_name>" --code-dir "." --location westus --sdkLanguage "Node" --folder deploymentScripts/msbotClone --verbose
If you used the .env
file to store your translationKey
then you'll need to add this key and its value to the Application Settings for your deployed bot.
- Log into the Azure portal
- In the left nav, click on
Bot Services
- Click the
<your_bot_name>
Name to display the bot's Web App Settings - Click the
Application Settings
- Scroll to the
Application settings
section - Click
+ Add new setting
- Add the key
translationKey
with a value of the Translator Text APIAuthentication key
created from the steps above
As you make changes to your bot running locally, and want to deploy those change to Azure Bot Service, you can publish those change using either publish.cmd
if you are on Windows or ./publish
if you are on a non-Windows platform. The following is an example of publishing
# run the publish helper (non-Windows) to update Azure Bot Service. Use publish.cmd if running on Windows
./publish
To learn more about deploying a bot to Azure, see Deploy your bot to Azure for a complete list of deployment instructions.