Code:
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
//trust Webchat &SMS channel
MicrosoftAppCredentials.TrustServiceUrl(@"https://webchat.botframework.com", DateTime.MaxValue);
MicrosoftAppCredentials.TrustServiceUrl(@"https://sms.botframework.com", DateTime.MaxValue);
Trace.TraceInformation($"Incoming Activity is {activity.ToJson()}");
if (activity.Type == ActivityTypes.Message)
{
if (!string.IsNullOrEmpty(activity.Text))
{
//detect language of input text
var userLanguage = TranslationHandler.DetectLanguage(activity);
//save user's LanguageCode to Azure Table Storage
var message = activity as IMessageActivity;
try
{
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, message))
{
var botDataStore = scope.Resolve<IBotDataStore<BotData>>();
var key = new AddressKey()
{
BotId = message.Recipient.Id,
ChannelId = message.ChannelId,
UserId = message.From.Id,
ConversationId = message.Conversation.Id,
ServiceUrl = message.ServiceUrl
};
var userData = await botDataStore.LoadAsync(key, BotStoreType.BotUserData, CancellationToken.None);
var storedLanguageCode = userData.GetProperty<string>(StringConstants.UserLanguageKey);
//update user's language in Azure Table Storage
if (storedLanguageCode != userLanguage)
{
userData.SetProperty(StringConstants.UserLanguageKey, userLanguage);
await botDataStore.SaveAsync(key, BotStoreType.BotUserData, userData, CancellationToken.None);
await botDataStore.FlushAsync(key, CancellationToken.None);
}
}
}
catch (Exception ex)
{
throw ex;
}
//translate activity.Text to English before sending to LUIS for intent
activity.Text = TranslationHandler.TranslateTextToDefaultLanguage(activity, userLanguage);
//await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
await Conversation.SendAsync(activity, MakeRoot);
}
else {
await Conversation.SendAsync(activity, MakeRoot);
}
}
else
{
HandleSystemMessageAsync(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
internal static IDialog<object> MakeRoot()
{
try
{
return Chain.From(() => new RootDialog());
}
catch (Exception ex)
{
throw ex;
}
}
我正在Bot框架中实现Microsoft语言翻译器,并且还实现了语言理解服务(Luis)。在我的代码中,一旦用户向机器人发送了一条消息,语言翻译器便会先检测用户语言,然后再在根对话框中将其发送给LUIS(我的LUIS应用程序是英语),然后机器人将响应翻译回用户语言使用状态数据。
在我使用表单流之前,一切工作都很好,如果不是英文单词,则由bot用户在表单流中输入的某些文本将被翻译成其他一些语言。请如何在不删除语言翻译API的情况下更正此问题?
注意: 我的RootDailog中有一个调用表单流的部分。无论如何,我可以阻止语言翻译器API翻译表单输入吗?
答案 0 :(得分:1)
如果堆栈中的最后一个对话框是FormFlow对话框,则可以检查对话框堆栈,而不进行翻译。像这样:
bool translate = true;
using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity))
{
var botData = scope.Resolve<IBotData>();
await botData.LoadAsync(default(CancellationToken));
var stack = scope.Resolve<IDialogTask>();
if (stack.Frames != null && stack.Frames.Count > 0)
{
var lastFrame = stack.Frames[stack.Frames.Count - 1];
var frameValue = lastFrame.Target.GetType().GetFields()[0].GetValue(lastFrame.Target);
if (frameValue.GetType() == typeof(FormDialog<SandwichOrder>))
{
//in the FormFlow dialog ... do NOT translate
translate = false;
}
}
}
if (translate)
{
//translate activity.Text to English before sending to LUIS for intent
activity.Text = TranslationHandler.TranslateTextToDefaultLanguage(activity, userLanguage);
}
await Conversation.SendAsync(activity, MakeRoot);