Ejemplo n.º 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)
            {
                //to show user that bot is typing
                //var connector = new ConnectorClient(new Uri(activity.ServiceUrl));
                //Activity isTypingReply = activity.CreateReply();
                //isTypingReply.Type = ActivityTypes.Typing;
                //isTypingReply.Text = null;
                //await connector.Conversations.ReplyToActivityAsync(isTypingReply);


                if (!string.IsNullOrEmpty(activity.Text))
                {
                    var userLanguage = TranslationHandler.DetectLanguage(activity);

                    //save user's LanguageCode to Azure 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 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;
                    }

                    activity.Text = TranslationHandler.TranslateTextToDefaultLanguage(activity, userLanguage);
                }
                //await Conversation.SendAsync(activity, MakeRootDialog);
                //await Conversation.SendAsync(activity, () => new RootDialog());
                //await Conversation.SendAsync(activity, () => new Dialogs.SimpleQnADialog());
                await SendAsync(activity, (scope) => new RootDialog(scope.Resolve <IUserToAgent>()));
            }
            else
            {
                await HandleSystemMessage(activity);
            }
            var response = Request.CreateResponse(HttpStatusCode.OK);

            return(response);
        }