Create a Simple Language Detector Bot using Bot Builder SDK and Google Translation API

We create a simple Language Detector 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. This is part 2 of the series “Advanced Language Translator Bot“.

Pre-requisites

  1. Google Cloud Platform Account
  2. Visual Studio 2017 or later
  3. Bot Emulator
  4. Bot Framework v4 SDK Templates for Visual Studio

This demo is in continuation of Part 1 where we created a Simple Language Translator Bot using Bot Builder SDK and Google Translation API. I assume that you have followed Part 1 because in this demo we will directly start to code. You must have the Cloud Translation API active and environment variable set to the downloaded configuration file.

Video

Creating a bot using Bot Builder SDK in C#

Create the same Echo bot project created in Part 1. Install the NuGet package “Google.Cloud.Translation.V2” and import it in EchoBot.cs.

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 text = turnContext.Activity.Text;
            var detection = client.DetectLanguage(text);
            var replyText = $"Language: {detection.Language}\tConfidence Score: {detection.Confidence * 100}";
            await turnContext.SendActivityAsync(MessageFactory.Text(replyText, replyText), cancellationToken);
        }

Press Cntrl+F5 to run the application. Open Bot Emulator and give the bot URL to start the conversation.

Thank you All!!! Hope you find this useful.


Up ↑

%d bloggers like this: