I created a bot project using bot composer with multiple language generation files. I created a one custom action to identify which language bot should initiate conversation/chat which will be called before greeting dialog/before welcome message.
I had deployed bot to Wats App channel with default language as English(en-US) (Bot Composer -->Project Settings--> Bot Languages-->English).
Even thought bot is deployed to Azure web app in English language, but If custom action dialog result returns Italian ("it-it") language, bot should show greeting response with "it-it" lg file response only.
How do I achieve this kind of functionality in Bot Composer?
Ex: Custom Action Output : en-Us
Bot: Hi, Welcome to Multi Lingual Bot
Ex: Custom Action Output(Italy) : it-it
Bot: Ciao, benvenuto in Multi Lingual Bot
Solution: Create your own Middleware Component in Bot Composer Project. I had provided steps to follow how to create middleware with partial coding.
Code:
public class LanguageDetectionMiddleware : IMiddleware
{
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default)
{
//Step1: Fetch Channel Data
// Step2: Fetch Coutry Code and Number : Since I am using wats app channel, I will get phone number of any user with country code and number.
string channelLocale = await FetchLocaleFromChannelMobileNumber(Channeldata);
//Step4 : Assign turn activity locale to ChannelLocale.
turnContext.Activity.Locale = channelLocale;
//save the language to user state.
await this.userstate.SaveChangesAsync(turnContext, false, cancellationToken);
}
}
Code changes to be done in Startup.cs File:
//step5: Add middleware in startup.cs file adapter
.Use(s.GetService<LanguageDetectionMiddleware>());
// step6: Add Language Middle to Configure Services
services.AddSingleton(sp =>
{
return new LanguageDetectionMiddleware(sp.GetService<IBotTelemetryClient>(), userState);
});