简体   繁体   中英

Implementing suggested actions in bot framework dialog and formFlow

var reply = activity.CreateReply("I have colors in mind, but need your help to choose the best one.");
reply.Type = ActivityTypes.Message;
reply.TextFormat = TextFormatTypes.Plain;

reply.SuggestedActions = new SuggestedActions()
{
    Actions = new List<CardAction>()
    {
        new CardAction(){ Title = "Blue", Type=ActionTypes.ImBack, Value="Blue" },
        new CardAction(){ Title = "Red", Type=ActionTypes.ImBack, Value="Red" },
        new CardAction(){ Title = "Green", Type=ActionTypes.ImBack, Value="Green" }
    }
};

How can i implement the above code in a dialog and formflow. Its quite easy doing this in the message controller.

Have a look at my article about implementing a custom prompter .

You can post a message with suggested actions in your PromptAsyncDelegate . Include whatever logic you like in order to determine whether suggested actions should be used or not. If you only want suggested actions for one field, it could look something like this:

/// <summary>
/// Here is the method we're using for the PromptAsyncDelgate.
/// </summary>
private static async Task<FormPrompt> PromptAsync(IDialogContext context, FormPrompt prompt,
    MyClass state, IField<MyClass> field)
{
    var preamble = context.MakeMessage();
    var promptMessage = context.MakeMessage();

    if (field.Name == nameof(ColorField))
    {
        promptMessage.Text =
            "I have colors in mind, but need your help to choose the best one.";

        promptMessage.SuggestedActions = new SuggestedActions()
        {
            Actions = new List<CardAction>()
            {
                new CardAction(){ Title = "Blue", Type=ActionTypes.ImBack, Value="Blue" },
                new CardAction(){ Title = "Red", Type=ActionTypes.ImBack, Value="Red" },
                new CardAction(){ Title = "Green", Type=ActionTypes.ImBack, Value="Green" },
            }
        };
    }
    else
    {
        if (prompt.GenerateMessages(preamble, promptMessage))
        {
            await context.PostAsync(preamble);
        }
    }

    await context.PostAsync(promptMessage);

    return prompt;
}

As seen in the article, you include the PromptAsyncDelegate in your form builder like this:

var builder = new FormBuilder<MyClass>().Prompter(PromptAsync);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM