嗨,我是谷歌脚本的新手,我如何解析webhook?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了嗨,我是谷歌脚本的新手,我如何解析webhook?相关的知识,希望对你有一定的参考价值。
我从我的POS发送了一些代码,我在我的电子表格中添加了一个google脚本,并且它接收了webhook数据。虽然它将时间戳分为一个单元格,然后是几个空白单元格,然后是最后一个单元格中的整个字符串。这是来自谷歌表的Screen Shot。下面的字符串在第6个单元格中。
{ text:
'A Payment was made
For customer Cash Drawer Register for amount: 1.0 the message
was:',
html:
'A Payment was made
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:firstname:lastname:amount:在像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");
}
答案
你试过这段代码吗?
function doPost(e) {
var content = JSON.parse(e.postData.contents);
return ContentService
.createTextOutput()
.setMimeType(ContentService.MimeType.JSON)
.setContent(JSON.stringify(content));
}
以上是关于嗨,我是谷歌脚本的新手,我如何解析webhook?的主要内容,如果未能解决你的问题,请参考以下文章
嗨,我是 react native 的新手,我想知道如何限制将收到的状态值