【发布时间】:2018-07-25 11:20:59
【问题描述】:
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 应用程序是英文),然后机器人将响应翻译回使用状态数据的用户语言。
在我使用表单流之前一切正常,机器人用户在表单流中的一些输入文本如果不是英语单词,则将被翻译成其他语言。请问如何在不删除语言翻译 API 的情况下更正此问题?
注意: 我的 RootDailog 中有一个部分调用了表单流。无论如何我可以阻止语言翻译器 API 翻译表单输入吗?
【问题讨论】:
-
请分享您使用翻译器 api 的代码,以及您的 FormFlow 对话框的使用方式。 (可以判断哪个对话框在堆栈上,并确定是否相应地调用翻译 api。但是,如果没有您的代码,就不可能确定应该如何完成。)
-
我已经从我的消息控制器中添加了代码以便更好地解释
标签: c# botframework