[英]How to retrieve input data from an adaptive card?
I am trying to retrieve data from an adaptive card in a chatbot but I am not sure how to. 我正在尝试从聊天机器人中的自适应卡中检索数据,但不确定如何操作。
I've created an adaptive card using Adaptive card designer and have it successfully displaying in a chatbot but want to be able to retrieve the data inputted by a user once 'Submit' is selected. 我已经使用自适应卡设计器创建了一个自适应卡,并使其成功显示在聊天机器人中,但是希望一旦选择“提交”后就能够检索用户输入的数据。
I believe it would have something to do with the fact that I do not have an endpoint for the message to go to, but I am not sure how to declare one. 我认为这与我没有要发送的消息的终结点有关,但是我不确定如何声明一个终结点。
Any help is appreciated! 任何帮助表示赞赏!
C# C#
private async Task choiceCAIAResult(IDialogContext context, IAwaitable<string> result)
{
string message = await result;
if (message == "Yes")
{
var replyMessage = context.MakeMessage();
CreateAdaptiveCardApplicationJson(context, result);
// replyMessage.Attachments = new List<Attachment> { attachment };
// await context.PostAsync(replyMessage);
}
if (message == "No")
{
var replyMessage = context.MakeMessage();
Attachment attachment = GetCAIAHeroCard();
replyMessage.Attachments = new List<Attachment> { attachment };
await context.PostAsync(replyMessage);
}
}
private async Task CreateAdaptiveCardApplicationJson(IDialogContext context, IAwaitable<string> result)
{
var returnMessage = context.MakeMessage();
var json = await GetCardText("adaptiveCard");
var results = AdaptiveCard.FromJson(json);
var card = results.Card;
returnMessage.Attachments.Add(new Attachment()
{
Content = card,
ContentType = AdaptiveCard.ContentType,
Name = "Card",
});
card.Actions.Add(new AdaptiveSubmitAction() {
Title = "Next",
});
Debug.WriteLine(returnMessage.Attachments[0].Content);
await context.PostAsync(returnMessage);
}
public async Task<string> GetCardText(string cardName)
{
var path = HostingEnvironment.MapPath($"/Dialogs/{cardName}.json");
if (!File.Exists(path))
return string.Empty;
using (var f = File.OpenText(path))
{
return await f.ReadToEndAsync();
}
}
JSON JSON
{ "type": "AdaptiveCard", "body": [
{
"type": "TextBlock",
"horizontalAlignment": "Center",
"size": "Large",
"text": "Residential Aged Care Online Application Form",
"wrap": true
},
{
"type": "TextBlock",
"size": "Medium",
"text": "Applicant Details"
},
{
"type": "Input.Text",
"id": "firstName",
"placeholder": "First Name"
},
{
"type": "Input.Text",
"id": "lastName",
"placeholder": "Last Name"
},
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"horizontalAlignment": "Left",
"verticalContentAlignment": "Center",
"items": [
{
"type": "TextBlock",
"id": "dobTitle",
"horizontalAlignment": "Right",
"size": "Medium",
"text": "Date of Birth"
}
]
},
{
"type": "Column",
"items": [
{
"type": "Input.Date",
"id": "dob"
}
],
"width": "stretch"
}
]
},
{
"type": "Input.ChoiceSet",
"id": "gender",
"style": "compact",
"placeholder": "Gender",
"choices": [
{
"title": "Female",
"value": "female"
},
{
"title": "Male",
"value": "male"
},
{
"title": "Intersex",
"value": "intersex"
},
{
"title": "Indeterminate",
"value": "indeterminate"
},
{
"title": "Transgender - Female",
"value": "transFemale"
},
{
"title": "Transgender - Male",
"value": "transMale"
},
{
"title": "Other",
"value": "other"
}
]
},
{
"type": "Input.Text",
"id": "street1",
"title": "Street1",
"placeholder": "Street 1"
},
{
"type": "Input.Text",
"id": "city",
"title": "city",
"placeholder": "City"
},
{
"type": "Input.ChoiceSet",
"id": "state",
"style": "compact",
"title": "State",
"placeholder": "State",
"choices": [
{
"title": "SA",
"value": "SA"
},
{
"title": "ACT",
"value": "ACT"
},
{
"title": "NSW",
"value": "NSW"
},
{
"title": "NT",
"value": "NT"
},
{
"title": "QLD",
"value": "QLD"
},
{
"title": "TAS",
"value": "TAS"
},
{
"title": "VIC",
"value": "VIC"
},
{
"title": "WA",
"value": "WA"
},
{
"title": "N/A",
"value": "N/A"
}
]
},
{
"type": "Input.Text",
"id": "postCode",
"title": "PostCode",
"placeholder": "Post Code"
},
{
"type": "Input.Text",
"id": "phone",
"title": "Phone",
"placeholder": "Phone",
"style": "Tel"
},
{
"type": "Input.Text",
"id": "email",
"title": "Email",
"placeholder": "Email",
"style": "Email"
},
{
"type": "Input.ChoiceSet",
"id": "currentAccommodation",
"style": "compact",
"title": "CurrentAccommodation",
"placeholder": "Current Accommodation",
"choices": [
{
"title": "Home",
"value": "home"
},
{
"title": "Hospital",
"value": "hospital"
},
{
"title": "Aged Care",
"value": "agedCare"
},
{
"title": "Other",
"value": "other"
}
]
} ], "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", "version": "1.0"
}
As you mentioned, you need to have an endpoint for the message. 如前所述,您需要为消息提供一个端点。 I've used adaptive cards with RESTful web services, but any web service where you can perform an HTTP Post request will do.
我已经将自适应卡与RESTful Web服务一起使用,但是可以执行HTTP Post请求的任何Web服务都可以。 As far as I know, the only way to send data from adaptive cards is to use an HTTP POST action (anyone feel free to correct me if I'm wrong here).
据我所知,从自适应卡发送数据的唯一方法是使用HTTP POST操作(如果我在这里错了,任何人都可以纠正我)。
You'll want to look into the Actions portion of the documentation to see how to 您需要查看文档的“动作”部分,以了解如何
Here's the actions portion of a JSON package with a simple submit button: 这是带有简单提交按钮的JSON包的操作部分:
"actions": [
{
"type": "Action.Http",
"id": "Submit",
"title": "Submit",
"method": "POST",
"url": "webserviceURL",
"body": "{\n \"requestId\": \"testapprove\",\n \"callerId\": \"id\",\n \"requestContent\": \"content\",\n \"responseContent\": \"content\"\n}",
"headers": [
{
"name": "content-type",
"value": "\"application/json; charset=utf-8\""
}
]
}
],
tutorial: http://www.drdobbs.com/web-development/restful-web-services-a-tutorial/240169069 教程: http : //www.drdobbs.com/web-development/restful-web-services-a-tutorial/240169069
stack overlow post on what REST is: What are RESTful web services? 关于REST是什么的堆栈过低文章: 什么是RESTful Web服务?
You don't have to use REST in your web service, but it is becoming pretty common. 您不必在Web服务中使用REST,但是它变得非常普遍。 Googling will lead you to many tutorials on creating your own web service.
谷歌搜索将带您进入许多有关创建自己的Web服务的教程。
Use websites designed to receive POST requests from anyone. 使用旨在接收任何人的POST请求的网站。
Here's one you could use for testing: https://httpbin.org/ 这是您可以用来测试的一个: https : //httpbin.org/
Another: https://docs.postman-echo.com/ 另一个: https : //docs.postman-echo.com/
If you only need to send data locally, then google 'set up local http server'. 如果您只需要在本地发送数据,则可以使用Google“设置本地http服务器”。 There are many tutorials out there.
有很多教程。 What you choose to use will probably depend on your environment and which languages you are familiar with.
您选择使用什么可能取决于您的环境和您熟悉的语言。 Here is one for python that will echo get and post requests.
这是python的一个,它将回显get和post请求。 Here is another python one, and the comments cover how to access posted information.
这是另一个python,注释涵盖了如何访问发布的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.