[英]How to validate input fields in adaptive card bot framework v4 (waterfall model) c#
[英][BotFramework]: How to capture/extract the values submitted through Adaptive card rendered in C# Web Chat bot in a waterfall dialog?
我已经使用C#使用SDK 4在C#中创建了聊天机器人,该聊天机器人具有多个对话框,每个对话框都调用另一个对话框,在一个对话框中,我正在STEP#1中渲染自适应卡,该卡只有2个输入,并带有“确定”按钮:1.日期2.时间3。 OK按钮,以便我可以提取/捕获通过STEP#2中的OK按钮提交的值,然后继续进行处理。
问题:如何在C#的瀑布对话框中提取在STEP#2的步骤#1中提交的值?
语言:C#
Bot SDK:V4
请提供逐步指南,以帮助我刚接触BOT和编码?
我已经尝试过一些操作,例如:1.将呈现的卡片放在提示中2.尝试通过以下步骤提取/捕获值:stepContext.Context.Activity.Value
所有这些都无济于事。
步骤1: var cardAttachment = CreateAdaptiveCardAttachment(this.cards);
var reply = stepContext.Context.Activity.CreateReply();
reply.Attachments = new List<Attachment>() { cardAttachment };
return await stepContext.Context.SendActivityAsync(reply);
//或传return await stepContext.PromptAsync("datetextPrompt", new PromptOptions()
{
Prompt = reply,
});
步骤#2:我想提取或获取价值? 怎么做?
在瀑布对话框中使用自适应卡
本地,自适应卡不能像提示一样工作。 带有提示,提示将显示并等待用户输入,然后继续。 但是对于自适应卡(即使它包含一个输入框和一个提交按钮),自适应卡中也没有代码,这会使瀑布对话框在继续对话框之前先等待用户输入。
因此,如果您正在使用需要用户输入的自适应卡,则通常需要处理用户在“瀑布对话框”上下文之外提交的所有内容。
话虽如此,如果您想在瀑布对话框中使用自适应卡,则有一种解决方法。 基本上,您:
在“瀑布对话框”类中(步骤1和2):
private async Task<DialogTurnResult> DisplayCardAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
// Display 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),
};
var message = MessageFactory.Text("");
message.Attachments = new List<Attachment>() { cardAttachment };
await stepContext.Context.SendActivityAsync(message, cancellationToken);
// Create the text prompt
var opts = new PromptOptions
{
Prompt = new Activity
{
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();
}
在您的主要bot类( <your-bot>.cs
)中,在OnTurnAsync()
,在该方法的开头附近,在await dialogContext.ContinueDialogAsync(cancellationToken)
之前的某个位置被调用(步骤3):
var activity = turnContext.Activity;
if (string.IsNullOrWhiteSpace(activity.Text) && activity.Value != null)
{
activity.Text = JsonConvert.SerializeObject(activity.Value);
}
自适应卡发送的“提交”结果与常规用户文本略有不同。 当用户键入聊天并发送正常消息时,它最终出现在Context.Activity.Text
。 当用户填写自适应卡上的输入时,它会以Context.Activity.Value
结尾,该对象是键名称是您卡中的id
,而值是自适应卡中的字段值的对象。
例如,json:
{
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"text": "Test Adaptive Card"
},
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"items": [
{
"type": "TextBlock",
"text": "Text:"
}
],
"width": 20
},
{
"type": "Column",
"items": [
{
"type": "Input.Text",
"id": "userText",
"placeholder": "Enter Some Text"
}
],
"width": 80
}
]
}
],
"actions": [
{
"type": "Action.Submit",
"title": "Submit"
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.0"
}
..创建一张看起来像这样的卡片:
如果用户在文本框中输入“ Testing Testing 123”,然后单击Submit,则Context.Activity
类似于:
{ type: 'message',
value: { userText: 'Testing Testing 123' },
from: { id: 'xxxxxxxx-05d4-478a-9daa-9b18c79bb66b', name: 'User' },
locale: '',
channelData: { postback: true },
channelId: 'emulator',
conversation: { id: 'xxxxxxxx-182b-11e9-be61-091ac0e3a4ac|livechat' },
id: 'xxxxxxxx-182b-11e9-ad8e-63b45e3ebfa7',
localTimestamp: 2019-01-14T18:39:21.000Z,
recipient: { id: '1', name: 'Bot', role: 'bot' },
timestamp: 2019-01-14T18:39:21.773Z,
serviceUrl: 'http://localhost:58453' }
用户提交可以在Context.Activity.Value.userText
看到。
请注意,自适应卡提交是作为postBack发送的,这意味着提交数据不会作为对话的一部分出现在聊天窗口中,而是保留在自适应卡上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.