如何在conversationUpdate活动中获取区域设置?
我想为每个基于其语言环境添加的成员实现欢迎消息。 代码如下:
if (message.Type == ActivityTypes.ConversationUpdate) { // some code... if (message.Locale == "en-us") { var reply = message.CreateReply("Hello world"); } else { // some code... } // some code... }
奇怪的是,即使我在使用bot模拟器和BotFramework-WebChat测试它时都设置了语言环境,语言环境也是null
。 收到消息时,locale属性正常工作。
有没有办法在conversationUpdate活动期间获取语言环境?
先感谢您!
在第一次输入之前获得用户语言的一种解决方案是使用backchannel
function。 这允许我们以隐藏的方式将信息推送到我们的机器人,例如提供区域设置或自定义ID。
您将在此处在GitHub帐户中找到此演示。
特别是,这里是您的网络聊天将事件发送到机器人的postActivity
,最后使用postActivity
,从Microsoft的Webchat GitHub描述中的示例创建:
我添加了一个控制台事件来显示发布的活动。
然后由bot的MessageController
接收此事件并进行处理:
[BotAuthentication] public class MessagesController : ApiController { /// /// POST: api/Messages /// Receive a message from a user and reply to it /// public async Task Post([FromBody]Activity activity) { // DEMO PURPOSE: echo all incoming activities Activity reply = activity.CreateReply(Newtonsoft.Json.JsonConvert.SerializeObject(activity, Newtonsoft.Json.Formatting.None)); var connector = new ConnectorClient(new Uri(activity.ServiceUrl)); connector.Conversations.SendToConversation(reply); // Process each activity if (activity.Type == ActivityTypes.Message) { await Conversation.SendAsync(activity, () => new Dialogs.RootDialog()); } // Webchat: getting an "event" activity for our js code else if (activity.Type == ActivityTypes.Event && activity.ChannelId == "webchat") { var receivedEvent = activity.AsEventActivity(); if ("localeSelectionEvent".Equals(receivedEvent.Name, StringComparison.InvariantCultureIgnoreCase)) { await EchoLocaleAsync(activity, activity.Locale); } } // Sample for emulator, to debug locales else if (activity.Type == ActivityTypes.ConversationUpdate && activity.ChannelId == "emulator") { foreach (var userAdded in activity.MembersAdded) { if (userAdded.Id == activity.From.Id) { await EchoLocaleAsync(activity, "fr-FR"); } } } var response = Request.CreateResponse(HttpStatusCode.OK); return response; } private async Task EchoLocaleAsync(Activity activity, string inputLocale) { Activity reply = activity.CreateReply($"User locale is {inputLocale}, you should use this language for further treatment"); var connector = new ConnectorClient(new Uri(activity.ServiceUrl)); await connector.Conversations.SendToConversationAsync(reply); } }
插图de l’arrivéedes消息:
对于讲法语的用户, 这里是我公司博客上更详细的答案