Beispiel #1
0
        async protected override Task NextAction(IDialogContext context, IAwaitable <IMessageActivity> message, LuisResult result)
        {
            System.Diagnostics.Trace.TraceInformation($"Luis query: {result.Query}. Entities: [{string.Join(",",result.Entities.Select(e=> $"{{{e.Type}, {e.Entity}}}").ToArray())}]");
            switch (_state)
            {
            case StateMachine.Initial:
                _state = StateMachine.InitialResponse;

                _model = new M.Model();

                goto case StateMachine.InitialResponse;

            case StateMachine.InitialResponse:
                if (I.AnimalTransportation != result.TopScoringIntent.Intent)
                {
                    await context.PostAsync($"Извините, но я не могу понять Ваш вопрос.");

                    context.Done(null as Object);
                    return;
                }

                await IntentAnimalTransportation(context, result);

                goto case StateMachine.Weight;

            case StateMachine.Weight:
                if (true == _model.IsWeightValid)
                {
                    goto case StateMachine.Dimension;
                }

                _state = StateMachine.WeightResponse;

                await context.PostAsync($"Укажите вес животного:");

                //PromptDialog.Number(context, this.PromptResponseWeight, "Укажите вес животного в килограммах");

                //goto case StateMachine.Weight;

                context.Wait(this.ResponseMessageReceived);
                break;

            case StateMachine.WeightResponse:
                //if (I.Weight != result.TopScoringIntent.Intent) { await CantUnderstand(context); }
                //else {
                await IntentWeight(context, result);

                //}

                goto case StateMachine.Weight;

            case StateMachine.Dimension:
                if (true == _model.IsDimensionValid)
                {
                    goto case StateMachine.TravelType;
                }

                _state = StateMachine.DimensionResponse;

                await context.PostAsync($"Укажите размеры клетки (длина, ширина и высота в сантиметрах):");

                context.Wait(this.ResponseTranslateWhenIntent);
                break;

            case StateMachine.DimensionResponse:
                //if (I.Dimension != result.TopScoringIntent.Intent) { await CantUnderstand(context); }
                //else {
                await IntentDimension(context, result);

                //}

                goto case StateMachine.Dimension;

            case StateMachine.TravelType:
                if (true == _model.IsTravelTypeValid)
                {
                    goto case StateMachine.LocationFrom;
                }

                _state = StateMachine.TravelTypeResponse;

                {
                    var m = context.MakeMessage();
                    m.Attachments.Add(
                        new ThumbnailCard(text: $"Где Вы желаете везти своё животное ?",
                                          buttons: new []
                    {
                        new CardAction(ActionTypes.ImBack, title: TravelTypeCabin, value: TravelTypeCabin),
                        new CardAction(ActionTypes.ImBack, title: TravelTypeCheckedIn, value: TravelTypeCheckedIn),
                    }).ToAttachment());
                    await context.PostAsync(m);
                }
                context.Wait(this.ResponseMessageReceived);
                break;

            case StateMachine.TravelTypeResponse:
            {
                var m = context.Activity?.AsMessageActivity()?.Text;
                if (TravelTypeCabin.Equals(m, StringComparison.InvariantCultureIgnoreCase))
                {
                    _model.TravelType = M.TravelType.Cabin;
                }
                else if (TravelTypeCheckedIn.Equals(m, StringComparison.InvariantCultureIgnoreCase))
                {
                    _model.TravelType = M.TravelType.CheckedIn;
                }
                else
                {
                    await CantUnderstand(context);
                }
            }

                goto case StateMachine.TravelType;

            case StateMachine.LocationFrom:
                if (true == _model.IsLocationFromValid)
                {
                    goto case StateMachine.LocationTo;
                }

                _state = StateMachine.LocationFromResponse;

                await context.PostAsync($"Откуда Вы вылетаете ?");

                context.Wait(this.ResponseTranslateWhenIntent);
                break;

            case StateMachine.LocationFromResponse:
            {
                //var n = await LuisHelpers.ProcessLocation(result.ScoreEntitiesGeography());
                //if (true == n.isProcessed) { _model.LocationFrom = n.geo?.normalized; }
                await ProcessGeoFromPredictive(new EntityRecommendation[] { new EntityRecommendation("PlaceName", entity: result.Query) }, _model);

                if (!_model.IsLocationFromValid)
                {
                    await CantUnderstand(context);
                }
            }

                goto case StateMachine.LocationFrom;

            case StateMachine.LocationTo:
                if (true == _model.IsLocationToValid)
                {
                    goto case StateMachine.Validation;
                }

                _state = StateMachine.LocationToResponse;

                await context.PostAsync($"Куда летите ?");

                context.Wait(this.ResponseTranslateWhenIntent);
                break;

            case StateMachine.LocationToResponse:
            {
                //var n = await LuisHelpers.ProcessLocation(result.ScoreEntitiesGeography());
                //if (true == n.isProcessed) { _model.LocationTo = n.geo?.normalized; }
                await ProcessGeoToPredictive(new EntityRecommendation[] { new EntityRecommendation("DestinationPlaceName", entity: result.Query) }, _model);

                if (!_model.IsLocationToValid)
                {
                    await CantUnderstand(context);
                }
            }

                goto case StateMachine.LocationTo;

            case StateMachine.Validation:
                _state = StateMachine.ValidationResponse;
                {
                    var card = new AdaptiveCards.AdaptiveCard()
                    {
                        Body = new List <AdaptiveCards.CardElement>()
                        {
                            new AdaptiveCards.TextBlock()
                            {
                                Text     = $"Проверьте, пожалуйста, правильно ли я всё понял:",
                                Size     = AdaptiveCards.TextSize.Medium,
                                Weight   = AdaptiveCards.TextWeight.Bolder,
                                IsSubtle = false
                            },
                            new AdaptiveCards.ColumnSet()
                            {
                                Separation = AdaptiveCards.SeparationStyle.Strong,
                                Columns    = new List <AdaptiveCards.Column>()
                                {
                                    new AdaptiveCards.Column()
                                    {
                                        Size  = "1",
                                        Items = new List <AdaptiveCards.CardElement>()
                                        {
                                            new AdaptiveCards.TextBlock()
                                            {
                                                Text  = _model.LocationFrom,
                                                Size  = AdaptiveCards.TextSize.ExtraLarge,
                                                Color = AdaptiveCards.TextColor.Accent
                                            }
                                        }
                                    },
                                    new AdaptiveCards.Column()
                                    {
                                        Size  = "auto",
                                        Items = new List <AdaptiveCards.CardElement>()
                                        {
                                            new AdaptiveCards.Image()
                                            {
                                                Url  = "http://messagecardplayground.azurewebsites.net/assets/airplane.png",
                                                Size = AdaptiveCards.ImageSize.Small
                                            }
                                        }
                                    },
                                    new AdaptiveCards.Column()
                                    {
                                        Size  = "1",
                                        Items = new List <AdaptiveCards.CardElement>()
                                        {
                                            new AdaptiveCards.TextBlock()
                                            {
                                                Text  = _model.LocationTo,
                                                Size  = AdaptiveCards.TextSize.ExtraLarge,
                                                Color = AdaptiveCards.TextColor.Accent,
                                                HorizontalAlignment = AdaptiveCards.HorizontalAlignment.Right
                                            }
                                        }
                                    }
                                }
                            }
                        },
                        Actions = new List <AdaptiveCards.ActionBase>()
                        {
                            new AdaptiveCards.SubmitAction()
                            {
                                Title = ValidationOk, Data = ValidationOk
                            },
                            new AdaptiveCards.SubmitAction()
                            {
                                Title = ValidationError, Data = ValidationError
                            }
                        }
                    };

                    var m = context.MakeMessage();

                    //m.Attachments.Add(new Attachment()
                    //{
                    //    ContentType = AdaptiveCards.AdaptiveCard.ContentType,
                    //    Content = card
                    //});

                    m.Attachments.Add(
                        new ReceiptCard(title: $"Проверьте, пожалуйста, правильно ли я всё понял:",
                                        facts: FactsFromModel(_model).ToArray(),
                                        buttons: new[]
                    {
                        new CardAction(ActionTypes.ImBack, title: ValidationOk, value: ValidationOk),
                        new CardAction(ActionTypes.ImBack, title: ValidationError, value: ValidationError),
                    },
                                        total: "---" // HACK: stupid Windows Desktop Skype don't show Receipt Card without total field
                                        ).ToAttachment()
                        );
                    await context.PostAsync(m);
                }
                context.Wait(this.ResponseMessageReceived);
                break;

            case StateMachine.ValidationResponse:
            {
                var m = context.Activity?.AsMessageActivity()?.Text;
                if (true == ValidationOk.Equals(m, StringComparison.InvariantCultureIgnoreCase))
                {
                    _model.IsValid = true;
                }
                else if (true == ValidationError.Equals(m, StringComparison.InvariantCultureIgnoreCase))
                {
                    await context.PostAsync($"Хорошо. Давайте попробуем ещё раз.");

                    _model = new M.Model();
                    goto case StateMachine.Weight;
                }
                else
                {
                    await CantUnderstand(context);

                    goto case StateMachine.Validation;
                }
            }

                goto case StateMachine.Completed;

            case StateMachine.Completed:
                await context.PostAsync($"Стоимость провоза составит {_rnd.Next(3, 9) * 1000} рублей");

                context.Done(null as Object);
                return;

            default:
                throw new IllegalStateException("NextAction");
            }

            if (true == _isDebug)
            {
                await LogModel(context);
            }
        }