在bot框架对话框和formFlow中实现建议的操作

时间:2019-01-22 11:30:03

标签: c# botframework

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" }
    }
};

如何在对话框和Formflow中实现以上代码。在消息控制器中这样做非常容易。

1 个答案:

答案 0 :(得分:1)

看看我有关implementing a custom prompter的文章。

您可以在PromptAsyncDelegate中发布带有建议操作的消息。包括您喜欢的任何逻辑以确定是否应使用建议的操作。如果您只希望针对一个字段建议采取的措施,则可能看起来像这样:

/// <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;
}

如本文所述,您可以像这样在表单构建器中加入PromptAsyncDelegate

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