[英]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.