public async Task RunBehaviorLogicAsync(string connectionId, string username, string payload, Services.ServiceContext context) { var behaviorContext = await CreateBehaviorContextAsync(connectionId, username, payload, context); var availableSteps = behaviorContext.Iterator.Current(); BehaviorStep currentStep = null; foreach (var step in availableSteps) { if (await CheckStepCondition(behaviorContext, step)) { currentStep = step; break; } } if (currentStep != null) { behaviorContext.Iterator.Next(currentStep?.Id); await RunStepAsync(behaviorContext, _outgoingServicePool.Resolve(context.TransportName), currentStep); } if (!behaviorContext.Iterator.Current().Any()) { behaviorContext.Iterator.Reset(); } }
private async Task <bool> CheckStepCondition(BehaviorContext context, BehaviorStep step) { if (!string.IsNullOrEmpty(step.Condition?.Instruction)) { if (step.Condition.Instruction.Equals(DefaultBehaviorInstructionNone)) { return(true); } if (step.Condition.Instruction.Equals(DefaultBehaviorInstructionNext)) { return(false); } return((await _interpreter.Interpret(step.Condition, context.Dialog, context.Database, context.ServiceContext.TransportName)).GetTypedResult <bool>()); } return(false); }
private async Task RunStepAsync(BehaviorContext context, IOutgoingService outgoingService, BehaviorStep step) { _logger.LogDebug($"Step: {step.Id} is running for client: {context.Client.Id} within bot: {context.ServiceContext.BotId}."); try { if (step.Command != null) { if (step.Command.Type == Enums.BehaviorInstructionType.Instruction && //todo: не учитываются кейсы отсутвия инструкции и системной команды в инструкции !string.IsNullOrEmpty(step.Command?.Instruction) && !step.Command.Instruction.Equals(DefaultBehaviorInstructionNone)) { await HandleInterpretationResult(await _interpreter.Interpret(step.Command, context.Dialog, context.Database, context.ServiceContext.TransportName), context); } } if (!string.IsNullOrEmpty(step.Response)) { var response = await _interpreter.InsertEntityParameters(step.Response, context.Dialog, context.Database); await outgoingService.SendAsync(response, context.Client.Id); } } catch (Exception ex) { _logger.LogError(ex, $"Behavior step failed. StepId: {step.Id}. BotId: {context.ServiceContext.BotId}. ClientId: {context.Client.Id}."); throw; } }
public static bool IsValid(BehaviorStep step) { return(true); }