如何使用c#和google.cloud.dialogflow.cx.v3生成有效的Google DialogFlow CX Webhook响应JSON
我已经使用C#和ASP.NET Core创建了一个Webhook,以尝试生成对DialogFlow的Webhook响应,但是我确实在使用Google.cloud.dialogflow.cx.v3来创建类似于有效负载的有效负载我知道我需要生产。我意识到我可以落在“创建” JSON字符串上,但是如果可以的话,我宁愿使用库。这是我需要创建的响应:
{
fulfillmentResponse: {
messages: [
{
payload: {
plainText: "Details for order ID: {order_id}",
richContent: [
{
type: "card",
title: "{order_id}",
text: [
"<span class='subtitle'>Ordered</span>",
"{order_date}",
"<span class='subtitle'>Status</span>",
"{order_status}",
"<span class='subtitle'>Store</span>",
"{order_store}",
],
link: {
url: "{tracking_link}"text: "Track Shipment"
}
}
]
}
}
]
}
}
这是我的代码当前产生的(如目前,我只是想重新创建明文部分。我什至还没有到达Richcontent
{
"webhookResponse": {
"fulfillmentResponse": {
"messages": [
{
"text": null,
"payload": {
"fields": {
"plainText": {
"nullValue": 0,
"numberValue": 0,
"stringValue": "Details for Order: 11607",
"boolValue": false,
"structValue": null,
"listValue": null,
"kindCase": 3
}
}
},
"conversationSuccess": null,
"outputAudioText": null,
"liveAgentHandoff": null,
"endInteraction": null,
"playAudio": null,
"mixedAudio": null,
"telephonyTransferCall": null,
"messageCase": 2
}
],
"mergeBehavior": 0
},
"pageInfo": null,
"sessionInfo": null,
"payload": null,
"targetPage": "",
"targetFlow": "",
"transitionCase": 0,
"targetPageAsPageName": null,
"targetFlowAsFlowName": null
}
}
)字段以及信息似乎包裹在“ Webhookresponse”中,而不是从ExhullmentResponse开始。我有点卡住。这是我到目前为止创建响应的代码:
public class OrderStatusResponse : DialogFlowResponse
{
public OrderStatusResponse(OrderStatusDto orderStatus, string requestId)
{
this.webhookResponse = new WebhookResponse();
WebhookResponse.Types.FulfillmentResponse fulfillmentResponse = new WebhookResponse.Types.FulfillmentResponse();
this.webhookResponse.FulfillmentResponse = fulfillmentResponse;
var plainText = new Google.Protobuf.WellKnownTypes.Value();
var payload = new Google.Protobuf.WellKnownTypes.Struct();
plainText.StringValue = $"Details for Order: {orderStatus.OrderResults.First().OrderId}";
var responseItem = new ResponseMessage();
responseItem.Payload = payload;
responseItem.Payload.Fields.Add("plainText", plainText);
fulfillmentResponse.Messages.Add(responseItem);
}
}
我只是将其返回为'响应'在下面的控制器中 iActionResult :
[HttpPost]
[SwaggerResponse(200, "OrderStatusResponse", typeof(Api.Internal.Orders.Responses.OrderStatusResponse))]
[SwaggerResponse(400, "OrderNotFoundException", typeof(OrderNotFoundException))]
[SwaggerResponse(400, "InvalidRequestException", typeof(InvalidRequestException))]
[Route("search/")]
public async Task<IActionResult> GetOrdersBySearch([FromBody] OrderStatusRequest request)
{
requestId = ControllerHelper.GetRequestId(Request.HttpContext);
try
{
_logger.LogInformation($"Starting request {requestId}");
var response = await _orderService.GetOrdersBySearch(request, requestId);
return Ok(response);
}
catch (OrderNotFoundException oex)
{
_logger.LogError(oex.ToString());
return BadRequest(oex.Problem);
}
catch (InvalidRequestException irex)
{
_logger.LogError(irex.ToString());
return BadRequest(irex.Problem);
}
catch (Exception ex)
{
_logger.LogError(ex.ToString());
return StatusCode(500, new FriendlyErrorException(requestId).Problem);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最后,对我的解决方案只是使用标准的C#Pocos,而System.Text.json进行序列化 /挑选。这很好,我不需要参考Google Protobuf库。
In the end, the solution for me was just to use standard C# POCOs, and System.Text.JSON for serialising / deserialising. This works fine, I have not needed to refer to the Google protobuf libraries.
客户端库(或任何 google cloud .net .net库)类是来自 protobuf定义 - 使用
这样的东西,其中
响应
是SDK对象The Google.Cloud.Dialogflow.Cx.V3 client library (or any Google Cloud .NET library) classes are derived from Protobuf definitions - Use the Google.Protobuf.JsonFormatter to create valid JSON objects.
Something like this, where
response
is a SDK object