コード例 #1
0
        /// <summary>
        /// POST: api/Messages
        /// Receive a message from a user and reply to it
        /// </summary>
        public async Task <HttpResponseMessage> Post([FromBody] Activity activity)
        {
            if (activity.Type == ActivityTypes.Message)
            {
                if (!string.IsNullOrEmpty(activity.Text))
                {
                    //detect language of input text
                    var userLanguage = await TranslateService.DetermineLanguageAsync(activity.Text);

                    //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>(AppSettings.UserLanguageKey);

                            //update user's language in Azure Table Storage
                            if (storedLanguageCode != userLanguage)
                            {
                                userData.SetProperty(AppSettings.UserLanguageKey, userLanguage);
                                await botDataStore.SaveAsync(key, BotStoreType.BotUserData, userData, CancellationToken.None);

                                await botDataStore.FlushAsync(key, CancellationToken.None);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    activity.Text = await TranslateService.TranslateTextToDefaultLanguage(activity, userLanguage);

                    await Conversation.SendAsync(activity, MakeRoot);
                }
            }
            else
            {
                await HandleSystemMessage(activity);
            }
            var response = Request.CreateResponse(HttpStatusCode.OK);

            return(response);
        }