Get the List of All Supported Languages for Language Translator Bot | Microsoft Bot Framework

In this post, we will get the list of all supported languages in Azure Translation API using Bot Builder SDK v4 in C#. This is part of the series from Advanced Language Translator Bot using Azure Translation API.


Get Supported Languages List

In our last part, we have created a welcome menu card that will be shown to the user with 3 options. We will implement the first option now by capturing the user response.

Add the below code inside the ActStepAsync in MainDialog. This will capture the user response and call the method which calls an API to get the list. We then show all the list back to the user.

private async Task<DialogTurnResult> ActStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
            stepContext.Values["Operation"] = ((FoundChoice)stepContext.Result).Value;
            switch ((string)stepContext.Values["Operation"])
                case "Supported Languages":
                    string supportedLanguages = await _translatorRepository.GetSupportedLanguage("");
                    await stepContext.Context.SendActivityAsync(MessageFactory.Text(supportedLanguages), cancellationToken);
                    return await stepContext.NextAsync(null, cancellationToken);
            return await stepContext.NextAsync(null, cancellationToken);

Create a new folder at the root location with the name Utility. Add a new class file TranslatorRepository.cs to it.

Add the following methods to this newly created class file. This method calls the API to get the list of all supported languages and return it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;

namespace AzureTranslatorBot.Utilities
    public class TranslatorRepository
        public async Task<string> GetSupportedLanguage(string uri)
                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri(uri);
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, client.BaseAddress);

                var response = await MakeRequestAsync(request, client);
                return PrettyJson(response);
            catch (Exception e)
                throw new Exception();
        public async Task<string> MakeRequestAsync(HttpRequestMessage getRequest, HttpClient client)
            var response = await client.SendAsync(getRequest).ConfigureAwait(false);
            var responseString = string.Empty;
                responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
            catch (HttpRequestException)
                // empty responseString
            return responseString;
        public string PrettyJson(string unPrettyJson)
            var options = new JsonSerializerOptions()
                WriteIndented = true
            var jsonElement = System.Text.Json.JsonSerializer.Deserialize<JsonElement>(unPrettyJson);
            return System.Text.Json.JsonSerializer.Serialize(jsonElement, options);

Create an object of this new class file inside the MainDialog.

protected readonly ILogger Logger;
        TranslatorRepository _translatorRepository;

        // Dependency injection uses this constructor to instantiate MainDialog
        public MainDialog(ILogger<MainDialog> logger, TranslatorRepository translatorRepository)
            : base(nameof(MainDialog))
            _translatorRepository = translatorRepository;
            Logger = logger;

            AddDialog(new TextPrompt(nameof(TextPrompt)));
            AddDialog(new ChoicePrompt(nameof(ChoicePrompt)));

            AddDialog(new WaterfallDialog(nameof(WaterfallDialog), new WaterfallStep[]

            // The initial child Dialog to run.
            InitialDialogId = nameof(WaterfallDialog);

Add the new service in Startup.cs file.


Run the bot and test in emulator.

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

Up ↑

%d bloggers like this: