Exemplo n.º 1
0
        public async Task <DialogTurnResult> PhraseStepAsync(WaterfallStepContext stepContext,
                                                             CancellationToken cancellationToken)
        {
            var state = await Accessors.CustomerDataState.GetAsync(stepContext.Context, null, cancellationToken);

            var phrase = await VoiceVerifier.GetEnrollmentPhraseAsync();

            var profileEnrollments = await VoiceVerifier.GetNumberOfEnrollments(state.UnAuthorizedCustomer);

            if (profileEnrollments >= 3)
            {
                return(await stepContext.PromptAsync(VoicePrompt, new PromptOptions
                {
                    Prompt = await Chatter.GetActivityAsIsAsync(StaticTexts.UploadVerificationVoiceText(phrase)),
                    RetryPrompt = await Chatter.GetActivityAsIsAsync(StaticTexts.VoiceNotAuthenticText(phrase))
                }, cancellationToken));
            }

            await Chatter.SendMessageAsync(stepContext.Context, StaticTexts.UploadVoicesIntroText);

            return(await stepContext.PromptAsync(PhrasePrompt, new PromptOptions
            {
                Prompt = await Chatter.GetActivityAsIsAsync(
                    StaticTexts.UploadVoiceText(phrase, profileEnrollments + 1))
            }, cancellationToken));
        }
Exemplo n.º 2
0
        public async Task <DialogTurnResult> ChooseQuantityStepAsync(WaterfallStepContext stepContext,
                                                                     CancellationToken cancellationToken)
        {
            var foundChoice = (FoundChoice)stepContext.Result;
            var state       = await Accessors.CustomerDataState.GetAsync(stepContext.Context,
                                                                         cancellationToken : cancellationToken);

            if (int.TryParse(foundChoice.Value, out int index))
            {
                state.TempItem = state.Items[index - 1];
            }
            else
            {
                state.TempItem = state.Items.First(dto => dto.DisplayName == foundChoice.Value);
            }

            await stepContext.Context.SendActivityAsync(
                MessageFactory.Attachment(StaticTexts.CreateItemAttachment(state.TempItem)), cancellationToken);

            await Chatter.SendMessageAsync(stepContext.Context, StaticTexts.ItemViewedText);

            await Accessors.CustomerDataState.SetAsync(stepContext.Context, state, cancellationToken);

            return(await stepContext.PromptAsync(QuantityPrompt, new PromptOptions
            {
                Prompt = await Chatter.GetActivityAsync(stepContext.Context, StaticTexts.EnterQuantityText),
                RetryPrompt = await Chatter.GetActivityAsync(stepContext.Context, StaticTexts.EnterQuantityAgainText)
            }, cancellationToken));
        }
Exemplo n.º 3
0
        public async Task <DialogTurnResult> ChooseOrderStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            var state = await Accessors.CustomerDataState.GetAsync(stepContext.Context, null, cancellationToken);

            if (state.SalesOrders == null)
            {
                var orders = await Client.GetSalesOrdersOfCustomer(state.Customer.Number);

                state.SalesOrders = ConvertDictionary(orders);
                var attachments = new List <Attachment>();
                foreach (var order in orders)
                {
                    attachments.Add(StaticTexts.CreatePostedSalesOrderAttachment(order.Key, order.Value, state.Customer));
                }

                await Accessors.CustomerDataState.SetAsync(stepContext.Context, state, cancellationToken);

                await stepContext.Context.SendActivityAsync(MessageFactory.Carousel(attachments), cancellationToken);

                await Chatter.SendMessageAsync(stepContext.Context, StaticTexts.ViewedOrdersText);
            }
            return(await stepContext.PromptAsync(OrdersPrompt, new PromptOptions
            {
                Prompt = await Chatter.GetActivityAsync(stepContext.Context, StaticTexts.EnterOrderNumberText),
                RetryPrompt = await Chatter.GetActivityAsync(stepContext.Context, StaticTexts.EnterOrderNumberAgainText)
            }, cancellationToken : cancellationToken));
        }
Exemplo n.º 4
0
        public async Task <bool> VoiceAuthenticatorAsync(PromptValidatorContext <IList <Attachment> > promptContext,
                                                         CancellationToken cancellationToken)
        {
            var state = await Accessors.CustomerDataState.GetAsync(promptContext.Context, null, cancellationToken);

            var phrase = await VoiceVerifier.GetEnrollmentPhraseAsync();

            if (promptContext.Recognized.Succeeded)
            {
                var authorized = await VoiceVerifier.AuthorizeCustomerAsync(state.UnAuthorizedCustomer,
                                                                            promptContext.Recognized.Value[0].ContentUrl);

                if (authorized)
                {
                    state.Customer = state.UnAuthorizedCustomer;
                    await Accessors.CustomerDataState.SetAsync(promptContext.Context, state, cancellationToken);
                }
                else
                {
                    await Chatter.SendMessageAsIsAsync(promptContext.Context,
                                                       StaticTexts.VoiceNotAuthenticText(phrase));
                }

                return(authorized);
            }
            await Chatter.SendMessageAsIsAsync(promptContext.Context,
                                               StaticTexts.VoiceNotAuthenticText(phrase));

            return(false);
        }
Exemplo n.º 5
0
        private async Task ResetCustomerData(ITurnContext turnContext, CancellationToken cancellationToken,
                                             bool isOrdering)
        {
            var state = await Accessors.CustomerDataState.GetAsync(turnContext, cancellationToken : cancellationToken);

            state = StaticTexts.SetCustomerData(isOrdering, state.ChoosenLanguage, state.BotWelcomedUser,
                                                state.Customer, null, false);
            await Accessors.CustomerDataState.SetAsync(turnContext, state, cancellationToken);
        }
Exemplo n.º 6
0
        public async Task <DialogTurnResult> PostSalesOrderAndViewStepAsync(WaterfallStepContext stepContext,
                                                                            CancellationToken cancellationToken)
        {
            var result = (bool)stepContext.Result;
            var state  = await Accessors.CustomerDataState.GetAsync(stepContext.Context,
                                                                    cancellationToken : cancellationToken);

            var lines = state.TempSalesOrderLines;

            if (!result)
            {
                await Chatter.SendMessageAsync(stepContext.Context, StaticTexts.OrderCanceledText);
            }
            else
            {
                var postSalesOrderDto = new PostSalesOrderDto
                {
                    customerNumber = state.Customer.Number,
                    currencyCode   = "USD"
                };
                var getOrderDto = await Client.PostSalesOrder(postSalesOrderDto);

                var linesDtos = new List <GetSalesOrderLineDto>();
                foreach (var line in lines)
                {
                    var postSalesOrderLineDto = new PostSalesOrderLineDto
                    {
                        itemId   = line.Item.Id,
                        quantity = line.Quantity,
                        lineType = "Item"
                    };
                    var returnedDto = await Client.PostSalesOrderLine(getOrderDto.Id, postSalesOrderLineDto);

                    linesDtos.Add(returnedDto);
                }

                var finalOrder = await Client.GetSalesOrder(getOrderDto.Id);

                await stepContext.Context.SendActivityAsync(
                    MessageFactory.Attachment(
                        StaticTexts.CreatePostedSalesOrderAttachment(finalOrder, linesDtos, state.Customer)),
                    cancellationToken : cancellationToken);

                await Chatter.SendMessageAsync(stepContext.Context, StaticTexts.OrderPostedText);
            }
            await Accessors.CustomerDataState.SetAsync(stepContext.Context, state, cancellationToken);

            await Chatter.SendMessageAsync(stepContext.Context, StaticTexts.AnythingElseText);

            return(await stepContext.EndDialogAsync(cancellationToken : cancellationToken));
        }
Exemplo n.º 7
0
        public async Task <DialogTurnResult> ConfirmProfileStepAsync(WaterfallStepContext stepContext,
                                                                     CancellationToken cancellationToken)
        {
            var data = await Accessors.CustomerDataState.GetAsync(stepContext.Context,
                                                                  cancellationToken : cancellationToken);

            var attachment = StaticTexts.CreateCustomerAttachment(data.Customer);
            await stepContext.Context.SendActivityAsync(MessageFactory.Attachment(attachment), cancellationToken);

            return(await stepContext.PromptAsync(ConfirmIdPrompt, new PromptOptions
            {
                Prompt = await Chatter.GetActivityAsync(stepContext.Context, StaticTexts.ConfirmProfileText),
                RetryPrompt = await Chatter.GetActivityAsync(stepContext.Context, StaticTexts.ConfirmProfileText)
            }, cancellationToken));
        }
Exemplo n.º 8
0
        public async Task <DialogTurnResult> ConfirmSalesOrderStepAsync(WaterfallStepContext stepContext,
                                                                        CancellationToken cancellationToken)
        {
            var state = await Accessors.CustomerDataState.GetAsync(stepContext.Context,
                                                                   cancellationToken : cancellationToken);

            var attachment = StaticTexts.CreateSalesOrderAttachment(state.TempSalesOrderLines);
            await stepContext.Context.SendActivityAsync(MessageFactory.Attachment(attachment), cancellationToken);

            await Chatter.SendMessageAsync(stepContext.Context, StaticTexts.TempOrderViewedText);

            return(await stepContext.PromptAsync(ConfirmSalesOrderPrompt, new PromptOptions
            {
                Prompt = await Chatter.GetActivityAsync(stepContext.Context, StaticTexts.ConfirmSalesOrderText),
                RetryPrompt = await Chatter.GetActivityAsync(stepContext.Context, StaticTexts.ConfirmSalesOrderText)
            }, cancellationToken));
        }
Exemplo n.º 9
0
        public async Task <DialogTurnResult> ConfirmQuantityStepAsync(WaterfallStepContext stepContext,
                                                                      CancellationToken cancellationToken)
        {
            var state = await Accessors.CustomerDataState.GetAsync(stepContext.Context,
                                                                   cancellationToken : cancellationToken);

            state.TempQuantity = (int)stepContext.Result;
            var prompt = await Chatter.GetActivityAsync(stepContext.Context,
                                                        StaticTexts.ConfirmQuantityText(state.TempQuantity, state.TempItem.DisplayName));

            await Accessors.CustomerDataState.SetAsync(stepContext.Context, state, cancellationToken);

            return(await stepContext.PromptAsync(ConfirmQuantityPrompt, new PromptOptions
            {
                Prompt = prompt,
                RetryPrompt = prompt
            }, cancellationToken));
        }
Exemplo n.º 10
0
        public async Task <DialogTurnResult> ChooseActionStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            var state = await Accessors.CustomerDataState.GetAsync(stepContext.Context, null, cancellationToken);

            var choosenOrder = await Client.GetSalesOrderByNumber(state.ChoosenOrderNumber);

            var attachment = StaticTexts.CreatePostedSalesOrderAttachment(choosenOrder,
                                                                          state.SalesOrders[state.ChoosenOrderNumber], state.Customer);
            await stepContext.Context.SendActivityAsync(MessageFactory.Attachment(attachment), cancellationToken);

            await Chatter.SendMessageAsync(stepContext.Context, StaticTexts.YourSelectedOrderText);

            return(await stepContext.PromptAsync(ActionPrompt, new PromptOptions
            {
                Prompt = await Chatter.GetActivityAsync(stepContext.Context, StaticTexts.OrderActionText),
                RetryPrompt = await Chatter.GetActivityAsync(stepContext.Context, StaticTexts.OrderActionText),
                Choices = ChoiceFactory.ToChoices(new List <string> {
                    "Delete", "Done"
                })
            }, cancellationToken));
        }
Exemplo n.º 11
0
        public async Task <bool> VoiceValidatorAsync(PromptValidatorContext <IList <Attachment> > promptContext,
                                                     CancellationToken cancellationToken)
        {
            var state = await Accessors.CustomerDataState.GetAsync(promptContext.Context, null, cancellationToken);

            var phrase = await VoiceVerifier.GetEnrollmentPhraseAsync();

            var profileEnrollments = await VoiceVerifier.GetNumberOfEnrollments(state.UnAuthorizedCustomer);

            try
            {
                if (promptContext.Recognized.Succeeded)
                {
                    if (!await VoiceVerifier.CheckIfCustomerHasProfileAsync(state.UnAuthorizedCustomer))
                    {
                        await VoiceVerifier.AddCustomerProfileAsync(state.UnAuthorizedCustomer);
                    }

                    await VoiceVerifier.AddCustomerEnrollmentAsync(state.UnAuthorizedCustomer,
                                                                   promptContext.Recognized.Value[0].ContentUrl);

                    return(true);
                }

                await Chatter.SendMessageAsIsAsync(promptContext.Context,
                                                   StaticTexts.UploadVoiceText(phrase, profileEnrollments + 1));

                return(false);
            }
            catch (Exception)
            {
                await Chatter.SendMessageAsIsAsync(promptContext.Context,
                                                   StaticTexts.UploadVoiceText(phrase, profileEnrollments + 1));

                return(false);
            }
        }
Exemplo n.º 12
0
        public async Task OnTurnAsync(ITurnContext turnContext,
                                      CancellationToken cancellationToken = default(CancellationToken))
        {
            var state = await Accessors.CustomerDataState.GetAsync(turnContext,
                                                                   () => StaticTexts.SetCustomerData(false, null, false, null, null, false),
                                                                   cancellationToken : cancellationToken);

            if (turnContext.Activity.Type == ActivityTypes.Message)
            {
                if (turnContext.Activity.Attachments != null)
                {
                    var url         = turnContext.Activity.Attachments[0].ContentUrl;
                    var transcribed = await SpeechRecognizer.RecognizeSpeech(url);

                    if (transcribed != "")
                    {
                        await Chatter.SendMessageAsIsAsync(turnContext, "You said:" + transcribed);
                    }
                    turnContext.Activity.Text = transcribed;
                }

                var text = await Chatter.GetInputAsync(turnContext);

                var intention =
                    await NaturalLanguageEngine.RecognizeIntention(text, cancellationToken);

                bool ordering = state.isOrdering;
                bool viewing  = state.isViewing;


                if (!state.BotWelcomedUser || state.ChoosenLanguage == null)
                {
                    await LanguageDialogSet.StartOrContinueDialogAsync(turnContext, cancellationToken);

                    state.BotWelcomedUser = true;
                    await Accessors.CustomerDataState.SetAsync(turnContext, state, cancellationToken);
                }
                else if (intention == StaticTexts.StartOverIntention)
                {
                    await EndAllDialogsAsync(turnContext, cancellationToken);

                    await Chatter.SendMessageAsync(turnContext, StaticTexts.CancelText);

                    await Chatter.SendMessageAsync(turnContext, StaticTexts.AnythingElseText);
                }
                else if (intention == StaticTexts.HelpIntention)
                {
                    await Chatter.SendMessageAsync(turnContext, StaticTexts.HelpText);
                }
                else if (intention == StaticTexts.ChangeLanguageIntention)
                {
                    await EndAllDialogsAsync(turnContext, cancellationToken);

                    state.ChoosenLanguage = null;
                    await Accessors.CustomerDataState.SetAsync(turnContext, state, cancellationToken);

                    await LanguageDialogSet.StartOrContinueDialogAsync(turnContext, cancellationToken);
                }
                else if (ordering || (intention == StaticTexts.StartOrderIntention && !viewing))
                {
                    await SalesDialogSet.StartOrContinueDialogAsync(turnContext, cancellationToken);
                }
                else if (viewing || intention == StaticTexts.ViewOrdersIntention)
                {
                    await ViewOrdersDialogSet.StartOrContinueDialogAsync(turnContext, cancellationToken);
                }
                else if (intention == StaticTexts.GreetingIntention)
                {
                    await Chatter.SendMessageAsync(turnContext, StaticTexts.GreetingText);
                }
                else if (intention == StaticTexts.JokeIntention)
                {
                    await Chatter.SendMessageAsync(turnContext, StaticTexts.JokeText);
                }
                else
                {
                    await Chatter.SendMessageAsync(turnContext, StaticTexts.NoneText);
                }
            }
            else if (turnContext.Activity.Type == ActivityTypes.ConversationUpdate)
            {
                if (turnContext.Activity.MembersAdded.Any())
                {
                    if (turnContext.Activity.MembersAdded[0].Name == "User")
                    {
                        if (!state.BotWelcomedUser)
                        {
                            await LanguageDialogSet.StartOrContinueDialogAsync(turnContext, cancellationToken);

                            state.BotWelcomedUser = true;
                            await Accessors.CustomerDataState.SetAsync(turnContext, state, cancellationToken);
                        }
                    }
                }
            }
            await Accessors.ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);
        }