Krumelur Krumelur - 2 years ago 139
C# Question

Incorrect message processing of PromptDialog.Choice()?

I noticed that using

PromptDialog.Choice()
to begin a conversation proactively is not working as expected and wonder if I'm doing something wrong or if it is a problem in the bot framework.

I'm using the prompt in my root dialog like this:

public async Task StartAsync(IDialogContext context)
{
PromptDialog.Choice(...);
}


The problem is that for the root dialog multiple messages arrive. There are at least two "conversation updated" messages, one for adding the bot, another for adding the user.

The prompt dialog interprets those as answers and of course they don't match the available prompt choices. So the prompt dialog does one more attempt and shows itself again. As a result, I always get multiple prompts on conversation start.

I tracked it down to the message handler in the dialog and saw that it does not check the message type. As a workaround I have created my own prompt dialog which ignores all non-message type activities and now it is working as expected:

[Serializable]
public class CustomPromptDialog : PromptChoice<string>
{
public CustomPromptDialog(string title, params string [] buttons) : base(
new PromptOptions<string>(prompt: title, retry: "", tooManyAttempts: "", options: new List<string>(buttons), attempts: 2, promptStyler: null))
{
}

protected async override Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> message)
{
var activity = await message;

// This is what is IMHO missing in the built-in version.
if(activity.Type != ActivityTypes.Message)
{
return;
}

await base.MessageReceivedAsync(context, message);
}
}


Am I using the prompt incorrectly or is it a bug in the framework?

Answer Source

As replied in comment, the solution for your case is to launch your Dialog containing the PromptChoice only when you are notified that a user is joining the conversation.

For that case you can check the MemberAdded info inside conversationUpdate message, and launch your dialog only when the added member is not the bot, that is to say the Id of the member is not equal to the Id of the bot, which is message.Recipient.Id:

private Activity HandleSystemMessage(Activity message)
{
    if (message.Type == ActivityTypes.ConversationUpdate)
    {
        if (message.MembersAdded.Any(o => o.Id != message.Recipient.Id))
        {
            // Your PromptChoiceDialog here        
        }
    }
    return message;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download