Skip to content

Instantly share code, notes, and snippets.

@tanepiper
Last active September 9, 2015 23:51
Show Gist options
  • Save tanepiper/07e7deffec310397254f to your computer and use it in GitHub Desktop.
Save tanepiper/07e7deffec310397254f to your computer and use it in GitHub Desktop.
A Google Apps script to push
// I can't figure out how to add custom text to the `payload` variable above.
// I've tried:
var accessRequest = "The following user wants to join WADE:" + contactHow
var payload = {
channel: "#botspam",
username: "recruitbot",
text: accessRequest,
icon_emoji: ":raised_hands::skin-tone-4:"
};
var options = {
'method': 'post',
'payload': 'payload=' + JSON.stringify(payload)
};
var response = UrlFetchApp.fetch(url, options);
// But get "500, invalid JSON object"
// Any ideas?
var REQ_SHEET = "Form Responses 1";
var SLACK_API = "PUT YOURS IN";
var BOT_ACTION = "Sent to Slack!";
function myFunction() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(REQ_SHEET); // hard coding which spreadsheet
var lastRow = sheet.getLastRow();
var startRow = 1; // First row of data to process
var numRows = 1; // how many do we retrieve?
// Fetch the range of cells
var dataRange = sheet.getRange(startRow, 1, numRows, 11) // Actual bounds up to 11 to get row[10]
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i]
var opener = "The following user wants to join WADE: ";
var contactHow = row[1]; // is column B is EMAIL
var aboutMe = row[2]; // is column C
var accessRequest = opener.concat(contactHow);
//
// I can't figure out how to push the actual details into this request. Getting invalid JSON. Going the lazy route for now.
//
// Time to build our payload!
var payload = {
channel: "#botspam",
username: "recruitbot",
text: "there is a new request. Go check it out <https://docs.google.com/spreadsheets/d/1NL8afyuMg9Wqof0O0QuNFTayAI-tQ8D1Lm8DAIN6TyM/edit?usp=sharing>",
icon_emoji: ":raised_hands::skin-tone-4:"
};
var url = SLACK_API;
var options = {
'method': 'post',
'payload': 'payload=' + JSON.stringify(payload)
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(response.getContentText());
sheet.getRange(startRow + i, 4).setValue(BOT_ACTION); // reuse this!
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment