在瀑布对话中使用建议的动作

时间:2019-01-05 00:43:33

标签: c# botframework

在waterstep上下文中使用建议的动作

    private static async Task PromptOptions3(string prompt, string optionA, string optionB, string optionC, WaterfallStepContext stepContext, CancellationToken cancellationToken)

    {
        var reply = stepContext.Context.Activity.CreateReply(prompt);
        reply.SuggestedActions = new SuggestedActions()
        {
            Actions = new List<CardAction>()
            {
                new CardAction() { Title = optionA, Value = optionA },
                new CardAction() { Title = optionB, Value = optionB },
                new CardAction() { Title = optionC, Value = optionC },
            },
        };
        await stepContext.Context.SendActivityAsync(reply, cancellationToken);
    }




    private async Task<DialogTurnResult> PromptForRequestStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        // Save name, if prompted.
        var greetingState = await UserProfileAccessor.GetAsync(stepContext.Context);
        var lowerCaseName = stepContext.Result as string;
        if (string.IsNullOrWhiteSpace(greetingState.Name) && lowerCaseName != null)
        {
            // Capitalize and set name.
            greetingState.Name = char.ToUpper(lowerCaseName[0]) + lowerCaseName.Substring(1);
            await UserProfileAccessor.SetAsync(stepContext.Context, greetingState);
        }     
        if (greetingState.Request == "1")
        {
            var opts = new PromptOptions
            {
                Prompt = new Activity
                {
                    Type = ActivityTypes.Message,
                    Text = "please work"
                },
            };
            await PromptOptions3("Choose from the following:", "Login to OneDrive", "Upload a file", "Create a folder", stepContext, cancellationToken);
            return await stepContext.PromptAsync(OneDrivePrompt, opts);
        }

建议的操作没有出现。我希望它会显示出来,用户只需单击它即可作为输入,而无需键入。当我用一个简单的代码而不是Waterfallstep尝试它时,它就起作用了。我不知道如何解决此问题,因为我不熟悉bot框架。

1 个答案:

答案 0 :(得分:2)

所以,我不知道您的OneDrivePrompt现在是什么样的提示,但是我想这不是ChoicePrompt,坦率地说,这就是您真正想要的提示因为它将完成提供一组选项并确保一个人选择其中一个选项的所有工作。

首先,您要将OneDrivePrompt更改为如下配置的ChoicePrompt

yourDialogSet.Add(new ChoicePrompt(OneDrivePrompt) { Style = ListStyle.SuggestedAction });

接下来,您将想要更改瀑布步骤以与ChoicePrompt配合使用,以专门让 it 展示选项并验证是否选择了其中一个选项:

private async Task<DialogTurnResult> PromptForRequestStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
    // Save name, if prompted.
    var greetingState = await UserProfileAccessor.GetAsync(stepContext.Context);
    var lowerCaseName = stepContext.Result as string;
    if (string.IsNullOrWhiteSpace(greetingState.Name) && lowerCaseName != null)
    {
        // Capitalize and set name.
        greetingState.Name = char.ToUpper(lowerCaseName[0]) + lowerCaseName.Substring(1);
        await UserProfileAccessor.SetAsync(stepContext.Context, greetingState);
    }     
    if (greetingState.Request == "1")
    {
        var opts = new PromptOptions
        {
            Prompt = MessageFactory.Text("Choose from the following:")
            Choices = new List<Choice>
            {
                new Choice("Login to OneDrive"),
                new Choice("Upload a file"),
                new Choice("Create a folder"),
            },
        };

        return await stepContext.PromptAsync(OneDrivePrompt, opts);
    }