Amintas Lopes Neto
Amintas Lopes Neto

Reputation: 165

Dialog Continuation Issue on Bot Framework V4

I want to start a user dialog right after a welcome message has been displayed in my bot - without any initial user interaction.
Code snippet to accomplish that:

public RootDialogBot(BotServices services, BotAccessors accessors, ILoggerFactory loggerFactory)
        {
            if (loggerFactory == null)
            {
                throw new System.ArgumentNullException(nameof(loggerFactory));
            }

            _logger = loggerFactory.CreateLogger<RootDialogBot>();            
            _accessors = accessors ?? throw new System.ArgumentNullException(nameof(accessors));
            _botServices = services ?? throw new System.ArgumentNullException(nameof(services));

            _studentProfileAccessor = _accessors.UserState.CreateProperty<StudentProfile>("studentProfile");

            if (!_botServices.QnAServices.ContainsKey("QnAMaker"))
            {
                throw new System.ArgumentException($"Invalid configuration. Please check your '.bot' file for a QnA service named QnAMaker'.");
            }
            if (!_botServices.LuisServices.ContainsKey("LUIS"))
            {
                throw new System.ArgumentException($"Invalid configuration. Please check your '.bot' file for a Luis service named LUIS'.");
            }                     
                .Add(new Activity2MainDialog(_accessors.UserState, Activity2MainDialog))
                .Add(new Activity2LedFailToWorkDialog(_accessors.UserState, Activity2LedFailToWorkDialog));            
        }
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
        {
...
if (turnContext.Activity.Type == ActivityTypes.ConversationUpdate)
            {                
                if (turnContext.Activity.MembersAdded != null)
                {
                    // Save the new turn count into the conversation state.
                    await _accessors.UserState.SaveChangesAsync(turnContext, false, cancellationToken);
                    await _accessors.ConversationState.SaveChangesAsync(turnContext, false, cancellationToken);
                    var message = "Welcome!";
                    await SendWelcomeMessageAsync(turnContext, dc, message,cancellationToken);  //Welcome message
                }
            } 
...
}
private static async Task SendWelcomeMessageAsync(ITurnContext turnContext, DialogContext dc,string message, CancellationToken cancellationToken)
        {
            foreach (var member in turnContext.Activity.MembersAdded)
            {
                if (member.Id != turnContext.Activity.Recipient.Id)
                {
                    await turnContext.SendActivityAsync(message, cancellationToken: cancellationToken);
                    await dc.BeginDialogAsync(Activity2MainDialog, "activity2MainDialog", cancellationToken);
                }
            }
        }


The dialog (Activity2MainDialog) works fine until it reaches a return await stepContext.ContinueDialogAsync(cancellationToken); call.
Then it halts.
I believe it has something to do with the conversation state but I still couldn't find a solution for that.
Code snippet of the return await stepContext.ContinueDialogAsync(cancellationToken); call

public class Activity2MainDialog : ComponentDialog
    {
        private static BellaMain BellaMain = new BellaMain();
        private static FTDMain FTDMain = new FTDMain();
        private readonly IStatePropertyAccessor<StudentProfile> _studentProfileAccessor;        
    ...
        public Activity2MainDialog(UserState userState, string dialogMainId)
                : base(dialogMainId)
        {
            InitialDialogId = Id;
            _studentProfileAccessor = userState.CreateProperty<StudentProfile>("studentProfile");

            WaterfallStep[] waterfallSteps = new WaterfallStep[]
            {
                MsgWelcomeStepAsync
        ...                
            };

            // Add named dialogs to the DialogSet. These names are saved in the dialog state.
            AddDialog(new WaterfallDialog(dialogMainId, waterfallSteps));
            AddDialog(new TextPrompt(nameof(TextPrompt)));
            AddDialog(new ChoicePrompt(nameof(ChoicePrompt)));
            AddDialog(new ConfirmPrompt(nameof(ConfirmPrompt)));
        }
        private async Task<DialogTurnResult> MsgWelcomeStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
        await stepContext.Context.SendActivityAsync("**Oi**", "Oi", cancellationToken: cancellationToken);
            return await stepContext.ContinueDialogAsync(cancellationToken);
        }
        private async Task<DialogTurnResult> QuestGoAheadStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            message = "Vamos nessa?";
            await stepContext.Context.SendActivityAsync(message , message , cancellationToken: cancellationToken);
            retryPromptMessage = message;
            return await stepContext.PromptAsync(nameof(ChoicePrompt),
                        new PromptOptions
                        {
                            Prompt = null,
                            RetryPrompt = MessageFactory.Text(retryPromptMessage, retryPromptSpeakMessage), InputHints.ExpectingInput),            
                            Choices = new[]
                            {
                                    new Choice {Value = "Sim", Synonyms = new List<string> {"yes","yeah","esta","ta","esta","ok","vamos","curti","curtir","claro","tá","sei","top"}},
                                    new Choice {Value = "Não", Synonyms = new List<string> {"no"}}
                            }.ToList(),
                            Style = ListStyle.Auto                            
                        });
        }

Thoughts on how to fix it? Thx

Upvotes: 1

Views: 116

Answers (1)

mdrichardson
mdrichardson

Reputation: 7241

I'm fairly certain the issue is the ContinueDialog call. You need to end that step with:

return await stepContext.NextAsync(null, cancellationToken);

See CoreBot for more example code.

If this doesn't fix your issue, let me know and I'll adjust my answer.

Upvotes: 1

Related Questions