如何使用c#和Google.Cloud.Dialogflow.Cx.生成有效的Google DialogFlow CX



我用c#和ASP创建了一个webhook。为了尝试生成webhook响应DialogFlow,但我真的很难使用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'中,而不是从fulfillmentResponse开始。我有点卡住了。下面是我到目前为止创建响应的代码:

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);
}
}

我只是返回'response'在一个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);
}
}

最后,我的解决方案是使用标准的c# poco,并使用System.Text.JSON进行序列化/反序列化。这工作得很好,我不需要参考Google protobuf库。

Google.Cloud.Dialogflow.Cx。V3客户端库(或任何Google Cloud . net库)类派生自Protobuf定义-使用Google.Protobuf. jsonformatter创建有效的JSON对象。

像这样,其中response是一个SDK对象

using Google.Protobuf;
...
JsonFormatter jf = new JsonFormatter(new JsonFormatter.Settings(true));
Console.WriteLine(jf.Format(response));

相关内容

  • 没有找到相关文章