Neal Jha
Neal Jha

Reputation: 13

Pushing Slack data to Google Sheets w/ Event Subscription

I'm trying to create a bot on Slack that sends the data of new messages sent to a private channel (that the bot is in) to a Google Sheet. I was successfully able to do this with data following a Slack slash command, by using this script:

    function doPost(e) {
  if (typeof e !== 'undefined') {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName('Sheet1');
    sheet.getRange(1,1).setValue(JSON.stringify(e));
  }
}

I tried using the same script with the Events API, but it needs to pass a request with a challenge parameter, and the endpoint must respond with the challenge value. Using the GScript web app's URL, I keep getting a failed response. How do I have the URL Verification Handshake work with Google Sheets and respond with the correct challenge string?

HTTP Post Fail

Upvotes: 1

Views: 1566

Answers (1)

Tanaike
Tanaike

Reputation: 201378

How about this modification?

The official document says as follows. This has already been mentioned in your question.

challenge: a randomly generated string produced by Slack. The point of this little game of cat and mouse is that you're going to respond to this request with a response body containing this value.

So please modify your script for returning the value of challenge from doPost as follows.

Modified script:

function doPost(e) {
  if (typeof e !== 'undefined') {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName('Sheet1');
    sheet.getRange(1,1).setValue(JSON.stringify(e));
  }
  var v = JSON.parse(e.postData.contents);  // Added
  return ContentService.createTextOutput(v.challenge);  // Added
}

Note:

  • When you modified the script of Web Apps, please redeploy the Web Apps as new version. By this, the latest script is reflected to Web Apps. Please be careful this.

References:

Added:

From your replying of I'm getting the same error., I thought that the latest script might not be reflected to Web Apps. And as a simple script, please copy and paste the following script instead of your script. And please redeploy the Web Apps as new version. Then, please test it again.

Sample script:

function doPost(e) {
  var v = JSON.parse(e.postData.contents);
  return ContentService.createTextOutput(v.challenge);
}

Upvotes: 3

Related Questions