解决方案:在 Bot Composer 项目中创建自己的中间件组件。我提供了步骤来遵循如何使用部分编码创建中间件。
代码:
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);
}
}
要在 Startup.cs 文件中进行的代码更改:
//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);
});