Botframework-sdk: C# Using Prompt Dialog.Choice ends up firing twice or auto selects first answer

Created on 19 Oct 2016  路  6Comments  路  Source: microsoft/botframework-sdk

Hi,
Am using the following code.

`public Task StartAsync(IDialogContext context)
{
string WelcomeMsg = "hi";
try
{
var PromptOptions = new string[] { "Yeah", "Nope" };
PromptDialog.Choice(
context,
WelcomePrompt,
PromptOptions,
WelcomeMsg, promptStyle: PromptStyle.Auto
);
}
catch (TooManyAttemptsException ex)
{
if (Controllers.DebugMode.Debug)
{
context.PostAsync("exception" + ex);
}
}
return Task.CompletedTask;
}
private async Task WelcomePrompt(IDialogContext context, IAwaitable result)
{
var Prompt = await result;
string PromptString = Prompt.ToString().ToLower().Trim();
if (PromptString.Length > 0)
{

            PromptString = PromptString.Trim();
            if (PromptString == "yeah")
            {
                NextFunction NF = new NextFunction();
                await NF .StartAsync(context);
            }
            {
                AnotherFunction AF = new AnotherFunction ();
                await AF.Give(context);
            }
        }
    }`

My problem is that the confirm box is shown twice or it will auto select a reply ( the first one) and go to next function!

Is it a bug or coding problem?!

Most helpful comment

In your IDialog.StartAsync implementation, you may want to send some welcome message, then start the choice prompt. Code that sends messages in the StartAsync is really meant for proactive bots (https://docs.botframework.com/en-us/technical-faq/#what-is-the-difference-between-proactive-and-reactive) - reactive bots will often just call "context.Wait(MessageReceived)" in their IDialog.StartAsync implementation.

All 6 comments

In your IDialog.StartAsync implementation, you may want to send some welcome message, then start the choice prompt. Code that sends messages in the StartAsync is really meant for proactive bots (https://docs.botframework.com/en-us/technical-faq/#what-is-the-difference-between-proactive-and-reactive) - reactive bots will often just call "context.Wait(MessageReceived)" in their IDialog.StartAsync implementation.

@willportnoy Thanks for the Help,
Mine is a proactive bot.
Hence how should i recode this structure to that?!

@willportnoy This is my whole code.

Am new to c# hence is confused about this,

What modifications should I do? and is there any stack overflow page for posting this doubts?

`
[Serializable]
public class WelcomeDialogue : IDialog
{
public Task StartAsync(IDialogContext context)
{
string WelcomeMsg = "Hi";
try
{
var PromptOptions = new string[] { "Yay", "Nope" };
PromptDialog.Choice(
context,
WelcomePrompt,
PromptOptions,
WelcomeMsg, promptStyle: PromptStyle.Auto
);
}
catch (TooManyAttemptsException ex)
{
context.PostAsync("exception" + ex);
}
return Task.CompletedTask;

}
private async Task WelcomePrompt(IDialogContext context, IAwaitable<string> result)
{
    var Prompt = await result;
    string PromptString = Prompt.ToString().ToLower().Trim();
    if (PromptString.Length > 0)
    {

        PromptString = PromptString.Trim();
        if (PromptString == "yay")
        {
          NextTask NT = new NextTask();
            await NT.StartAsync(context);
        }
        else
        {
            AnotherTask AT = new AnotherTask();
            await AT.go(context);
        }
    }
}

`

I got the problem! skype had my conversation id which he kept. once an error occurs, he will not destroy it until yu clear the Bot memory.

activity.GetStateClient().BotState.DeleteStateForUser(activity.ChannelId, activity.From.Id);

Used this code to clear it

Good to hear - I'm going to close this issue for now, but feel free to open a new issue for further discussion.

on first attempt it self it's calling ChoiceReceivedAsync method as well.

public async Task StartAsync(IDialogContext context)
{
await context.PostAsync($"I can help you with it.");
PromptDialog.Choice(
context: context,
resume: ChoiceReceivedAsync,
options: (IEnumerable)Enum.GetValues(typeof(RequestChoice)),
prompt: "You want to create Request for Self or someone else :",
promptStyle: PromptStyle.Auto
);
}

    public virtual async Task ChoiceReceivedAsync(IDialogContext context, IAwaitable<RequestChoice> activity)
    {

}

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Arimov picture Arimov  路  3Comments

verdysh picture verdysh  路  3Comments

mattlanham picture mattlanham  路  3Comments

somprabhsharma picture somprabhsharma  路  3Comments

Vigneshramkumar picture Vigneshramkumar  路  3Comments