嗨,我是Google脚本的新手,我该如何解析Webhook?

时间:2019-04-10 01:55:40

标签: google-apps-script web-applications webhooks

我从POS发送了一些代码,我在电子表格中添加了一个google脚本,它接收了webhook数据。尽管它将时间戳分为一个单元格,然后是几个空白单元格,然后将整个字符串都放在最后一个单元格中。 这是Google表格中的Screen Shot。下面的字符串在第6个单元格中。

{ text:
   'A Payment was made\n\nFor customer Cash Drawer Register for amount: 1.0 the message\nwas:',
  html:
   'A Payment was made \n\n For customer Cash Drawer Register for amount: 1.0 the message was: ',
  link: 'https://****.****.com/payments/****',
  attributes:
   { id: 12986288,
     created_at: '2019-04-10T11:35:05.832+10:00',
     updated_at: '2019-04-10T11:35:05.832+10:00',
     success: true,
     payment_amount: 1,
     invoice_ids: [ 14066768 ],
     ref_num: '',
     applied_at: '2019-04-10',
     payment_method: 'Cash',
     transaction_response: null,
     customer:
      { id: 7960296,
        firstname: 'Cash Drawer',
        lastname: 'Register',
        fullname: 'Cash Drawer Register',
        business_name: 'Cash Drawer Register',
        email: null,
        phone: null,
        mobile: null,
        created_at: '2017-02-02T09:12:09.186+11:00',
        updated_at: '2019-04-10T11:35:05.828+10:00',
        pdf_url: null,
        address: null,
        address_2: null,
        city: null,
        state: null,
        zip: null,
        latitude: null,
        longitude: null,
        notes: null,
        get_sms: false,
        opt_out: false,
        disabled: false,
        no_email: false,
        location_name: null,
        location_id: null,
        properties: {},
        online_profile_url: null,
        tax_rate_id: null,
        notification_email: null,
        invoice_cc_emails: null,
        invoice_term_id: null,
        referred_by: null,
        ref_customer_id: null,
        business_and_full_name: 'Cash Drawer Register - Cash Drawer Register',
        business_then_name: 'Cash Drawer Register' } } }

我只是在下面使用这个基本的webhook脚本,所以我只需要帮助解析它,以便可以在上面的代码中检索详细信息。我需要: ID: 名字: 姓: 量: 在screen shot

这样的单独单元格中
 //this is a function that fires when the webapp receives a GET request
function doGet(e) {
  return HtmlService.createHtmlOutput("request received");
}

//this is a function that fires when the webapp receives a POST request
function doPost(e) {
  var params = JSON.stringify(e.postData.contents);
  params = JSON.parse(params);
  var myData = JSON.parse(e.postData.contents);
  var testRunUrl = myData.test_run_url;
  var testRunName = myData.test_name;
  var testRunEnv = myData.environment_name;
  var testRunResult = myData.result;
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = Math.max(sheet.getLastRow(),1);
  sheet.insertRowAfter(lastRow);
  var timestamp = new Date();
  sheet.getRange(lastRow + 1, 1).setValue(timestamp);
  sheet.getRange(lastRow + 1, 2).setValue(testRunName);
  sheet.getRange(lastRow + 1, 3).setValue(testRunEnv);
  sheet.getRange(lastRow + 1, 4).setValue(testRunResult);
  sheet.getRange(lastRow + 1, 5).setValue(testRunUrl);
  sheet.getRange(lastRow + 1, 6).setValue(params);
  SpreadsheetApp.flush();
  return HtmlService.createHtmlOutput("post request received");
}

1 个答案:

答案 0 :(得分:0)

您尝试过此代码吗?

function doPost(e) {
      var content = JSON.parse(e.postData.contents);

      return ContentService
              .createTextOutput()
              .setMimeType(ContentService.MimeType.JSON)
              .setContent(JSON.stringify(content));
}