Пример #1
0
        public ConversationResponse HandleMessage(Activity activity, ItemContextParameters parameters)
        {
            IConversation conversation = (ConversationHistory.Conversations.Any())
                ? ConversationHistory.Conversations.Last()
                : null;

            var inConversation = conversation != null && !conversation.IsEnded;

            // determine which intent user wants and context
            var result        = LuisService.Query(AppId, activity.Text);
            var requestedQuit =
                result.TopScoringIntent.Intent.ToLower().Equals("quit") &&
                result.TopScoringIntent.Score > 0.4;

            var intent = IntentProvider.GetIntent(AppId, result.TopScoringIntent.Intent);

            // if the user is trying to end or finish a conversation
            if (inConversation && requestedQuit)
            {
                conversation.IsEnded = true;
                return(intent.Respond(null, null, null));
            }

            // start a new conversation if not in one
            if (!inConversation && intent != null && result.TopScoringIntent.Score > 0.4)
            {
                conversation = ConversationFactory.Create(result, intent);
                ConversationHistory.Conversations.Add(conversation);
                inConversation = true;
            }

            if (inConversation)
            {
                // check and request all required parameters of a conversation
                foreach (ConversationParameter p in conversation.Intent.RequiredParameters)
                {
                    if (!TryGetParam(p.ParamName, result, conversation, parameters, p.ParamGetter))
                    {
                        return(RequestParam(p, conversation, parameters));
                    }
                }

                conversation.IsEnded = true;

                return(conversation.Intent.Respond(result, parameters, conversation));
            }

            // determine mood of comment
            var sentiment      = GetSentiment(activity.Text);
            var sentimentScore = (sentiment?.Documents != null && sentiment.Documents.Any())
                ? sentiment.Documents.First().Score
                : 1;

            // is a user frustrated or is their intention unclear
            return((sentimentScore <= 0.4)
                ? IntentProvider.GetIntent(AppId, "frustrated user").Respond(null, null, null)
                : IntentProvider.GetDefaultResponse(AppId));
        }
        public virtual IConversation GetCurrentConversation()
        {
            var intent = IntentProvider.GetTopScoringIntent(this);

            IConversation conversation = null;

            var isConfident    = Result.TopScoringIntent.Score > ApiKeys.LuisIntentConfidenceThreshold;
            var hasValidIntent = intent != null && isConfident && intent.KeyName != "none";

            if (ConversationHistory.Conversations.Any())
            {
                conversation = ConversationHistory.Conversations.Last();
            }

            var inConversation = conversation != null && !conversation.IsEnded;

            if (!inConversation && hasValidIntent)
            {
                conversation = ConversationFactory.Create(Result, intent);
                ConversationHistory.Conversations.Add(conversation);
            }

            return(conversation);
        }