在SDK4中使用自适应卡时如何等待用户输入

时间:2019-06-13 20:04:00

标签: c# botframework

我正在尝试使用Virtual Assistant模板中的Waterfallstep解析自适应卡,它不等待用户输入被填充到卡中。当我使用Multiprompt模板时,以下代码有效,但在Virtual Assistant模板中不起作用。

private async Task<DialogTurnResult> 
StartTheDialogAsync(WaterfallStepContext sc, CancellationToken 
cancellationToken)
{
 var attachment = CreateAdaptiveCardAttachment(cards);
 var replyMessage = sc.Context.Activity.CreateReply();
 replyMessage.Attachments.Add(attachment);
 await sc.Context.SendActivityAsync(replyMessage, cancellationToken);
 return new DialogTurnResult(DialogTurnStatus.Waiting);
 }

1 个答案:

答案 0 :(得分:0)

Bot框架中没有任何东西可以使对话框在发送自适应卡后等待用户输入。我的猜测是由于示例的编码方式,它在MultiPrompt示例中同时起作用。

强制等待的两种方法是:

  1. 在自适应卡之后直接发送空白文本提示
  2. 将自适应卡作为附件发送到空白文本提示

请参阅以下任一答案以获取更多详细信息:

您基本上需要两段代码:

private async Task<DialogTurnResult> DisplayCardAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
    // Create the Adaptive Card
    var cardPath = Path.Combine(".", "AdaptiveCard.json");
    var cardJson = File.ReadAllText(cardPath);
    var cardAttachment = new Attachment()
    {
        ContentType = "application/vnd.microsoft.card.adaptive",
        Content = JsonConvert.DeserializeObject(cardJson),
    };

    // Create the text prompt
    var opts = new PromptOptions
    {
        Prompt = new Activity
        {
            Attachments = new List<Attachment>() { cardAttachment },
            Type = ActivityTypes.Message,
            Text = "waiting for user input...", // You can comment this out if you don't want to display any text. Still works.
        }
    };

    // Display a Text Prompt and wait for input
    return await stepContext.PromptAsync(nameof(TextPrompt), opts);
}

private async Task<DialogTurnResult> HandleResponseAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
    // Do something with step.result
    // Adaptive Card submissions are objects, so you likely need to JObject.Parse(step.result)
    await stepContext.Context.SendActivityAsync($"INPUT: {stepContext.Result}");
    return await stepContext.NextAsync();
}

var activity = turnContext.Activity;

if (string.IsNullOrWhiteSpace(activity.Text) && activity.Value != null)
{
    activity.Text = JsonConvert.SerializeObject(activity.Value);
}

同样,请阅读链接的答案以获取更多详细信息。