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.
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.
Leave a Reply