自适应卡发送的提交结果与常规用户文本略有不同。当用户在聊天中输入内容并发送普通消息时,最终会出现context.activity.text
。当用户在自适应卡上填写输入时,它最终会出现在context.activity.value
,这是一个对象,其中键名称是id
在你的menuJson
值为自适应卡中的字段值。
例如,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"
}
.. 创建一张如下所示的卡片:
如果用户在文本框中输入“测试测试 123”并单击“提交”,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
.
请注意,自适应卡提交作为回发发送,这意味着提交数据不会作为对话的一部分出现在聊天窗口中 - 它保留在自适应卡上。
使用自适应卡瀑布对话框 https://learn.microsoft.com/en-us/azure/bot-service/bot-builder-concept-dialog?view=azure-bot-service-4.0#waterfall-dialogs
您的问题与此不太相关,但由于您最终可能会尝试此操作,因此我认为将其包含在我的答案中可能很重要。
本质上,自适应卡片不像提示那样工作。有提示时,将显示提示并等待用户输入,然后再继续。但是对于自适应卡(即使它包含输入框和提交按钮),自适应卡中没有代码会导致瀑布对话框在继续对话框之前等待用户输入。
因此,如果您使用接受用户输入的自适应卡,您通常希望处理用户在瀑布对话框上下文之外提交的任何内容。
话虽这么说,如果您想使用自适应卡作为瀑布对话框的一部分,有一个解决方法。基本上,你:
- 显示自适应卡
- 显示文本提示
- 将用户的自适应卡输入转换为文本提示的输入
在您的瀑布对话框文件中(步骤 1 和 2):
async displayCard(step) {
// Display the Adaptive Card
await step.context.sendActivity({
text: 'Adaptive Card',
attachments: [yourAdaptiveCard],
});
// Display a Text Prompt
return await step.prompt('textPrompt', 'waiting for user input...');
}
async handleResponse(step) {
// Do something with step.result
// Adaptive Card submissions are objects, so you likely need to JSON.parse(step.result)
...
return await step.next();
In your bot.ts
文件(步骤 3):
const activity = dc.context.activity;
if (!activity.text && activity.value) {
activity.text = JSON.stringify(activity.value);
}