Example #1
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);
        }