Esempio 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)
        {
            bool          askedForUserName = false;
            string        userName         = "";
            StringBuilder replyMessage     = new StringBuilder();

            if (activity.Type == ActivityTypes.Message)
            {
                StateClient sc       = activity.GetStateClient();
                BotData     userData = await sc.BotState.GetPrivateConversationDataAsync(activity.ChannelId, activity.Conversation.Id, activity.From.Id);

                askedForUserName = userData.GetProperty <bool>("AskedForUserName");
                userName         = userData.GetProperty <string>("UserName") ?? "";
                if (!askedForUserName)
                {
                    replyMessage.Append($"Hi ! I am your friend Pluto. What's your name? ");
                    userData.SetProperty <bool>("AskedForUserName", true);
                }
                else
                {
                    if (userName == "")
                    {
                        replyMessage.Append($"Hello {activity.Text}. How can I help you?");
                        userData.SetProperty <string>("UserName", activity.Text);
                    }
                    else
                    {
                        var model = await manager.GetModelFromLuis(activity.Text);

                        var answer = await manager.ProcessResponse(model);

                        replyMessage.Append(answer);
                    }
                }
                await sc.BotState.SetPrivateConversationDataAsync(activity.ChannelId, activity.Conversation.Id, activity.From.Id, userData);

                //var model = await manager.GetModelFromLuis(activity.Text);
                ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));

                // return our reply to the user
                // var answer =await manager.ProcessResponse(model);
                // replyMessage.Append(answer);
                Activity reply = activity.CreateReply(replyMessage.ToString());
                await connector.Conversations.ReplyToActivityAsync(reply);
            }
            else
            {
                HandleSystemMessage(activity);
            }
            var response = Request.CreateResponse(HttpStatusCode.OK);

            return(response);
        }