We create a simple Translator chatbot using Google Translation API. We will use the Bot Framework to create a bot in our local environment. We have used the Echo Bot template because of its simple structure.
We modified the Bot file to communicate with Google API. We used German as our target language. The target language can be changed based on preference.
This is part 1 of the series “Advanced Language Translator Bot“.
Pre-requisites
- Google Cloud Platform Account
- Visual Studio 2017 or later
- Bot Emulator
- Bot Framework v4 SDK Templates for Visual Studio
Enabling Google Translation API
Log in to Google Cloud Console. Click on Select a project on the top left.

Click on New Project.

Give the project name and create.

Select the newly created project from the dropdown.

Under Dashboard -> APIs -> Select Go to API Overview.

Click Enable APIs and Services

Search for Google Translation and select Cloud Translation API.

Enable the API. If asked to provide billing account details, provide them. Enabling API will only work if you have a valid Google Billing Account.
To access this API, we will need credentials. Click on Create Credentials.

Select Cloud Translation API from the drop-down and click on “No, I am not using them”. Because we will be using this API in our local environment.

Create a new service account with a preferred role and select key type as JSON.

The private key with service account details will be downloaded in JSON format. Store it safely on your computer.

Setting up Local Environment
In order to communicate with Google API, we will have to create an Environment Variable which will point to the downloaded service account keys.
Click on Start and type Environment Variable and Select “Edit Environment Variable for your account”.
Add a new Environment Variable with Variable name “GOOGLE_APPLICATION_CREDENTIALS” and Variable value “<Path of downloaded JSON>”.

Creating a bot using Bot Builder SDK in C#
Open Visual Studio and create a new project. Select the template as Echo Bot. For learning how to create an Echo bot, go to my post on Create Echo Bot using Bot Builder SDK in Local Environment | Microsoft Bot Framework.
In the Solution Explorer, click on Dependencies and select Manage Nuget Packages. Browse for “Google.Cloud.Translation.V2” and Install it.

Open EchoBot.cs file and replace the code of method “OnMessageActivityAsync(ITurnContext turnContext, CancellationToken cancellationToken)” with below code.
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
var client = TranslationClient.Create();
var response = client.TranslateText(turnContext.Activity.Text, LanguageCodes.German, LanguageCodes.English);
var replyText = $"{response.TranslatedText}";
await turnContext.SendActivityAsync(MessageFactory.Text(replyText, replyText), cancellationToken);
}
Make sure you import “Google.Cloud.Translation.V2” library. You can also change the target language from German to any other language.
Press Cntrl+F5 to run the application. Open Bot Emulator and give the bot URL to start the conversation.
