Tim*_*nso 3 c# bots botframework
我使用的是Bot Framework(V4),并且有一个分为两个步骤的WaterfallDialog;第一步是捕获表单数据,第二步是处理表单数据。
第一步发送答复:
private async Task<DialogTurnResult> CaptureFormStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
var reply = await stepContext.Context.Activity.GetReplyFromCardAsync("BusinessyForm");
await stepContext.Context.SendActivityAsync(reply, cancellationToken);
}
Run Code Online (Sandbox Code Playgroud)
的GetReplyFromCardAsync是一个扩展方法这需要的JSON表示Activity具有自适应卡作为attachnent。自适应卡与此类似。自适应卡还具有提交动作。
我的问题是如何将这些信息传递给下一步?
在先前的尝试中,我已返回提示:
return await stepContext.PromptAsync(
"custom-prompt-id",
new PromptOptions { Prompt = MessageFactory.Text("Hello!") },
cancellationToken);
Run Code Online (Sandbox Code Playgroud)
但是在这种情况下,我不想提示用户其他信息。理想情况下,我想“保持”对话框状态以进行响应,当检测到响应时,下一步将处理提交结果。
我尝试过的事情:
这可行吗?任何帮助将非常感激!
自适应卡发送的“提交”结果与常规用户文本略有不同。当用户输入聊天内容并发送普通消息时,其结尾为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"
}
Run Code Online (Sandbox Code Playgroud)
..创建一张看起来像这样的卡片:
如果用户在文本框中输入“ 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' }
Run Code Online (Sandbox Code Playgroud)
可以在中查看用户提交的内容Context.Activity.Value.userText。
请注意,自适应卡提交是作为postBack发送的,这意味着提交数据不会作为对话的一部分出现在聊天窗口中,而是保留在自适应卡上。
在瀑布对话框中使用自适应卡
本地,自适应卡不能像提示一样工作。带有提示,提示将显示并等待用户输入,然后继续。但是对于自适应卡(即使它包含一个输入框和一个提交按钮),自适应卡中也没有代码,这会使瀑布对话框在继续对话框之前先等待用户输入。
因此,如果您使用的是接受用户输入的自适应卡,则通常希望处理用户在“瀑布对话框”上下文之外提交的所有内容。
话虽如此,如果您想在瀑布对话框中使用自适应卡,则有一种解决方法。基本上,您:
在“瀑布对话框”类中(步骤1和2):
{
"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"
}
Run Code Online (Sandbox Code Playgroud)
在您的主要bot类(<your-bot>.cs)中,在OnTurnAsync()该方法的开头附近的下方,await dialogContext.ContinueDialogAsync(cancellationToken)会调用之前的某个位置(第3步):
{ 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' }
Run Code Online (Sandbox Code Playgroud)