[英]How to use C# and Google.Cloud.Dialogflow.Cx.V3 to generate valid Google DialogFlow CX webhook response 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”中,而不是从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);
}
}
我只是在下面的控制器的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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.