private async Task HandleAddQnA(ITurnContext <IEventActivity> turnContext, QnAMakerEndpointEx value, CancellationToken cancellationToken) { var qnA = new Dictionary <string, QnAMakerEndpointEx>(_model.QnAs.Get()); if (qnA.ContainsKey(value.KnowledgeBaseId)) { return; } qnA.Add(value.KnowledgeBaseId, value); await _model.QnAs.Set(turnContext, qnA, cancellationToken); await SendDebug(turnContext, $"{QnAEvent.AddQnA} {value.KnowledgeBaseId}", cancellationToken); }
protected override async Task OnEventAsync(ITurnContext <IEventActivity> turnContext, CancellationToken cancellationToken) { switch (turnContext.Activity.Name) { // QnA case QnAEvent.GetQnA: { await HandleGetQnA(turnContext, cancellationToken); break; } case QnAEvent.EnableQnA: { var value = (turnContext.Activity.Value as JObject).ToObject <QnAMakerEndpointEx>(); var qnA = new Dictionary <string, QnAMakerEndpointEx>(_model.QnAs.Get()); qnA[value.KnowledgeBaseId].Enable = value.Enable; await _model.QnAs.Set(turnContext, qnA, cancellationToken); await SendDebug(turnContext, $"{QnAEvent.EnableQnA} {value.Enable}", cancellationToken); await HandleGetQnA(turnContext, cancellationToken); break; } case QnAEvent.CreateQnA: { var value = (turnContext.Activity.Value as JObject).ToObject <CreateKbDTO>(); var operation = await _knowledgebase.CreateAsync(value, cancellationToken); operation = await WaitForOperation(operation, QnAEvent.CreateQnA, cancellationToken); var id = operation.ResourceLocation.Split('/')[2]; var ex = new QnAMakerEndpointEx { KnowledgeBaseId = id, Host = _qnAMakerEndpoint.Host, EndpointKey = _qnAMakerEndpoint.EndpointKey, Name = value.Name, Enable = false, Sources = new Dictionary <string, Source>(), }; await HandleAddQnA(turnContext, ex, cancellationToken); await HandleGetQnA(turnContext, cancellationToken); break; } case QnAEvent.AddQnA: { var value = (turnContext.Activity.Value as JObject).ToObject <QnAMakerEndpointEx>(); await HandleAddQnA(turnContext, value, cancellationToken); await HandleGetQnA(turnContext, cancellationToken); break; } case QnAEvent.DelQnA: { var value = (turnContext.Activity.Value as JObject).ToObject <QnAMakerEndpoint>(); await _knowledgebase.DeleteAsync(value.KnowledgeBaseId, cancellationToken); var qnA = new Dictionary <string, QnAMakerEndpointEx>(_model.QnAs.Get()); qnA.Remove(value.KnowledgeBaseId); await _model.QnAs.Set(turnContext, qnA, cancellationToken); await SendDebug(turnContext, $"{QnAEvent.DelQnA} {value.KnowledgeBaseId}", cancellationToken); await HandleGetQnA(turnContext, cancellationToken); break; } case QnAEvent.UpdateQnA: { var value = (turnContext.Activity.Value as JObject).ToObject <QnAMakerEndpointEx>(); var operation = await _knowledgebase.UpdateAsync(value.KnowledgeBaseId, new UpdateKbOperationDTO(update : new UpdateKbOperationDTOUpdate(value.Name)), cancellationToken); operation = await WaitForOperation(operation, QnAEvent.UpdateQnA, cancellationToken); await SendDebug(turnContext, $"{QnAEvent.UpdateQnA} {value.Name}", cancellationToken); var qnA = new Dictionary <string, QnAMakerEndpointEx>(_model.QnAs.Get()); qnA[value.KnowledgeBaseId].Name = value.Name; await _model.QnAs.Set(turnContext, qnA, cancellationToken); await HandleGetQnA(turnContext, cancellationToken); break; } // Source // TODO if same, will delete first case QnAEvent.AddSource: { var value = (turnContext.Activity.Value as JObject).ToObject <AddSourceEvent>(); if (value.QnaList != null) { foreach (var qna in value.QnaList) { qna.Source = value.QnaListId; } } // Handle del var thisSource = _model.QnAs.Get()[value.KnowledgeBaseId].Sources; var toDel = new List <string>(); if (!string.IsNullOrEmpty(value.QnaListId) && thisSource.ContainsKey(value.QnaListId)) { toDel.Add(value.QnaListId); } if (value.Urls != null) { foreach (var url in value.Urls) { if (thisSource.ContainsKey(url)) { toDel.Add(url); } } } if (value.Files != null) { foreach (var file in value.Files) { if (thisSource.ContainsKey(file.FileName)) { toDel.Add(file.FileName); } } } if (toDel.Count > 0) { await HandleDelSource(turnContext, new DelSourceEvent { KnowledgeBaseId = value.KnowledgeBaseId, Ids = toDel }, cancellationToken); } var operation = await _knowledgebase.UpdateAsync(value.KnowledgeBaseId, new UpdateKbOperationDTO(value), cancellationToken); operation = await WaitForOperation(operation, QnAEvent.AddSource, cancellationToken); await _knowledgebase.PublishAsync(value.KnowledgeBaseId, cancellationToken); var qnA = new Dictionary <string, QnAMakerEndpointEx>(_model.QnAs.Get()); thisSource = qnA[value.KnowledgeBaseId].Sources; var sb = new StringBuilder(); void AddToSource(string Id, string Description, SourceType type) { var source = new Source { Id = Id, Description = Description, Type = type }; thisSource.Add(source.Id, source); sb.Append(source.Id); sb.Append(' '); } if (!string.IsNullOrEmpty(value.QnaListId)) { AddToSource(value.QnaListId, value.QnaListDescription, SourceType.Editorial); } for (int i = 0; i < value.Urls?.Count; ++i) { AddToSource(value.Urls[i], value.UrlsDescription[i], SourceType.Url); } for (int i = 0; i < value.Files?.Count; ++i) { AddToSource(value.Files[i].FileName, value.FilesDescription[i], SourceType.File); } await _model.QnAs.Set(turnContext, qnA, cancellationToken); await SendDebug(turnContext, $"{QnAEvent.AddSource} {sb.ToString()}", cancellationToken); await HandleGetQnA(turnContext, cancellationToken); break; } case QnAEvent.DelSource: { var value = (turnContext.Activity.Value as JObject).ToObject <DelSourceEvent>(); await HandleDelSource(turnContext, value, cancellationToken); await _knowledgebase.PublishAsync(value.KnowledgeBaseId, cancellationToken); await HandleGetQnA(turnContext, cancellationToken); break; } // Configs case QnAEvent.SetMinScore: { var value = Convert.ToSingle(turnContext.Activity.Value); await _model.MinScore.Set(turnContext, value, cancellationToken); await SendDebug(turnContext, $"SetMinScore {value}", cancellationToken); break; } case QnAEvent.SetResultNumber: { var value = Convert.ToInt32(turnContext.Activity.Value); await _model.ResultNumber.Set(turnContext, value, cancellationToken); await SendDebug(turnContext, $"SetResultNumber {value}", cancellationToken); break; } // Answer Lg case QnAEvent.SetAnswerLg: { var template = turnContext.Activity.Value.ToString(); await _model.AnswerLg.Set(turnContext, template, cancellationToken); await SendDebug(turnContext, $"SetAnswerLg {template}", cancellationToken); break; } case QnAEvent.TestAnswerLg: { var template = turnContext.Activity.Value.ToString(); var engine = new TemplateEngine(); engine.AddText(template, importResolver: ModelPropertyTemplateEngine.importResolverDelegate); var data = new { data = new { debug = _model.Debug.Get(), results = new List <QueryResult>(), indices = new List <int>() } }; var answer = engine.EvaluateTemplate(Answer.Entrance, data); var activity = ActivityFactory.CreateActivity(answer); await turnContext.SendActivityAsync(activity, cancellationToken); data.data.results.Add(new QueryResult { Answer = "Answer 0" }); data.data.indices.Add(0); answer = engine.EvaluateTemplate(Answer.Entrance, data); activity = ActivityFactory.CreateActivity(answer); await turnContext.SendActivityAsync(activity, cancellationToken); var total = _model.ResultNumber.Get(); if (total != 1) { for (int i = 1; i < total; ++i) { data.data.results.Add(new QueryResult { Answer = $"Answer {i}" }); data.data.indices.Add(i); } answer = engine.EvaluateTemplate(Answer.Entrance, data); activity = ActivityFactory.CreateActivity(answer); await turnContext.SendActivityAsync(activity, cancellationToken); } break; } // Others case QnAEvent.SetDebug: { var value = Convert.ToBoolean(turnContext.Activity.Value); await _model.Debug.Set(turnContext, value, cancellationToken); await SendDebug(turnContext, $"SetDebug {value}", cancellationToken); break; } } }