'How to use C# and Google.Cloud.Dialogflow.Cx.V3 to generate valid Google DialogFlow CX webhook response JSON

I've created a webhook using C# and ASP.NET Core in order to try to generate the webhook response to DialogFlow, but I'm really struggling with using Google.Cloud.Dialogflow.Cx.V3 to create a payload that resembles the payload I know I need to produce. I realise I could just fall back on 'hand creating' the JSON string, but I'd rather use the library if I can. Here is the response I need to create:

{
  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"
              }
            }
          ]
        }
      }
    ]
  }
}

Here is what my code currently produces (As it stands, I'm only trying to recreate the plainText part. I haven't even gotten to the richContent as yet)

{
    "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
    }
}

There are all kinds of additional fields, as well as the message seeming to be wrapped in 'webhookResponse' rather than starting at fulfillmentResponse. I'm a bit stuck. Here is the code I have so far to create the response:

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

    }
}

And I am just returning that as 'response' inside an IActionResult from the controller below:

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


Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JamesMatson