private async Task <DialogTurnResult> SearchClosingDateStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) { var crmState = await _accessors.CRMStateAccessor.GetAsync(stepContext.Context, () => new CRMState(), cancellationToken); var userState = await _accessors.LoggedUserAccessor.GetAsync(stepContext.Context, () => new LoggedUserState(), cancellationToken); //Gathering the date if not already given if (crmState.Opportunity.EstimatedCloseDate == null || crmState.Opportunity.EstimatedCloseDate == DateTime.MinValue) { var luisResult = await _botServices.LuisServices[CulturedBot.LuisAppName].RecognizeAsync(stepContext.Context, cancellationToken); var entities = luisResult.Entities; string timex = (string)entities["datetime"]?[0]?["timex"]?.First; var formatConvertor = new FormatConvertor(); crmState.Opportunity.EstimatedCloseDate = formatConvertor.TurnTimexToDateTime(timex); } //Asking for retry if necessary var promptMessage = ""; if (crmState.Opportunity.EstimatedCloseDate == null || crmState.Opportunity.EstimatedCloseDate == DateTime.MinValue) { promptMessage = $"{CulturedBot.AdmitNotUnderstanding} {CulturedBot.AskIfWantToSkip}"; } userState.IsEligibleForPotentialSkippingStep = !string.IsNullOrEmpty(promptMessage); if (userState.IsEligibleForPotentialSkippingStep) { await _accessors.LoggedUserAccessor.SetAsync(stepContext.Context, userState, cancellationToken); await _accessors.CRMStateAccessor.SetAsync(stepContext.Context, crmState, cancellationToken); var activityPrompt = MessageFactory.Text(promptMessage); activityPrompt.Locale = CulturedBot.Culture?.Name; var activityRetryPrompt = MessageFactory.Text(CulturedBot.AskYesOrNo); activityRetryPrompt.Locale = CulturedBot.Culture?.Name; var promptOptions = new PromptOptions { Prompt = activityPrompt, RetryPrompt = activityRetryPrompt, }; return(await stepContext.PromptAsync(_retryFetchingClosingDateFromUserPrompt, promptOptions, cancellationToken)); } await _accessors.LoggedUserAccessor.SetAsync(stepContext.Context, userState, cancellationToken); await _accessors.CRMStateAccessor.SetAsync(stepContext.Context, crmState, cancellationToken); return(await stepContext.NextAsync(cancellationToken : cancellationToken)); }
private async Task UpdateDialogStatesAsync(RecognizerResult luisResult, string intentName, ITurnContext turnContext) { if (luisResult.Entities != null && luisResult.Entities.HasValues) { // Get latest States var crmState = await _accessors.CRMStateAccessor.GetAsync(turnContext, () => new CRMState()); var luisState = await _accessors.LuisStateAccessor.GetAsync(turnContext, () => new LuisState()); var entities = luisResult.Entities; // Supported LUIS Entities string[] luisExpectedLeadName = { "leadfullname", "personNameP" }; string luisExpectedCompanyName = "companyName"; string luisExpectedDateTime = "datetime"; string luisExpectedProductTitle = "productTitle"; string luisExpectedConfidenceOpportunity = "confidenceOpportunity"; string luisHintSearchLeadAddress = "searchaddress"; string luisHintSearchLeadCompany = "searchcompany"; string luisHintSearchLeadPhone = "searchphone"; string luisHintSearchLeadEmail = "searchemail"; string luisHintSearchCompanyContact = "searchcontact"; string luisHintSearchContactName = "searchcontactname"; string luisHintSearchNumberOpportunites = "searchnumberopportunities"; string luisHintSearchOpportunites = "searchopportunities"; //Given Data foreach (var name in luisExpectedLeadName) { if (entities[name] != null) { var fullName = (string)entities[name][0]; if (crmState.Lead == null) { crmState.Lead = new Lead(); } crmState.Lead.FullName = fullName; break; } } if (entities[luisExpectedCompanyName] != null) { var companyName = (string)entities[luisExpectedCompanyName][0].First; crmState.Company.Name = companyName; } if (entities[luisExpectedDateTime] != null) { string timex = (string)entities[luisExpectedDateTime]?[0]?["timex"]?.First; var formatConvertor = new FormatConvertor(); crmState.Opportunity.EstimatedCloseDate = formatConvertor.TurnTimexToDateTime(timex); } if (entities[luisExpectedProductTitle] != null) { var productTitle = (string)entities[luisExpectedProductTitle][0]; crmState.Product.Title = productTitle; } if (intentName == ProxiCallIntents.CreateOpportunity) { if (entities[luisExpectedConfidenceOpportunity] != null) { var confidenceOpportunity = (string)entities[luisExpectedConfidenceOpportunity][0].First; crmState.Opportunity.ChangeConfidenceBasedOnName(confidenceOpportunity); } else { crmState.Opportunity.ChangeConfidenceBasedOnName(OpportunityConfidence.Average.Name); } } //Hints if (entities[luisHintSearchLeadAddress] != null) { luisState.AddDetectedEntity(ProxiCallEntities.SEARCH_ADDRESS_ENTITYNAME); } if (entities[luisHintSearchLeadCompany] != null) { luisState.AddDetectedEntity(ProxiCallEntities.SEARCH_COMPANY_ENTITYNAME); } if (entities[luisHintSearchLeadPhone] != null) { luisState.AddDetectedEntity(ProxiCallEntities.SEARCH_PHONENUMBER_ENTITYNAME); } if (entities[luisHintSearchLeadEmail] != null) { luisState.AddDetectedEntity(ProxiCallEntities.SEARCH_EMAIL_ENTITYNAME); } if (entities[luisHintSearchCompanyContact] != null) { luisState.AddDetectedEntity(ProxiCallEntities.SEARCH_CONTACT_ENTITYNAME); } if (entities[luisHintSearchContactName] != null) { luisState.AddDetectedEntity(ProxiCallEntities.SEARCH_CONTACT_NAME_ENTITYNAME); } if (entities[luisHintSearchOpportunites] != null) { luisState.AddDetectedEntity(ProxiCallEntities.SEARCH_OPPORTUNITIES_NAME_ENTITYNAME); } if (entities[luisHintSearchNumberOpportunites] != null) { luisState.AddDetectedEntity(ProxiCallEntities.SEARCH_NUMBER_OPPORTUNITIES_ENTITYNAME); } //Searching for "informations" about leads var searchInformationsOnLead = intentName == ProxiCallIntents.SearchLeadData && (luisState.Entities == null || luisState.Entities.Count == 0); var searchInformationsOnCompany = intentName == ProxiCallIntents.SearchCompanyData && (luisState.Entities == null || luisState.Entities.Count == 0); var searchInformationsOnContactLead = intentName == ProxiCallIntents.SearchCompanyData && luisState.Entities != null && luisState.Entities.Contains(ProxiCallEntities.SEARCH_CONTACT_ENTITYNAME) && luisState.Entities.Count == 1; if (searchInformationsOnLead) { luisState.AddDetectedEntity(ProxiCallEntities.SEARCH_COMPANY_ENTITYNAME); luisState.AddDetectedEntity(ProxiCallEntities.SEARCH_NUMBER_OPPORTUNITIES_ENTITYNAME); } if (searchInformationsOnCompany) { luisState.AddDetectedEntity(ProxiCallEntities.SEARCH_NUMBER_OPPORTUNITIES_ENTITYNAME); } if (searchInformationsOnContactLead) { luisState.AddDetectedEntity(ProxiCallEntities.SEARCH_NUMBER_OPPORTUNITIES_ENTITYNAME); } // Set the new values into state. luisState.IntentName = intentName; await _accessors.CRMStateAccessor.SetAsync(turnContext, crmState); await _accessors.LuisStateAccessor.SetAsync(turnContext, luisState); } }