How to get a return message to slack

I am setting up a / command in slack through google sheets and the return is a mess, but I think I am doing something wrong with the code but I can not figure out what.

function doPost(e) {
    if (typeof e !== 'undefined') {
        var ss = SpreadsheetApp.getActiveSpreadsheet();
        var sheet = ss.getSheetByName('Sheet1');
        sheet.getRange(1,1).setValue(JSON.stringify(e));
    }
    return ContentService.createTextOutput('Thank you for your whitelist input! You rock! :kea: 
   :tada:');
}

The response is:

<!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>

Rather than the Thank you message, I am just trying to get it to return the thank you message

Upvotes: 0

Views: 149

Answers (1)

0Valt
0Valt

Reputation: 10365

notice: in case I am wrong with the interpretation, let me know and the answer will be updated

Problem

Stringifying the whole event object instead of a specific parameter

Update

After the discussion, I was able to find out that the issue was, for the most part, related to the behaviour of the /exec endpoint - the solution is to update the script version for the latest code snapshot to become available for incoming requests.

Generic Solution

When a request is made to a Web App that has a doPost function declared with POST method, the event object is constructed automatically and has the following structure at the time of writing:

Event object structure

| Property      | Type   |
| ------------- | ------ |
| queryString   | String |
| parameter     | Object |
| parameters    | Object |
| contextPath   | String |
| contentLength | Number |
| postData      | Object |

postData property structure

| Property | Type   |
| -------- | ------ |
| length   | Number |
| type     | String |
| contents | String |
| name     | String |

The data you actually need should definitely be contained in parameter, parameters or postData depending on your solution.

Optimizations

  1. You don't have to check for e to be undefined for the reason explained above. In case you want to test your code by running in editor, separate the actual logic from the trigger (for example, move it to a function).

References

  1. Web Apps event object reference

Upvotes: 1

Related Questions