Example #1
0
        /// <summary>
        /// Every conversation turn for our Echo Bot will call this method.
        /// There are no dialogs used, since it's "single turn" processing, meaning a single
        /// request and response.
        /// </summary>
        /// <param name="turnContext">A <see cref="ITurnContext"/> containing all the data needed
        /// for processing this conversation turn. </param>
        /// <param name="cancellationToken">(Optional) A <see cref="CancellationToken"/> that can be used by other objects
        /// or threads to receive notice of cancellation.</param>
        /// <returns>A <see cref="Task"/> that represents the work queued to execute.</returns>
        /// <seealso cref="BotStateSet"/>
        /// <seealso cref="ConversationState"/>
        /// <seealso cref="IMiddleware"/>
        public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
        {
            // Create a dialog context
            var dc = await _dialogs.CreateContextAsync(turnContext);


            // Handle Message activity type, which is the main activity type for shown within a conversational interface
            // Message activities may contain text, speech, interactive cards, and binary or unknown attachments.
            // see https://aka.ms/about-bot-activity-message to learn more about the message and other activity types
            if (turnContext.Activity.Type == ActivityTypes.Message)
            {
                // Continue the current dialog
                var dialogResult = await dc.ContinueDialogAsync(cancellationToken);

                if (!turnContext.Responded)
                {
                    switch (dialogResult.Status)
                    {
                    case DialogTurnStatus.Empty:
                        // Your code goes here
                        // Check LUIS model
                        var luisResults = await _luis.RecognizeAsync(dc.Context, cancellationToken).ConfigureAwait(false);

                        var topIntent          = luisResults?.GetTopScoringIntent();
                        var audioAttachmentUrl = BotUtils.GetAudioUploadUrl(turnContext.Activity);
                        if (!string.IsNullOrEmpty(audioAttachmentUrl))
                        {
                            turnContext.Activity.Text = await SpeechService.RecognizeAudioAsync(turnContext.Activity.Attachments[0].ContentUrl);
                        }

                        switch (topIntent?.intent)
                        {
                        case "IWantANewWatch":
                            await turnContext.SendActivityAsync("I can help you with that! Let me see what I can find");
                            await DisplayDefaultWatchesAsync(turnContext);

                            await dc.BeginDialogAsync(nameof(IWantANewWatchDialog));

                            break;

                        case "TryWatch":
                            await dc.BeginDialogAsync(nameof(TryWatchDialog));

                            break;

                        case "Greetings":
                            await DisplayWelcomeAudioMessageAsync(turnContext, cancellationToken);

                            break;

                        case "TellMeWhatToWear":
                            await dc.BeginDialogAsync(nameof(TellMeWhatToWearDialog));

                            break;

                        default:
                            await turnContext.SendActivityAsync("Sorry, I didn't understand that.");

                            break;
                        }

                        break;

                    case DialogTurnStatus.Waiting:
                        // The active dialog is waiting for a response from the user, so do nothing.
                        break;

                    case DialogTurnStatus.Complete:
                        await dc.EndDialogAsync();

                        break;

                    default:
                        await dc.CancelAllDialogsAsync();

                        break;
                    }
                }
            }
            else
            {
                // Add welcome message here
                if (turnContext.Activity.Type == ActivityTypes.ConversationUpdate && turnContext.Activity.MembersAdded.FirstOrDefault()?.Id == turnContext.Activity.Recipient.Id)
                {
                    await turnContext.SendActivityAsync("Hi! How I can help you today?");
                }
            }

            // Save the dialog state into the conversation state.
            await _accessors.ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);

            // Save the user profile updates into the user state.
            await _accessors.UserState.SaveChangesAsync(turnContext, false, cancellationToken);
        }