How to fix Invalid JSON payload

Viewed 18

I'm using a script to push Google Form entries to Google Chat, but I keep getting this error:

    Exception: Request failed for https://chat.googleapis.com returned code 400. Truncated server response: {
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"content\" at 'message.cards[0].sections[0].widgets[3]... (use muteHttpExceptions option to examine full response)
    at pushToHangoutsChat(Code:23:15)

This the script that triggers when the form is submitted:

function pushToHangoutsChat(e) {
  var URL_WEBHOOK = "webhookurlgoeshere";
  var form = e.source;
  var emailRespondent = e.response.getRespondentEmail();
  var title =form.getTitle();
  var formUrl = form.getSummaryUrl().replace("viewanalytics","edit#responses")
  var items = e.response.getItemResponses();
  var widgets = [];
  for(var i = 0; i< items.length; i++) {
    var item = items[i];
    widgets.push({"keyValue": {
      "topLabel": item.getItem().getTitle(),
      "content": item.getResponse(),
      "contentMultiline": true}
                 });
  }
  var card = buildCard(title,emailRespondent,widgets,formUrl);
  var options = {
    method : "post",
    contentType : "application/json; charset=UTF-8",
    payload : JSON.stringify(card)
  };
  UrlFetchApp.fetch(URL_WEBHOOK, options);
}

Any help would be greatly appreciated

1 Answers

Figured it out. The script only works for single page forms and the failures all triggered for multiple page entries, but still worked for single page entries.

Related