Answer the question
In order to leave comments, you need to log in
How to receive webhooks through google apps script and write data to google sheets?
Good afternoon.
I want to accept webhooks from the BotHelp service . The service sends data in JSON.
I poked around on the Internet and I think that doPost will suit me. Wrote code in apps script:
function doPost (e) {
var ss = SpreadsheetApp.openById('1YQmHQ07iFfQpSD-KBUZeHpFWOjOcdBkzrEmPPL9Bwf0');
var sheet = ss.getSheetByName('list');
var dateTime = Utilities.formatDate(new Date(), "GMT+3", "dd.MM.yyy HH:mm:ss");
var vkuid = e.parameter.user_id;
sheet.appendRow([dateTime, vkuid]);
return ContentService.createTextOutput("Done");
}
Answer the question
In order to leave comments, you need to log in
Found a solution with the help of Alexander Ivanov https://qna.habr.com/user/oshliaer , thanks a lot.
Here is the working code:
function doPost (e) {
var ss = SpreadsheetApp.openById('1YQmHQ07iFfQpSD-KBUZeHpFWOjOcdBkzrEmPPL9Bwf0');
var sheet = ss.getSheetByName('list');
var dateTime = Utilities.formatDate(new Date(), "GMT+3", "dd.MM.yyy HH:mm:ss");
var jsonString = e.postData.getDataAsString();
var jsonData = JSON.parse(jsonString);
var vkuid = jsonData.user_id;
//var vkuid = e.parameter.user_id;
sheet.appendRow([dateTime, vkuid]);
return ContentService.createTextOutput("Done");
}
if( !e || !e.postData || !e.postData.contents) {
console.log({title:"Bad POST data", e: e});
return;
}
contents = JSON.parse(e.postData.contents);
if (! contents) {
console.log({title:"Contents failed to decode", e:e});
return;
}
const vk_user_id = contents.user_id; // может, так?
// посмотрите что выводит в лог
console.log(contents);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question