Ejemplo n.º 1
0
        public async Task MessageWithoutMetadataShouldNotExecuteArtificialIntelligenceAnalyse()
        {
            // Arrange
            var requestJson = "{\"dialogState\":\"STARTED\",\"intent\":{\"confirmationStatus\":\"NONE\",\"name\":\"PlanMyTrip\",\"slots\":{\"fromCity\":{\"confirmationStatus\":\"NONE\",\"name\":\"fromCity\"},\"SlotName\":{\"confirmationStatus\":\"NONE\",\"name\":\"string\",\"resolutions\":{\"resolutionsPerAuthority\":[{\"authority\":\"string\",\"status\":{\"code\":\"ER_SUCCESS_MATCH\"},\"values\":[{\"value\":{\"name\":\"string\",\"id\":\"string\"}}]}]},\"value\":\"string\"},\"toCity\":{\"confirmationStatus\":\"NONE\",\"name\":\"toCity\",\"value\":\"Chicago\"},\"travelDate\":{\"confirmationStatus\":\"NONE\",\"name\":\"travelDate\"}}},\"locale\":\"en-US\",\"reason\":\"USER_INITIATED\",\"requestId\":\"amzn1.echo-api.request.1\",\"timestamp\":\"2019-03-23T00:34:14.000Z\",\"type\":\"IntentRequest\"}";
            var json        = JsonConvert.DeserializeObject <Dictionary <string, object> >(requestJson);

            var messageWithAnalyzable = new Message()
            {
                Content = new JsonDocument(json, MediaType.ApplicationJson)
            };

            var jsonInput = new LazyInput(
                messageWithAnalyzable,
                UserIdentity,
                new BuilderConfiguration(),
                new DocumentSerializer(new DocumentTypeResolver()),
                new EnvelopeSerializer(new DocumentTypeResolver()),
                ArtificialIntelligenceExtension,
                CancellationToken);

            // Act
            await jsonInput.AnalyzedContent;

            // Assert
            await ArtificialIntelligenceExtension.Received(0).AnalyzeAsync(
                Arg.Any <AnalysisRequest>(), Arg.Any <CancellationToken>()
                );
        }
Ejemplo n.º 2
0
 public InputVariableProviderTests()
 {
     BuilderConfiguration            = new BuilderConfiguration();
     DocumentSerializer              = new DocumentSerializer(new DocumentTypeResolver().WithMessagingDocuments());
     ArtificialIntelligenceExtension = Substitute.For <IArtificialIntelligenceExtension>();
     ArtificialIntelligenceExtension
     .AnalyzeAsync(Arg.Is <AnalysisRequest>(r => r.Text == NoIntentInputText), Arg.Any <CancellationToken>())
     .Returns(NoIntentResponse);
     ArtificialIntelligenceExtension
     .AnalyzeAsync(Arg.Is <AnalysisRequest>(r => r.Text == IntentInputText), Arg.Any <CancellationToken>())
     .Returns(IntentResponse);
     ArtificialIntelligenceExtension
     .AnalyzeAsync(Arg.Is <AnalysisRequest>(r => r.Text == IntentAndEntityText), Arg.Any <CancellationToken>())
     .Returns(IntentsAndEntitiesResponse);
     ArtificialIntelligenceExtension
     .AnalyzeAsync(Arg.Is <AnalysisRequest>(r => r.Text == MultipleIntentsAndEntitiesText), Arg.Any <CancellationToken>())
     .Returns(MultipleIntentsAndEntitiesResponse);
     ArtificialIntelligenceExtension
     .GetContentResultAsync(Arg.Is <ContentCombination>(cc => cc.Intent == default), Arg.Any <CancellationToken>())
     .Returns(NoContentResult);
     ArtificialIntelligenceExtension
     .GetContentResultAsync(Arg.Is <ContentCombination>(cc => cc.Intent == LearnIntent.Id), Arg.Any <CancellationToken>())
     .Returns(ContentResultWithIntent);
     ArtificialIntelligenceExtension
     .GetContentResultAsync(Arg.Is <ContentCombination>(cc => cc.Intent == IntentAndEntityCombination.Intent &&
                                                        cc.Entities.SequenceEqual(IntentAndEntityCombination.Entities)), Arg.Any <CancellationToken>())
     .Returns(ContentResultWithIntentAndEntity);
     ArtificialIntelligenceExtension
     .GetContentResultAsync(Arg.Is <ContentCombination>(cc => cc.Intent == IntentAndMultipleEntitiesCombination.Intent &&
                                                        cc.Entities.SequenceEqual(IntentAndMultipleEntitiesCombination.Entities)), Arg.Any <CancellationToken>())
     .Returns(ContentResultWithIntentAndMultipleEntities);
 }
 public InputVariableProviderTests()
 {
     BuilderConfiguration            = new BuilderConfiguration();
     DocumentSerializer              = new DocumentSerializer(new DocumentTypeResolver().WithMessagingDocuments());
     ArtificialIntelligenceExtension = Substitute.For <IArtificialIntelligenceExtension>();
     ArtificialIntelligenceExtension.AnalyzeAsync(Arg.Is <AnalysisRequest>(r => r.Text == NoIntentInputText), Arg.Any <CancellationToken>()).Returns(NoIntentResponse);
     ArtificialIntelligenceExtension.AnalyzeAsync(Arg.Is <AnalysisRequest>(r => r.Text == IntentsAndEntitiesText), Arg.Any <CancellationToken>()).Returns(IntentsAndEntitiesResponse);
     ArtificialIntelligenceExtension.AnalyzeAsync(Arg.Is <AnalysisRequest>(r => r.Text == MultipleIntentsAndEntitiesText), Arg.Any <CancellationToken>()).Returns(MultipleIntentsAndEntitiesResponse);
 }
Ejemplo n.º 4
0
        public async Task ReceiveAsync(Message message, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (!message.Content.ToString().ToLower().Contains("blip"))
            {
                await _sender.SendMessageAsync("Mensagem Padrão", message.From, cancellationToken);

                return;
            }

            ArtificialIntelligenceExtension ia = new ArtificialIntelligenceExtension(_sender);
            AnalysisResponse resposta          = await ia.AnalyzeAsync(new AnalysisRequest()
            {
                Text = message.Content.ToString()
            });

            await _sender.SendMessageAsync(resposta.Intentions.First().Answer.Value, message.From, cancellationToken);
        }
Ejemplo n.º 5
0
        public async Task FlowWithoutEntityConditionsShouldChangeStateAndSendMessage()
        {
            // Arrange
            var input = new PlainText()
            {
                Text = "Ping!"
            };
            var messageType    = "text/plain";
            var messageContent = "This is my entity";
            var entityName     = "My entity name";
            var entityValue    = "My entity value";

            var flow = new Flow()
            {
                Id     = Guid.NewGuid().ToString(),
                States = new[]
                {
                    new State
                    {
                        Id      = "root",
                        Root    = true,
                        Input   = new Input(),
                        Outputs = new[]
                        {
                            new Output
                            {
                                StateId    = "my-entity",
                                Conditions = new[]
                                {
                                    new Condition()
                                    {
                                        Source = ValueSource.Entity,
                                        Entity = entityName,
                                        Values = new[]
                                        {
                                            entityValue
                                        }
                                    }
                                }
                            },
                            new Output
                            {
                                StateId = "ping"
                            }
                        }
                    },
                    new State
                    {
                        Id           = "my-entity",
                        InputActions = new[]
                        {
                            new Action
                            {
                                Type     = "SendMessage",
                                Settings = new JObject()
                                {
                                    { "type", messageType },
                                    { "content", messageContent }
                                }
                            }
                        }
                    },
                    new State
                    {
                        Id           = "ping",
                        InputActions = new[]
                        {
                            new Action
                            {
                                Type     = "SendMessage",
                                Settings = new JObject()
                                {
                                    { "type", messageType },
                                    { "content", "This is not supposed to be received..." }
                                }
                            }
                        }
                    }
                }
            };

            ArtificialIntelligenceExtension
            .AnalyzeAsync(Arg.Is <AnalysisRequest>(r => r.Text == input.Text), Arg.Any <CancellationToken>())
            .Returns(new AnalysisResponse()
            {
                Entities = new[]
                {
                    new EntityResponse()
                    {
                        Name  = entityName,
                        Value = entityValue
                    },
                    new EntityResponse()
                    {
                        Name  = "Other entity name",
                        Value = "Other entity value"
                    }
                }
            });

            var target = GetTarget();

            // Act
            await target.ProcessInputAsync(input, User, flow, CancellationToken);

            // Assert
            ContextProvider.Received(1).CreateContext(User, Arg.Is <LazyInput>(i => i.Content == input), flow);
            StateManager.Received(1).SetStateIdAsync(flow.Id, User, "my-entity", Arg.Any <CancellationToken>());
            StateManager.Received(1).DeleteStateIdAsync(flow.Id, User, Arg.Any <CancellationToken>());
            Sender
            .Received(1)
            .SendMessageAsync(
                Arg.Is <Message>(m =>
                                 m.Id != null &&
                                 m.To.ToIdentity().Equals(User) &&
                                 m.Type.ToString().Equals(messageType) &&
                                 m.Content.ToString() == messageContent),
                Arg.Any <CancellationToken>());
        }