简体   繁体   中英

Unable to enable Slack's "Enable Events" through Google Scripts

So I'm attempting to create a SlackBot which can track reacts to a message it posted. However, I'm also trying to read from a Google Sheet, so the code for the bot will be in a Google Script connecting to the form.

I clearly need to enable events in order to see reacts, and the first step is to receive and respond to a POST request Slack sends:

Our Request:
POST
"body": { 
     "type": "url_verification",
     "token": "---",
     "challenge": "---"
}

I have to respond with something along the lines of:

HTTP 200 OK, 
Content-type: text/plain, 
"the challenge code"

When I recieve the POST request, it overwrites square 1,1 of the Google Sheet, which is not great, but more concerningly, my doPost function doesn't run.

Here's what I get back from Slack:

Your Response:
"code": 200
"error": "challenge_failed"
"body": {
 <!DOCTYPE html><html><head><link rel="shortcut icon" href="//ssl.gstatic.com/docs/script/images/favicon.ico"><title>Error</title><style type="text/css">body {background-color: #fff; margin: 0; padding: 0;}.errorMessage {font-family: Arial,sans-serif; font-size: 12pt; font-weight: bold; line-height: 150%; padding-top: 25px;}</style></head><body style="margin:20px"><div><img alt="Google Apps Script" src="//ssl.gstatic.com/docs/script/images/logo.png"></div><div style="text-align:center;font-family:monospace;margin:50px auto 0;max-width:600px">The script completed but did not return anything.</div></body></html> 
}

Now, here's all the code I have in my Google Script.

 function doPost(request){ var postJSON = request.postData.getDataAsString(); var sheet = SpreadsheetApp.getActiveSheet(); // doesn't happen sheet.getRange(5, 3).setValue("YEAH"); // when I send a req using CURL, I get the message // "The script completed but did not return anything" return postJSON; }

It's already deployed as a web app, and I've been updating it every time I make a change to the code, so that isn't the issue.

Any advice would be appreciated, thank you!

I think that in this case, it is required to return the value of challenge . So how about the following modification?

From:

var postJSON = request.postData.getDataAsString();

To:

var postJSON = JSON.parse(request.postData.getDataAsString());

And also, please modify as follows.

From:

return postJSON;

To:

return ContentService.createTextOutput(postJSON.challenge);

Note:

  • In this modification, it supposes that postJSON in your current script is {"type": "url_verification", "token": "---", "challenge": "---"} . Please be careful this.
  • Also, it supposes that the Web Apps is deployed as Execute the app as: Me and Who has access to the app: Anyone, even anonymous .
  • When the script of Web Apps is modified, please redeploy it as new version. By this, the latest script is reflected to the Web Apps. Please be careful this.

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM